Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add doc about target as a function #1262

Merged
merged 5 commits into from
Jul 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions content/configuration/target.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,57 @@ contributors:
- pastelsky
---

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

## `target`

`string`
`string | function(compiler)`

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):
Intructs webpack to target a specific environment.

| `target` | Description |
| ------------- |------------------------|
| `async-node`| Compile for usage in a Node.js-like environment (uses `fs` and `vm` to load chunks asynchronously) |
| ~~`atom`~~ | Alias for `electron-main` |
| ~~`electron`~~ | Alias for `electron-main` |
| `electron-main` | Compile for [Electron](http://electron.atom.io/) for main process. |
| `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. |
| `node` | Compile for usage in a Node.js-like environment (uses Node.js `require` to load chunks) |
|`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) |
|`web`| Compile for usage in a browser-like environment **(default)** |
|`webworker`| Compile as WebWorker |

### `string`

The following string values are supported via [`WebpackOptionsApply`](https://github.com/webpack/webpack/blob/master/lib/WebpackOptionsApply.js):

Option | Description
--------------------- | -----------------------
`async-node` | Compile for usage in a Node.js-like environment (uses `fs` and `vm` to load chunks asynchronously)
~~`atom`~~ | Alias for `electron-main`
~~`electron`~~ | Alias for `electron-main`
`electron-main` | Compile for [Electron](http://electron.atom.io/) for main process.
`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.
`node` | Compile for usage in a Node.js-like environment (uses Node.js `require` to load chunks) |
`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)
`web` | Compile for usage in a browser-like environment **(default)**
`webworker` | Compile as WebWorker

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).


### `function`

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.

For example, if you don't want any of the plugins they applied:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they here seems unwarranted, just pointing it out.

cc @skipjack @jchip

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to send a PR if it still exists


```js
const options = {
target: () => undefined
};
```

Or you can apply specific plugins you want:

```js
const webpack = require("webpack");

const options = {
target: (compiler) => {
compiler.apply(
new webpack.JsonpTemplatePlugin(options.output),
new webpack.LoaderTargetPlugin("web")
);
}
};
```