Files
upstream-univer/common/shared/tsdown/plugins/output-obfuscator.ts
upstream-mirror bcf6ef63ad
Some checks failed
📦 Build / build (push) Has been cancelled
🧪 CI Test / test shard (1/4) (push) Has been cancelled
🧪 CI Test / test shard (2/4) (push) Has been cancelled
🧪 CI Test / test shard (3/4) (push) Has been cancelled
🧪 CI Test / test shard (4/4) (push) Has been cancelled
🧪 CI Test / test (push) Has been cancelled
🧪 CI Test / eslint (push) Has been cancelled
🧪 CI Test / typecheck (push) Has been cancelled
🔫 Sync to Pro / dispatch-sync (push) Has been cancelled
🔮 Sync Mirror / sync (push) Has been cancelled
snapshot of https://github.com/dream-num/univer.git (dev, 2026-09-16; blobs > 131072B omitted)
2026-09-16 05:09:45 +05:00

43 lines
1.4 KiB
TypeScript

import type { ObfuscatorOptions } from 'javascript-obfuscator';
import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import JavaScriptObfuscator from 'javascript-obfuscator';
interface IBundleOutput {
fileName?: string;
}
export function createOutputObfuscatorPlugin(ignorePatterns?: RegExp[]) {
const obfuscationOptions: ObfuscatorOptions = {
ignoreImports: true,
};
return {
name: 'create-output-obfuscator',
writeBundle(outputOptions: { dir?: string }, bundle: Record<string, IBundleOutput>) {
if (!outputOptions.dir) {
return;
}
const outDir = outputOptions.dir;
for (const output of Object.values(bundle)) {
const fileName = output.fileName;
if (!fileName || !fileName.endsWith('.js') || fileName.endsWith('.map')) {
continue;
}
if (ignorePatterns?.some((pattern) => pattern.test(fileName))) {
continue;
}
const targetPath = path.resolve(outDir, fileName);
const sourceCode = readFileSync(targetPath, 'utf8');
const obfuscatedCode = JavaScriptObfuscator.obfuscate(sourceCode, obfuscationOptions).getObfuscatedCode();
writeFileSync(targetPath, obfuscatedCode, 'utf8');
}
},
};
}