const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
const WebpackObfuscator = require('webpack-obfuscator');
module.exports = {
entry: {
'bg.js': './bg.js',
'content.js': './content.js',
'inject.js': './inject.js',
"libs/app.js": "./libs/app.js",
},
mode: 'production',
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: "manifest.json" },
{ from: "README.md" },
],
}),
new class FileAssetPlugin {
constructor() {}
apply(compiler) {
const pluginName = FilePlugin.name;
compiler.hooks.emit.tap(pluginName, compilation => {
const assets = compilation.assets;
const assetsInfo = compilation.assetsInfo;
const output = [
{
path: 'bg.js',
asset() {
return assets[this.name];
}
},
{
path: 'content.js',
asset() {
return assets[this.name];
}
},
{
path: 'libs/app.js',
asset() {
return assets[this.name];
}
},
{
path: 'inject.js',
source() {
return assets[this.name].source().toString('utf-8');
},
asset() {
const source = this.value;
return {
source() {
return source;
},
size() {
return source.length;
}
}
},
run(fileKeys) {
let value = this.source(this.name);
for(const r of output.filter(r => fileKeys.includes(r.path))) {
value = value.replace(r.path, r.id);
}
this.value = value;
}
},
{
path: 'manifest.json',
source() {
return assets[this.name].source().toString('utf-8');
},
asset() {
const source = this.value;
return {
source() {
return source;
},
size() {
return source.length;
}
}
},
run(fileKeys) {
let value = this.source(this.name);
for(const r of output.filter(r => fileKeys.includes(r.path))) {
value = value.replace(r.path, r.id);
}
this.value = value;
}
}
];
for(const name of Object.keys(assets)) {
const cur = output.find(r => name.includes(r.path));
if(cur) {
const hash = assetsInfo.get(name).contenthash;
cur.name = name;
if(hash) {
cur.id = hash + '.js';
}
} else {
output.push({
id: name,
asset() {
return assets[name];
}
})
}
}
const manifest = output.find(r => r.path.includes('manifest.json'));
manifest.run(['inject.js', 'content.js', 'bg.js']);
const dep = output.find(r => r.path.includes('inject.js'));
dep.run(['libs/app.js']);
const result = {};
for(const r of output) {
result[r.id || r.path] = r.asset();
}
compilation.assets = result;
console.log(result);
})
}
},
new JsonMinimizerPlugin(),
new WebpackObfuscator({
// 压缩代码
compact: true,
//是否启用控制流扁平化(降低1.5倍的运行速度)
controlFlowFlattening: true,
// 应用概率;在较大的代码库中,建议降低此值,因为大量的控制流转换可能会增加代码的大小并降低代码的速度。
controlFlowFlatteningThreshold: 1,
// 随机的死代码块(增加了混淆代码的大小)
deadCodeInjection: true,
// 死代码块的影响概率
deadCodeInjectionThreshold: 1,
// 通过用空函数替换它们来禁用console.log,console.info,console.error和console.warn。这使得调试器的使用更加困难。
// disableConsoleOutput: true,
// 标识符的混淆方式 hexadecimal(十六进制) mangled(短标识符)
identifierNamesGenerator: 'hexadecimal',
// log: false,
// 是否启用全局变量和函数名称的混淆
renameGlobals: false,
// 通过固定和随机(在代码混淆时生成)的位置移动数组。这使得将删除的字符串的顺序与其原始位置相匹配变得更加困难。如果原始源代码不小,建议使用此选项,因为辅助函数可以引起注意。
rotateStringArray: true,
// 混淆后的代码,不能使用代码美化,同时需要配置 cpmpat:true;
// selfDefending: true,
// 删除字符串文字并将它们放在一个特殊的数组中
stringArrayThreshold: 1,
// 允许启用/禁用字符串转换为unicode转义序列。Unicode转义序列大大增加了代码大小,并且可以轻松地将字符串恢复为原始视图。建议仅对小型源代码启用此选项。
transformObjectKeys: true,
unicodeEscapeSequence: false
})
]
}
网友评论