Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env node
"use strict";
var opts = {
"mangle": {
"properties": true,
"toplevel": true
},
nameCache: { vars: {}, props: {} }
};
var terser = require('terser');
var hasOptionUndeclared = require('terser/package').version >= '4.1.1';
if (hasOptionUndeclared) {
opts.mangle.properties = {
"undeclared": true
};
}
terser.minify('var Bar = {};', opts);
var result = terser.minify('var Foo = { foo: function() { return Bar.bar() } };', opts);
var expected = `var r={o:function(){return a.${hasOptionUndeclared ? "t" : "v"}()}};`;
console.log("[task] test uglifying external properties",
"\n[Current ]", result.code, "\n[Expected]", expected);
if (result.code === expected) {
console.log(">>> same");
} else {
// https://github.com/terser-js/terser/issues/397
console.log(">>> different, so", hasOptionUndeclared ? "please check test code" : "recommend a terser <= v3.14.0");
}
p.regex = ToRegExp(p.regex);
}
if (m && typeof m.keep_fnames === "string") {
m.keep_fnames = ToRegExp(m.keep_fnames);
}
else if (m && !m.keep_fnames) {
m.keep_fnames = c.keep_fnames;
}
var comments = o.comments;
if (comments && typeof comments === "string") {
o.comments = ToRegExp(comments);
}
let ver = "", terser = null;
try {
// @ts-ignore
ver = require("terser/package").version;
} catch (e) {
console.log("Can not read the version of terser.");
}
try {
// @ts-ignore
terser = require("terser");
} catch (e) {
console.log("Can not read the module of terser.");
}
if (ver) {
var hasOptionUndeclared = ver >= '4.1.2', has_wrap_func_args = ver >= '4.3';
if (m) {
if (hasOptionUndeclared) {
if (p.undeclared == null) {
p.undeclared = true;
}
map = JSON.parse(map);
// make source map sources relative to output
map.sources = map.sources.map(source => {
// webpack:///webpack:/// happens too for some reason
while (source.startsWith('webpack:///'))
source = source.substr(11);
if (source.startsWith('./'))
source = source.substr(2);
if (source.startsWith('webpack/'))
return '/webpack/' + source.substr(8);
return sourceMapBasePrefix + source;
});
}
if (minify) {
const result = terser.minify(code, {
compress: false,
mangle: {
keep_classnames: true,
keep_fnames: true
},
sourceMap: sourceMap ? {
content: map,
filename,
url: `${filename}.map`
} : false
});
// For some reason, auth0 returns "undefined"!
// custom terser phase used over Webpack integration for this reason
if (result.code !== undefined)
({ code, map } = { code: result.code, map: result.map });
}
AJSDCE: AJSDCE,
applyImportAndExportNameChanges: applyImportAndExportNameChanges,
emitDCEGraph: emitDCEGraph,
applyDCEGraphRemovals: applyDCEGraphRemovals,
minifyWhitespace: function() { minifyWhitespace = true },
noPrint: function() { noPrint = true },
dump: function() { dump(ast) },
growableHeap: growableHeap,
};
passes.forEach(function(pass) {
registry[pass](ast);
});
if (!noPrint) {
var terserAst = terser.AST_Node.from_mozilla_ast(ast);
var output = terserAst.print_to_string({
beautify: !minifyWhitespace,
indent_level: minifyWhitespace ? 0 : 1,
keep_quoted_props: true, // for closure
});
print(output);
}
booleans: false,
inline: 0,
keep_fargs: false,
hoist_props: false,
loops: false,
reduce_funcs: false,
unsafe: true,
unsafe_math: true,
}),
};
const { output } = await bundle.generate({
format: "iife",
});
const minJs = terser.minify(output[0].code, uglifyOpts).code;
var css = fs.readFileSync(__dirname + '/bootstrap-reboot.css', 'utf8').replace(/\/\*[\s\S]+?\*\/\s*/gm, '');
css += fs.readFileSync(__dirname + '/style.css', 'utf8');
const minCss = new CleanCSS({level: 2}).minify(css).styles;
const html = [
'',
'',
'',
'',
'',
'<title>Interactive Results</title>',
'',
'<style>',
minCss,</style>
['esm', 'cjs', 'umd'].forEach(suf => {
const file = fs.readFileSync(`dist/${lib}/${lib}.${suf}.js`, 'utf8');
const fileSM = fs.readFileSync(`dist/${lib}/${lib}.${suf}.js.map`, 'utf8');
const result = terser.minify(file, {
module: suf !== 'umd',
sourceMap: {
content: fileSM,
filename: `${lib}.${suf}.min.js`,
url: `${lib}.${suf}.min.js.map`,
},
compress: { unsafe: true, passes: 2, warnings: true, pure_funcs: ['Object.freeze'] },
mangle: { properties: { regex: /^_(?!_)(?!internal)/ } }
});
const target = `dist/${lib}/${lib}.${suf}.min.js`;
const targetSM = `dist/${lib}/${lib}.${suf}.min.js.map`;
fs.writeFileSync(target, result.code, 'utf8');
fs.writeFileSync(targetSM, result.map, 'utf8');
console.log('-', target);
});
});
minify(code) {
const { dev, dropConsole } = getConfigOptions();
let buildCode = code;
if (!dev) {
const result = terser.minify(buildCode, {
warnings: false,
compress: {
drop_console: dropConsole,
},
output: {
comments: false,
},
ie8: true,
safari10: true,
});
if (result.error) console.log(result.error);
buildCode = result.code;
}
fse.writeFileSync(this.savePath, buildCode);
}
}
async function compile(mode) {
await fs.mkdirs("./dist/");
await fs.saferRemove([distJs]);
await browserifyFiles(srcFile, distJs);
const distStat = await fs.stat(distJs);
console.log(`Browserified ${distJs} - ${distStat.size / 1000}kb`);
var content = await fs.readFile(distJs, "utf8");
const minContent = Terser.minify(content);
await fs.writeFile(distMin, minContent.code, "utf8");
const minStat = await fs.stat(distMin);
console.log(`Minified ${distMin} - ${minStat.size / 1000}kb`);
}
booleans: false,
inline: 0,
keep_fargs: false,
hoist_props: false,
loops: false,
reduce_funcs: false,
unsafe: true,
unsafe_math: true,
}),
};
const { output } = await bundle.generate({
format: "iife",
});
const minJs = terser.minify(output[0].code, uglifyOpts).code;
var css = fs.readFileSync(__dirname + '/bootstrap-reboot.css', 'utf8').replace(/\/\*[\s\S]+?\*\/\s*/gm, '');
css += fs.readFileSync(__dirname + '/style.css', 'utf8');
const minCss = new CleanCSS({level: 2}).minify(css).styles;
const html = [
'',
'',
'',
'',
'',
'<title>Interactive Results</title>',
'',
'<style>',
minCss,</style>
function minifyJS(content) {
return terser(content, terserOptions).code
}