SourceMapDevToolPlugin
This plugin enables more fine grained control of source map generation. It is also enabled automatically by certain settings of the devtool configuration option.
new webpack.SourceMapDevToolPlugin(options);
Options
The following options are supported:
-
test(stringRegExp[string, RegExp]): Include source maps for modules based on their extension (defaults to.js,.mjs, and.css). -
include(stringRegExp[string, RegExp]): Include source maps for module paths that match the given value. -
exclude(stringRegExp[string, RegExp]): Exclude modules that match the given value from source map generation. -
filename(string): Defines the output filename of the SourceMap (will be inlined if no value is provided). -
append(string): Appends the given value to the original asset. Usually the#sourceMappingURLcomment.[url]is replaced with a URL to the source map file. Since webpack v4.36.0, path parameters are supported:[chunk],[filename]and[contenthash]. Settingappendtofalsedisables the appending. -
moduleFilenameTemplate(string): Seeoutput.devtoolModuleFilenameTemplate. -
fallbackModuleFilenameTemplate(string): See link above. -
namespace(string): Seeoutput.devtoolNamespace. -
module = true(boolean): Indicates whether loaders should generate source maps. -
columns = true(boolean): Indicates whether column mappings should be used. -
noSources = false(boolean): Prevents the source file content from being included in the source map. -
publicPath(string): Emits absolute URLs with public path prefix, e.g.https://example.com/project/. -
fileContext(string): Makes the[file]argument relative to this directory. -
sourceRoot(string): Provide a custom value for thesourceRootproperty in the SourceMap.
The fileContext option is useful when you want to store source maps in an upper level directory to avoid ../../ appearing in the absolute [url].
Examples
The following examples demonstrate some common use cases for this plugin.
Basic Use Case
You can use the following code to replace the configuration option devtool: inline-source-map with an equivalent custom plugin configuration:
module.exports = {
// ...
devtool: false,
plugins: [new webpack.SourceMapDevToolPlugin({})],
};
Exclude Vendor Maps
The following code would exclude source maps for any modules in the vendor.js bundle:
new webpack.SourceMapDevToolPlugin({
filename: '[name].js.map',
exclude: ['vendor.js'],
});
Host Source Maps Externally
Set a URL for source maps. Useful for hosting them on a host that requires authorization.
new webpack.SourceMapDevToolPlugin({
append: '\n//# sourceMappingURL=https://example.com/sourcemap/[url]',
filename: '[name].map',
});
And for cases when source maps are stored in the upper level directory:
project
|- dist
|- public
|- bundle-[hash].js
|- sourcemaps
|- bundle-[hash].js.map
With next config:
new webpack.SourceMapDevToolPlugin({
filename: 'sourcemaps/[file].map',
publicPath: 'https://example.com/project/',
fileContext: 'public',
});
Will produce the following URL:
https://example.com/project/sourcemaps/bundle-[hash].js.map
Further Reading
© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/plugins/source-map-dev-tool-plugin