42 lines
1022 B
JavaScript
42 lines
1022 B
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const ClosureCompiler = require('google-closure-compiler').compiler;
|
|
|
|
/**
|
|
* Minify a javascript file
|
|
*
|
|
* @param {string} inputFile
|
|
* @param {string} outputFile
|
|
* @param {object} options
|
|
*/
|
|
module.exports = function minify(inFile, outFile, options) {
|
|
const defaultOptions = {
|
|
js: inFile,
|
|
js_output_file: outFile,
|
|
compilation_level: 'ADVANCED',
|
|
language_in: 'ECMASCRIPT5',
|
|
language_out: 'ECMASCRIPT5',
|
|
warning_level: 'VERBOSE',
|
|
create_source_map: `${outFile}.map`,
|
|
rewrite_polyfills: false
|
|
};
|
|
|
|
options = options || {};
|
|
Object.keys(options).forEach(opt => {
|
|
defaultOptions[opt] = options[opt];
|
|
});
|
|
|
|
const compiler = new ClosureCompiler(defaultOptions);
|
|
|
|
compiler.run((exitCode, stdOut, stdErr) => {
|
|
if (stdErr) {
|
|
console.error(stdErr);
|
|
} else {
|
|
console.log(stdOut);
|
|
}
|
|
|
|
// Append the source map maping line to the minified file
|
|
fs.appendFileSync(outFile, '//# sourceMappingURL=' + path.basename(outFile) + '.map');
|
|
});
|
|
};
|