From 9f9672a5241dea10e2cbcc0626e0bb22405f01df Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sun, 3 Mar 2019 23:10:05 -0500 Subject: [PATCH 1/5] Add compressor options This allows specifying Terser options in mangle.json or package.json's "mangle" field. --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 13bc4af1..2c244771 100644 --- a/src/index.js +++ b/src/index.js @@ -400,6 +400,9 @@ function createConfig(options, entry, format, writeMeta) { nameCache = JSON.parse( fs.readFileSync(resolve(options.cwd, 'mangle.json'), 'utf8'), ); + if (nameCache.config) { + mangleOptions = Object.assign({}, mangleOptions || {}, nameCache.config); + } } catch (e) {} } loadNameCache(); @@ -539,16 +542,16 @@ function createConfig(options, entry, format, writeMeta) { terser({ sourcemap: true, output: { comments: false }, - compress: { + compress: Object.assign({ keep_infinity: true, pure_getters: true, global_defs: defines, passes: 10, - }, + }, mangleOptions.compress || {}), warnings: true, ecma: 5, toplevel: format === 'cjs' || format === 'es', - mangle: { + mangle: Object.assign({ properties: mangleOptions ? { regex: mangleOptions.regex @@ -557,7 +560,7 @@ function createConfig(options, entry, format, writeMeta) { reserved: mangleOptions.reserved || [], } : false, - }, + }, mangleOptions.mangle || {}), nameCache, }), mangleOptions && { From 8842f3eee11e6a8c48c70d0e17d1779808939be7 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 4 Mar 2019 09:07:25 -0500 Subject: [PATCH 2/5] Implement new minify config format --- src/index.js | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 2c244771..9dea4256 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,29 @@ const toTerserLiteral = (value, name) => { // default: behaviour from Terser (@prefix=1 produces expression/literal, unprefixed=1 produces string literal): }; +// Normalize Terser options from microbundle's relaxed JSON format (mutates argument in-place) +function normalizeMinifyOptions(minifyOptions) { + const mangle = minifyOptions.mangle || (minifyOptions.mangle = {}); + let properties = mangle.properties; + + // allow top-level "properties" key to override mangle.properties (including {properties:false}): + if (minifyOptions.properties != null) { + properties = mangle.properties = minifyOptions.properties && Object.assign(properties, minifyOptions.properties); + } + + // allow previous format ({ mangle:{regex:'^_',reserved:[]} }): + if (minifyOptions.regex || minifyOptions.reserved) { + if (!properties) properties = mangle.properties = {}; + properties.regex = properties.regex || minifyOptions.regex; + properties.reserved = properties.reserved || minifyOptions.reserved; + } + + if (properties) { + if (properties.regex) properties.regex = new RegExp(properties.regex); + properties.reserved = [].concat(properties.reserved || []); + } +} + // Parses values of the form "$=jQuery,React=react" into key-value object pairs. const parseMappingArgument = (globalStrings, processValue) => { const globals = {}; @@ -387,7 +410,8 @@ function createConfig(options, entry, format, writeMeta) { // let rollupName = safeVariableName(basename(entry).replace(/\.js$/, '')); let nameCache = {}; - let mangleOptions = options.pkg.mangle || false; + // Support "minify" field and legacy "mangle" field via package.json: + let minifyOptions = options.pkg.minify || options.pkg.mangle || false; const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx'; @@ -400,13 +424,16 @@ function createConfig(options, entry, format, writeMeta) { nameCache = JSON.parse( fs.readFileSync(resolve(options.cwd, 'mangle.json'), 'utf8'), ); - if (nameCache.config) { - mangleOptions = Object.assign({}, mangleOptions || {}, nameCache.config); + // mangle.json can contain a "minify" field, same format as the pkg.mangle: + if (nameCache.minify) { + minifyOptions = Object.assign({}, minifyOptions || {}, nameCache.minify); } } catch (e) {} } loadNameCache(); + normalizeMinifyOptions(minifyOptions); + let shebang; let config = { @@ -547,20 +574,11 @@ function createConfig(options, entry, format, writeMeta) { pure_getters: true, global_defs: defines, passes: 10, - }, mangleOptions.compress || {}), + }, minifyOptions.compress || {}), warnings: true, ecma: 5, toplevel: format === 'cjs' || format === 'es', - mangle: Object.assign({ - properties: mangleOptions - ? { - regex: mangleOptions.regex - ? new RegExp(mangleOptions.regex) - : null, - reserved: mangleOptions.reserved || [], - } - : false, - }, mangleOptions.mangle || {}), + mangle: Object.assign({}, minifyOptions.mangle || {}), nameCache, }), mangleOptions && { From d94a50e0c2d049ea54cb96e884154b3d173e6905 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 4 Mar 2019 09:20:51 -0500 Subject: [PATCH 3/5] Fix crasher --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 9dea4256..80d6234d 100644 --- a/src/index.js +++ b/src/index.js @@ -411,7 +411,7 @@ function createConfig(options, entry, format, writeMeta) { let nameCache = {}; // Support "minify" field and legacy "mangle" field via package.json: - let minifyOptions = options.pkg.minify || options.pkg.mangle || false; + let minifyOptions = options.pkg.minify || options.pkg.mangle || {}; const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx'; @@ -581,7 +581,7 @@ function createConfig(options, entry, format, writeMeta) { mangle: Object.assign({}, minifyOptions.mangle || {}), nameCache, }), - mangleOptions && { + nameCache && { // before hook options: loadNameCache, // after hook From 69b51655c5f957b6cae7d2d41612d1fddfdfc69e Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 4 Mar 2019 09:26:10 -0500 Subject: [PATCH 4/5] lint fixes --- src/index.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 80d6234d..f7b38b85 100644 --- a/src/index.js +++ b/src/index.js @@ -46,12 +46,14 @@ const toTerserLiteral = (value, name) => { // Normalize Terser options from microbundle's relaxed JSON format (mutates argument in-place) function normalizeMinifyOptions(minifyOptions) { - const mangle = minifyOptions.mangle || (minifyOptions.mangle = {}); + const mangle = minifyOptions.mangle || (minifyOptions.mangle = {}); let properties = mangle.properties; // allow top-level "properties" key to override mangle.properties (including {properties:false}): if (minifyOptions.properties != null) { - properties = mangle.properties = minifyOptions.properties && Object.assign(properties, minifyOptions.properties); + properties = mangle.properties = + minifyOptions.properties && + Object.assign(properties, minifyOptions.properties); } // allow previous format ({ mangle:{regex:'^_',reserved:[]} }): @@ -60,7 +62,7 @@ function normalizeMinifyOptions(minifyOptions) { properties.regex = properties.regex || minifyOptions.regex; properties.reserved = properties.reserved || minifyOptions.reserved; } - + if (properties) { if (properties.regex) properties.regex = new RegExp(properties.regex); properties.reserved = [].concat(properties.reserved || []); @@ -426,7 +428,11 @@ function createConfig(options, entry, format, writeMeta) { ); // mangle.json can contain a "minify" field, same format as the pkg.mangle: if (nameCache.minify) { - minifyOptions = Object.assign({}, minifyOptions || {}, nameCache.minify); + minifyOptions = Object.assign( + {}, + minifyOptions || {}, + nameCache.minify, + ); } } catch (e) {} } @@ -569,12 +575,15 @@ function createConfig(options, entry, format, writeMeta) { terser({ sourcemap: true, output: { comments: false }, - compress: Object.assign({ - keep_infinity: true, - pure_getters: true, - global_defs: defines, - passes: 10, - }, minifyOptions.compress || {}), + compress: Object.assign( + { + keep_infinity: true, + pure_getters: true, + global_defs: defines, + passes: 10, + }, + minifyOptions.compress || {}, + ), warnings: true, ecma: 5, toplevel: format === 'cjs' || format === 'es', From cf00d7dad1ab7de3678e56c41221affc91887b84 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 4 Mar 2019 18:29:37 -0500 Subject: [PATCH 5/5] fix mangle.json always being generated --- src/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.js b/src/index.js index f7b38b85..e319029e 100644 --- a/src/index.js +++ b/src/index.js @@ -412,6 +412,7 @@ function createConfig(options, entry, format, writeMeta) { // let rollupName = safeVariableName(basename(entry).replace(/\.js$/, '')); let nameCache = {}; + const bareNameCache = nameCache; // Support "minify" field and legacy "mangle" field via package.json: let minifyOptions = options.pkg.minify || options.pkg.mangle || {}; @@ -440,6 +441,8 @@ function createConfig(options, entry, format, writeMeta) { normalizeMinifyOptions(minifyOptions); + if (nameCache === bareNameCache) nameCache = null; + let shebang; let config = {