Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 2x 34x 34x 34x 33x 33x 32x | import path from "node:path";
import { minify } from "terser";
const identifierPattern = /[$_\p{ID_Start}](?:[$_\p{ID_Continue}]|\u200C|\u200D)*/gu;
export interface TransformOptions {
filePath: string;
minify: boolean;
nameCache: object;
pattern: RegExp;
reserved: readonly string[];
source: string;
}
export async function transformJavaScript(options: TransformOptions): Promise<string> {
const { filePath, nameCache, pattern, reserved, source } = options;
const shouldMinify = options.minify;
const result = await minify(
{
[path.basename(filePath)]: source,
},
{
compress: shouldMinify,
format: {
beautify: !shouldMinify,
comments: "some",
preserve_annotations: true,
shebang: true,
},
mangle: {
...(shouldMinify ? undefined : { reserved: collectIdentifiers(source) }),
properties: {
regex: pattern,
reserved: [...reserved],
},
toplevel: shouldMinify,
},
module: filePath.toLowerCase().endsWith(".mjs"),
nameCache,
toplevel: shouldMinify,
},
);
const code = result.code as string;
return `${code}\n`;
}
function collectIdentifiers(source: string): string[] {
return [...new Set(source.match(identifierPattern) ?? [])];
}
|