72 lines
3.6 KiB
JavaScript
72 lines
3.6 KiB
JavaScript
module.exports = {
|
||
productionSourceMap: false,
|
||
configureWebpack: config => {
|
||
console.log('开始尝试配置极限压缩');
|
||
|
||
// 确保minimizer数组存在
|
||
if (config.optimization && config.optimization.minimizer && Array.isArray(config.optimization.minimizer)) {
|
||
// 查找TerserWebpackPlugin实例,而不是依赖索引
|
||
const terserPluginIndex = config.optimization.minimizer.findIndex(plugin => {
|
||
return plugin.constructor && plugin.constructor.name === 'TerserWebpackPlugin' ||
|
||
(plugin.options && plugin.options.terserOptions);
|
||
});
|
||
|
||
if (terserPluginIndex !== -1) {
|
||
// 确保options对象存在
|
||
if (!config.optimization.minimizer[terserPluginIndex].options) {
|
||
config.optimization.minimizer[terserPluginIndex].options = {};
|
||
}
|
||
|
||
// 设置terserOptions - 极限压缩配置
|
||
config.optimization.minimizer[terserPluginIndex].options.terserOptions = {
|
||
compress: {
|
||
drop_console: true, // 去除所有console语句
|
||
drop_debugger: true, // 去除debugger语句
|
||
dead_code: true, // 移除死代码
|
||
unused: true, // 移除未使用的变量
|
||
toplevel: true, // 清理顶层作用域未使用的变量
|
||
passes: 5, // 增加压缩次数以获得更好的压缩效果
|
||
sequences: true, // 合并连续的变量声明
|
||
evaluate: true, // 提前计算常量表达式
|
||
reduce_vars: true, // 合并或移除变量
|
||
join_vars: true, // 合并变量声明
|
||
conditionals: true, // 优化条件表达式
|
||
comparisons: true, // 优化比较操作
|
||
booleans: true, // 优化布尔表达式
|
||
typeofs: true, // 优化typeof操作
|
||
collapse_vars: true, // 折叠定义后不再修改的变量
|
||
reduce_funcs: true, // 合并或移除未使用的函数
|
||
inline: true, // 内联简单函数
|
||
side_effects: true, // 移除有副作用的代码
|
||
keep_fargs: false, // 移除未使用的函数参数
|
||
keep_fnames: false, // 不保留函数名称
|
||
pure_funcs: ['console.log', 'console.warn', 'console.error', 'console.info'], // 标记为纯函数的console方法
|
||
pure_getters: true // 假设getter函数没有副作用
|
||
},
|
||
format: {
|
||
ascii_only: true, // 确保输出ASCII字符
|
||
comments: false, // 去除所有注释
|
||
beautify: false, // 不美化输出
|
||
ecma: 5, // 输出兼容ECMAScript 5
|
||
wrap_func_args: false, // 不包裹函数参数
|
||
bracketize: true, // 使用大括号包裹所有块
|
||
quote_style: 1 // 使用单引号
|
||
},
|
||
parse: {
|
||
ecma: 8, // 使用ECMAScript 8解析
|
||
bare_returns: true // 允许顶级return语句
|
||
},
|
||
mangle: {
|
||
toplevel: true, // 混淆顶层作用域的变量名
|
||
keep_fnames: false, // 不保留函数名称
|
||
keep_classnames: false // 不保留类名称
|
||
},
|
||
module: false, // 不是ES模块
|
||
sourceMap: false // 不生成source map
|
||
};
|
||
|
||
console.log('配置极限压缩完成');
|
||
}
|
||
}
|
||
}
|
||
}; |