Cannot exclude 3rd party files from node_modules when using options.<test|include|exclude>
- Operating System: Windows 10
- Node Version: v12.18.3
- NPM Version: 6.14.6
- webpack Version: 5.4.0
- terser-webpack-plugin Version: 5.0.3
Expected Behavior
When I wrote webpack.config.js as follows, all the 3rd party files from node_modules using options.exclude should be useful.
module.exports = {
entry: "./index.js",
optimization: {
minimizer: [
new TerserPlugin({
exclude: /node_modules/
})
]
}
}
Actual Behavior
No file was actually been excluded.
How Do We Reproduce?
Using the following files can reproduce this issue.
// webpack.config.js
const CopyWebpackPlugin = require("copy-webpack-plugin");
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
entry: "./index.js",
optimization: {
minimizer: [
new TerserPlugin({
exclude: /node_modules/
})
]
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: require.resolve("jquery/dist/jquery.min.js"),
to: "jquery"
}
]
})
],
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist"),
}
};
package.json is
{
"name": "static-test",
"version": "1.0.0",
"description": "static files",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"dependencies": {
"jquery": "3.5.1"
},
"devDependencies": {
"copy-webpack-plugin": "^6.3.0",
"webpack": "^5.4.0",
"webpack-cli": "^4.1.0"
}
}
index.js is
console.log("Hello!");
I find the asset has sourceFileName property when added by copy-webpack-plugin, the property name only display the output file type [name].js, not the original file name.
async optimize(compiler, compilation, assets, optimizeOptions) {
const cache = compilation.getCache('TerserWebpackPlugin');
let numberOfAssetsForMinify = 0;
const assetsForMinify = await Promise.all(Object.keys(assets).filter(name => {
const {
info
} = compilation.getAsset(name); // Skip double minimize assets from child compilation
if (info.minimized) {
return false;
}
if (!compiler.webpack.ModuleFilenameHelpers.matchObject.bind( // eslint-disable-next-line no-undefined
undefined, this.options)(name)) {
return false;
}
Yes, because we are working on assets level, it is impossible to only module, if you have problems with minification and want to exclude module(s):
- if you have broken code after minification, please open an issue in
terserrepo - if you have problem with code inside module, please open an issue in module repo
- if you still need to exclude some module(s), you can move them in separate chunk, using
splitChunksand exclude it
Why we do minification on whole files, because it has maximum efficiently
What do you want exclude? Please avoid describing code, it is not help, tell us about the problem you encountered and I can tell you how to solve it
Hi, I have the same problem.
I have all vendor modules in a chunk named "defaultVendors" and would like to exclude this chunk from obfuscation.
new TerserPlugin({
exclude: "defaultVendors"
})
The above does not work, what would be the correct way to exclude a chunk? There used to be a chunkFilter property, but that's no longer available in the latest versions.
new TerserPlugin({ exclude: /.*vendors.js$/ }), please look which assets webpack output ad exclude them
It clicked, terser operates on emitted assets. Works perfectly now, thank you!
I just want to copy the original 3rd party modules to the dist folder, some of them have already minimized, we use them as systemjs import source in runtime environment, so I don't want terser plugin to minimize or modify them again.
@ericclemmons