webpack-obfuscator排除规则失效问题求助
问题分析与解决方案
你的配置存在两个核心问题,导致排除规则不生效:
- 同时启用了
WebpackObfuscator插件和loader,二者功能重复且冲突,插件的排除逻辑不适用于你的源文件路径匹配需求 - 插件的排除参数是针对打包后的输出文件名,而非源文件目录,所以
['*node_modules*','*src*','*dist*']这种路径匹配完全无效
正确配置(满足仅排除node_modules、混淆src代码的需求)
修改webpack.config.js,移除冗余的插件配置,保留loader配置即可——因为loader已经通过exclude规则精准排除了node_modules目录:
const path = require('path'); var WebpackObfuscator = require('webpack-obfuscator'); module.exports = { mode: 'production', entry: { index: './src/index.js', }, output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist'), publicPath: '', sourceMapFilename: "[name].js.map" }, devtool: "source-map", optimization: { minimize: false, }, module: { rules: [ { test: /\.(sass|css)$/, use: [ 'style-loader', 'css-loader', ] }, { test: /\.js$/, // 排除node_modules目录,仅处理src下的JS文件 exclude: path.resolve(__dirname, 'node_modules'), enforce: 'post', use: { loader: WebpackObfuscator.loader, options: { rotateStringArray: true } } } ] } };
配置说明
enforce: 'post'确保loader在其他JS loader(如babel-loader,如果有的话)之后执行,避免混淆未转译的代码exclude: path.resolve(__dirname, 'node_modules')直接排除node_modules下的所有JS文件,这些文件不会被混淆- 移除插件是因为插件会处理整个打包后的bundle文件,无法精准区分源文件是否来自node_modules,反而会导致重复混淆或排除规则失效
内容的提问来源于stack exchange,提问作者else42.de
相关产品推荐
相关产品推荐

