diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-06-29 12:48:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-29 12:48:34 +0800 |
commit | bdeadffbf582b393dbc14a45b3e69ddf16f47690 (patch) | |
tree | 44575bdd928b43ddcdc630f2cd898786dbbd3172 /lib/minify.js | |
parent | 5e6f26445f932a180890be4792dab574e07cbb0f (diff) | |
download | tracifyjs-bdeadffbf582b393dbc14a45b3e69ddf16f47690.tar.gz tracifyjs-bdeadffbf582b393dbc14a45b3e69ddf16f47690.zip |
improve usability of name cache under `minify()` (#2176)
fixes #2174
Diffstat (limited to 'lib/minify.js')
-rw-r--r-- | lib/minify.js | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/minify.js b/lib/minify.js index cc638be3..b4bfe451 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -27,6 +27,23 @@ function set_shorthand(name, options, keys) { } } +function init_cache(cache) { + if (!cache) return; + if (!("cname" in cache)) cache.cname = -1; + if (!("props" in cache)) { + cache.props = new Dictionary(); + } else if (!(cache.props instanceof Dictionary)) { + cache.props = Dictionary.fromObject(cache.props); + } +} + +function to_json(cache) { + return { + cname: cache.cname, + props: cache.props.toObject() + }; +} + function minify(files, options) { var warn_function = AST_Node.warn_function; try { @@ -35,6 +52,7 @@ function minify(files, options) { ie8: false, keep_fnames: false, mangle: {}, + nameCache: null, output: {}, parse: {}, sourceMap: false, @@ -52,7 +70,7 @@ function minify(files, options) { set_shorthand("warnings", options, [ "compress" ]); if (options.mangle) { options.mangle = defaults(options.mangle, { - cache: null, + cache: options.nameCache && (options.nameCache.vars || {}), eval: false, ie8: false, keep_fnames: false, @@ -60,6 +78,16 @@ function minify(files, options) { reserved: [], toplevel: false, }, true); + if (options.nameCache && options.mangle.properties) { + if (typeof options.mangle.properties != "object") { + options.mangle.properties = {}; + } + if (!("cache" in options.mangle.properties)) { + options.mangle.properties.cache = options.nameCache.props || {}; + } + } + init_cache(options.mangle.cache); + init_cache(options.mangle.properties.cache); } if (options.sourceMap) { options.sourceMap = defaults(options.sourceMap, { @@ -153,6 +181,12 @@ function minify(files, options) { } } } + if (options.nameCache && options.mangle) { + if (options.mangle.cache) options.nameCache.vars = to_json(options.mangle.cache); + if (options.mangle.properties && options.mangle.properties.cache) { + options.nameCache.props = to_json(options.mangle.properties.cache); + } + } if (timings) { timings.end = Date.now(); result.timings = { |