Target
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)
Instructs webpack to target a specific environment.
string
The following string values are supported via WebpackOptionsApply
:
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) |
Compile for Electron for main process. | Compile for Electron for main process. |
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. |
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. |
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) |
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) |
Compile for usage in a browser-like environment (default) | Compile for usage in a browser-like environment (default) |
Compile as WebWorker | Compile as WebWorker |
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.
function
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:
const options = { target: () => undefined };
Or you can apply specific plugins you want:
const webpack = require('webpack'); const options = { target: (compiler) => { compiler.apply( new webpack.JsonpTemplatePlugin(options.output), new webpack.LoaderTargetPlugin('web') ); } };
© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://v4.webpack.js.org/configuration/target