CodeCoupler Webpack Externals Plugin Configuration Reference
You have to define each module that should be handled as external:
- In a file named
webpack.externals.jsin your root project folder. - Or in the plugin option
externals4.
If both configurations are set, both will be merged.
An absolute minimum webpack.externals.js file content would be:
As you can see an array will be defined here. Each item in this array is an object which define an external module. The available options for each entry are:
- module: The name of the module (corresponds to the package name).
- alias: Optional. String or array of strings 5. The module name you can use in your import and require statements instead of the module name. You can specify multiple module names here. They will all refer to the same installed module.
- global: Optional. The global variable name to access the library provided by the node module. Can be a string or an array if the variable is in a sub property of an global object.
- entries: Optional. String or array of strings. Name of files with the extension
.js,.cjs2 or.cssto copy from the modules folder to thedistfolder and inject links to them into your HTML files. - copy: Optional. String or array of strings. Globs to copy from the modules folder to the
distfolder. This can be used to copy assets like fonts. - includeExternals: If true the
webpack.externals.jsof the module will be read and merged (default is false 3). - moveTo: 1 This have to be a string
toporbottomor an object{ module: String, place: "before" or "after"}. See below.
Walkthrough Example
Here is an example to define jQuery as an external library:
This configuration will:
- Exclude the module
jqueryfrom the build - Define the global variable
jQueryas the source for this library - Copy the file
dist/jquery.min.jsfrom thenode_modulesfolder of jQuery to a subdirectory of yourdistfolder namedvendor/jquery-X.X.X/dist/jquery.min.js(the current version will be read from the current installed package) - Copy the corresponding source map (if exists of course)
dist/jquery.min.js.mapalso to the destination folder - Inject a link like
<script src="vendor/jquery/dist/jquery.min.js">to your HTML files processed by the html-webpack-plugin
Now we extend the example by adding the Bootstrap module into the configuration:
As you can see, the key global is not necessary and entries can be a single string or an array
of strings. All entries must end with .js, .cjs 2 or .css. All other exetensions will be ignored.
Last but not least we add the module of Font Awesome to the configuration:
Here you see the key copy in action. This can be a string or an array of strings. All the globs
defined here will be just copied to the destination folder but not injected anywhere. Here you can
define single files or folders.