forked from systemjs/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
terser.js
39 lines (35 loc) · 1.72 KB
/
terser.js
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
Chomp.addExtension('[email protected]:npm');
Chomp.registerTemplate('terser', function (task) {
if (task.engine || task.run)
throw new Error('"engine", "run" not configurable for Terser template.');
const opts = task.templateOptions;
const preamble = opts.output?.preamble;
const pjsonVersion = typeof preamble === 'string' && preamble.includes('#PJSON_VERSION');
const { autoInstall } = opts;
const targetName = task.targets[0].split('/').pop();
if (opts.sourceMap === true)
opts.sourceMap = { filename: targetName, url: '$TARGET', includeSources: true };
const optionsStr = JSON.stringify(opts, null, 2).replace(/\n/g, '\n ').replace('"$TARGET"', '`${basename(process.env.TARGET)}.map`');
return [{
name: task.name,
targets: task.targets,
deps: [...task.deps, ...pjsonVersion ? ['package.json'] : [], ...ENV.CHOMP_EJECT ? ['npm:install'] : ['node_modules/terser']],
engine: 'node',
run: ` import { readFileSync, writeFileSync } from 'fs';
import { basename } from 'path';
import { minify } from 'terser';
${pjsonVersion ? ` const pjson = JSON.parse(readFileSync('package.json', 'utf8'));` : ''}
const { code, map } = await minify({ [basename(process.env.DEP)]: readFileSync(process.env.DEP, 'utf8') }, ${
pjsonVersion ? optionsStr.replace('"preamble": ' + JSON.stringify(preamble), '"preamble": `' + preamble.replace(/(\`|\${)/, '\\$1').replace('#PJSON_VERSION', '${pjson.version}') + '`') : optionsStr
});
writeFileSync(process.env.TARGET, code + "\\n");
${opts.sourceMap ? ' writeFileSync(`${process.env.TARGET}.map`, map);\n' : ''}`
}, ...ENV.CHOMP_EJECT ? [] : [{
template: 'npm',
templateOptions: {
autoInstall,
packages: ['terser@5'],
dev: true
}
}]];
});