async-node
Compile for usage in a Node.js-like environment (uses fs
and vm
to load chunks asynchronously)
webpack can compile for multiple environments or targets. To understand what a target
is in detail, read through the targets concept page.
target
string
function (compiler) => string
Instructs webpack to target a specific environment. Defaults to 'browserslist'
or to 'web'
when no browserslist configuration was found.
string
The following string values are supported via WebpackOptionsApply
:
Option | Description |
---|---|
Option Description
Compile for usage in a Node.js-like environment (uses |
Compile for usage in a Node.js-like environment (uses
fs
and
vm
to load chunks asynchronously) |
Option Description
Compile for Electron for main process. |
Compile for Electron for main process. |
Option Description
Compile for Electron for renderer process, providing a target using |
Compile for
Electron
for renderer process, providing a target using
JsonpTemplatePlugin
,
FunctionModulePlugin
for browser environments and
NodeTargetPlugin
and
ExternalsPlugin
for CommonJS and Electron built-in modules. |
Option Description
Compile for Electron for renderer process, providing a target using |
Compile for
Electron
for renderer process, providing a target using
NodeTemplatePlugin
with
asyncChunkLoading
set to
true
,
FunctionModulePlugin
for browser environments and
NodeTargetPlugin
and
ExternalsPlugin
for CommonJS and Electron built-in modules. |
Option Description
Compile for usage in a Node.js-like environment (uses Node.js |
Compile for usage in a Node.js-like environment (uses Node.js
require
to load chunks) |
Option Description
Compile for usage in WebKit and uses JSONP for chunk loading. Allows importing of built-in Node.js modules and |
Compile for usage in WebKit and uses JSONP for chunk loading. Allows importing of built-in Node.js modules and
nw.gui
(experimental) |
Option Description
Compile for usage in a browser-like environment (default) |
Compile for usage in a browser-like environment (default) |
Option Description
Compile as WebWorker |
Compile as WebWorker |
Option Description
Infer a platform and the ES-features from a browserslist-config |
Infer a platform and the ES-features from a browserslist-config |
For example, when the target
is set to "electron-main"
, webpack includes multiple electron specific variables. For more information on which templates and externals are used, you can refer to webpack's source code.
browserslist
If a project has a browserslist config, then webpack will use it for:
last 2 node versions
the same as target: "node"
with some output.environment
settings).Supported browserslist values:
browserslist
- use automatically resolved browserslist config and environment (from the nearest package.json
or BROWSERLIST
environment variable)browserslist:modern
- use modern
environment from automatically resolved browserslist configbrowserslist:last 2 versions
- use an explicit browserslist query (config will be ignored)browserslist:/path/to/config
- explicitly specify browserslist configbrowserslist:/path/to/config:modern
- explicitly specify browserslist config and an environmentfunction
If a function is passed, then it will be called with the compiler as a parameter. Set target
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 applied:
webpack.config.js
module.exports = {
// ...
target: () => undefined
};
Or you can apply specific plugins you want:
webpack.config.js
const webpack = require('webpack');
module.exports = {
// ...
target: (compiler) => {
new webpack.JsonpTemplatePlugin(options.output).apply(compiler);
new webpack.LoaderTargetPlugin('web').apply(compiler);
}
};