From cc4b6d81fe7f7ee42bbdcbd81595b12c2c5c29fe Mon Sep 17 00:00:00 2001 From: Brian Mulhall Date: Sat, 14 Mar 2020 12:42:20 -0500 Subject: [PATCH] updating the minifier-js to modernize it's syntax and review and update it configuration. The configuration options and settings are also documented as to why and how they are being set so future developers will understand why it is configured as it is --- packages/minifier-js/minifier.js | 34 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/minifier-js/minifier.js b/packages/minifier-js/minifier.js index 87d74c1687..e024231068 100644 --- a/packages/minifier-js/minifier.js +++ b/packages/minifier-js/minifier.js @@ -1,17 +1,17 @@ -var terser; +const terser = Npm.require("terser"); meteorJsMinify = function (source) { - var result = {}; - var NODE_ENV = process.env.NODE_ENV || "development"; - - terser = terser || Npm.require("terser"); + const result = {}; + const NODE_ENV = process.env.NODE_ENV || "development"; try { - var terserResult = terser.minify(source, { + const options = { compress: { - drop_debugger: false, - unused: false, - dead_code: true, + passes: 1, // default is 1 (2 or more passes could lead to better compression) + inline: true, // default is true which will aggresively inline functions + drop_debugger: false, // remove debugger; statements + unused: false, // drop unreferenced functions and variables + dead_code: true, // remove unreachable code global_defs: { "process.env.NODE_ENV": NODE_ENV } @@ -19,24 +19,26 @@ meteorJsMinify = function (source) { // Fix issue #9866, as explained in this comment: // https://github.com/mishoo/UglifyJS2/issues/1753#issuecomment-324814782 // And fix terser issue #117: https://github.com/terser-js/terser/issues/117 - safari10: true, - }); + safari10: true, // set this option to true to work around the Safari 10/11 await bug + }; + + const terserResult = terser.minify(source, options); if (typeof terserResult.code === "string") { result.code = terserResult.code; result.minifier = 'terser'; } else { - throw terserResult.error || - new Error("unknown terser.minify failure"); + throw terserResult.error || new Error("unknown terser.minify failure"); } - } catch (e) { + } catch (err) { // Although Babel.minify can handle a wider variety of ECMAScript - // 2015+ syntax, it is substantially slower than UglifyJS/terser, so + // 2015+ syntax, it is substantially slower than terser, so // we use it only as a fallback. - var options = Babel.getMinifierOptions({ + const options = Babel.getMinifierOptions({ inlineNodeEnv: NODE_ENV }); + result.code = Babel.minify(source, options).code; result.minifier = 'babel-minify'; }