Skip to content

Commit 7270a60

Browse files
jchipskipjack
authored andcommitted
docs(config): update target.md (#1262)
Add documentation on passing a function for `target`. Also include some minor tweaks and formatting improvements.
1 parent 76cd022 commit 7270a60

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

content/configuration/target.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,57 @@ contributors:
99
- pastelsky
1010
---
1111

12-
webpack can compile for multiple environments or _targets_. To understand what a target is in detail, read [the concepts](/concepts/targets).
12+
webpack can compile for multiple environments or _targets_. To understand what a `target` is in detail, read through [the targets concept page](/concepts/targets).
1313

1414
## `target`
1515

16-
`string`
16+
`string | function(compiler)`
1717

18-
Tells webpack which environment the application is targeting. The following values are supported via [`WebpackOptionsApply`](https://github.com/webpack/webpack/blob/master/lib/WebpackOptionsApply.js):
18+
Intructs webpack to target a specific environment.
1919

20-
| `target` | Description |
21-
| ------------- |------------------------|
22-
| `async-node`| Compile for usage in a Node.js-like environment (uses `fs` and `vm` to load chunks asynchronously) |
23-
| ~~`atom`~~ | Alias for `electron-main` |
24-
| ~~`electron`~~ | Alias for `electron-main` |
25-
| `electron-main` | Compile for [Electron](http://electron.atom.io/) for main process. |
26-
| `electron-renderer` | Compile for [Electron](http://electron.atom.io/) for renderer process, providing a target using `JsonpTemplatePlugin`, `FunctionModulePlugin` for browser environments and `NodeTargetPlugin` and `ExternalsPlugin` for CommonJS and Electron built-in modules. |
27-
| `node` | Compile for usage in a Node.js-like environment (uses Node.js `require` to load chunks) |
28-
|`node-webkit`| Compile for usage in WebKit and uses JSONP for chunk loading. Allows importing of built-in Node.js modules and [`nw.gui`](http://docs.nwjs.io/en/latest/) (experimental) |
29-
|`web`| Compile for usage in a browser-like environment **(default)** |
30-
|`webworker`| Compile as WebWorker |
20+
21+
### `string`
22+
23+
The following string values are supported via [`WebpackOptionsApply`](https://github.com/webpack/webpack/blob/master/lib/WebpackOptionsApply.js):
24+
25+
Option | Description
26+
--------------------- | -----------------------
27+
`async-node` | Compile for usage in a Node.js-like environment (uses `fs` and `vm` to load chunks asynchronously)
28+
~~`atom`~~ | Alias for `electron-main`
29+
~~`electron`~~ | Alias for `electron-main`
30+
`electron-main` | Compile for [Electron](http://electron.atom.io/) for main process.
31+
`electron-renderer` | Compile for [Electron](http://electron.atom.io/) for renderer process, providing a target using `JsonpTemplatePlugin`, `FunctionModulePlugin` for browser environments and `NodeTargetPlugin` and `ExternalsPlugin` for CommonJS and Electron built-in modules.
32+
`node` | Compile for usage in a Node.js-like environment (uses Node.js `require` to load chunks) |
33+
`node-webkit` | Compile for usage in WebKit and uses JSONP for chunk loading. Allows importing of built-in Node.js modules and [`nw.gui`](http://docs.nwjs.io/en/latest/) (experimental)
34+
`web` | Compile for usage in a browser-like environment **(default)**
35+
`webworker` | Compile as WebWorker
3136

3237
For example, when the `target` is set to `"electron"`, webpack includes multiple electron specific variables. For more information on which templates and externals are used, you can refer to webpack's [source code](https://github.com/webpack/webpack/blob/master/lib/WebpackOptionsApply.js#L70-L185).
38+
39+
40+
### `function`
41+
42+
If a function is passed, then it will be called with the compiler as a parameter. Set it to a function if none of the predefined targets from the list above meet your needs.
43+
44+
For example, if you don't want any of the plugins they applied:
45+
46+
```js
47+
const options = {
48+
target: () => undefined
49+
};
50+
```
51+
52+
Or you can apply specific plugins you want:
53+
54+
```js
55+
const webpack = require("webpack");
56+
57+
const options = {
58+
target: (compiler) => {
59+
compiler.apply(
60+
new webpack.JsonpTemplatePlugin(options.output),
61+
new webpack.LoaderTargetPlugin("web")
62+
);
63+
}
64+
};
65+
```

0 commit comments

Comments
 (0)