esbuild-obfuscator-plugin
Version:
JavaScript obfuscator plugin for esbuild
110 lines (108 loc) • 4.29 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var esbuild_obfuscator_plugin_exports = {};
__export(esbuild_obfuscator_plugin_exports, {
default: () => ObfuscatorPlugin
});
module.exports = __toCommonJS(esbuild_obfuscator_plugin_exports);
var import_node_fs = require("fs");
var import_esbuild = require("esbuild");
var import_javascript_obfuscator = __toESM(require("javascript-obfuscator"), 1);
var import_micromatch = __toESM(require("micromatch"), 1);
function ObfuscatorPlugin(options) {
const { obfuscateOutput = false, filter = [], ...obfuscateOptions } = options;
return {
name: "obfuscator",
setup: (build) => {
function shouldObfuscate(path) {
if (typeof filter === "function") {
return filter(path);
} else if (Array.isArray(filter) && filter.length > 0) {
return isMatch(path, filter);
}
return true;
}
if (obfuscateOutput) {
build.initialOptions.write = false;
return build.onEnd(async ({ outputFiles, errors }) => {
if (!errors.length && outputFiles?.length) {
for (const output of outputFiles) {
if (!shouldObfuscate(output.path)) return;
const obfuscatedCode = import_javascript_obfuscator.default.obfuscate(
output.text,
obfuscateOptions
).getObfuscatedCode();
await import_node_fs.promises.writeFile(output.path, obfuscatedCode);
}
}
});
}
build.onLoad(
{
filter: /\.([cm]?[jt]sx?)$/i
},
async (args) => {
if (!shouldObfuscate(args.path)) return;
const extension = args.path.split(".").pop()?.match(/([jt]sx?)$/i);
if (!extension) {
throw new Error(`Could not determine loader for ${args.path}`);
}
const loader = extension[1]?.toLowerCase();
const toInject = [];
if (options.inject) {
for (const path of Object.keys(options.inject)) {
if (isMatch(args.path, path) && options.inject[path]) {
toInject.push(
typeof options.inject[path] === "string" ? options.inject[path] : options.inject[path].join("\n")
);
}
}
}
const result = await (0, import_esbuild.transform)(await import_node_fs.promises.readFile(args.path, "utf8"), {
loader,
banner: toInject.join("\n"),
sourcemap: false
});
const obfuscatedCode = import_javascript_obfuscator.default.obfuscate(
result.code,
options
).getObfuscatedCode();
return {
contents: obfuscatedCode,
loader
};
}
);
}
};
}
function isMatch(path, pattern) {
return import_micromatch.default.isMatch(path, pattern, { basename: true });
}