52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
var javascriptObfuscator = require("javascript-obfuscator");
|
|
var fs = require("node:fs");
|
|
var path = require("node:path");
|
|
function obfuscatorPlugin(obOptions) {
|
|
let { excludeChunks, options } = obOptions || {};
|
|
return {
|
|
name: "vite-plugin-javascript-obfuscator",
|
|
enforce: "post",
|
|
apply:
|
|
(obOptions === null || obOptions === void 0 ? void 0 : obOptions.apply) ||
|
|
(() => true),
|
|
async writeBundle(output_options, bundle) {
|
|
for (let output_file in bundle) {
|
|
if (bundle[output_file].type != "chunk") {
|
|
continue;
|
|
}
|
|
let chunk = bundle[output_file];
|
|
if (output_file.match(/\.(js|mjs|cjs)$/)) {
|
|
if (excludeChunks.indexOf(chunk.name) == -1) {
|
|
console.log(
|
|
`[briq-vite-obfuscator-plugin] Obfuscating chunk ${chunk.name} (${output_file}) ...`
|
|
);
|
|
let abs_path = path.join(output_options.dir, output_file);
|
|
const obfuscationResult = javascriptObfuscator.obfuscate(
|
|
chunk.code,
|
|
options
|
|
);
|
|
let code = obfuscationResult.getObfuscatedCode();
|
|
await new Promise((resolve, reject) => {
|
|
fs.writeFile(abs_path, code, (err) => {
|
|
if (err) {
|
|
reject(err);
|
|
return;
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
} else {
|
|
console.log(
|
|
`[briq-vite-obfuscator-plugin] Ignoring chunk ${chunk.name} (explicitly excluded)`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = obfuscatorPlugin;
|