From b39e8172b6be91102916a910b4aff28271abed21 Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Tue, 4 Dec 2018 12:47:52 +0700 Subject: [PATCH] fix: use custom terser-webpack-plugin setup Cherry-picked from and closes #131. --- lib/webpack/webpack.config.base.js | 53 ++++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/lib/webpack/webpack.config.base.js b/lib/webpack/webpack.config.base.js index 2ecea15..ad7e80c 100644 --- a/lib/webpack/webpack.config.base.js +++ b/lib/webpack/webpack.config.base.js @@ -65,16 +65,55 @@ module.exports = (api, config, type) => { // No need to minimize in server or dev mode if (type === 'client' && !api.options.dev && api.config.minimize !== false) { - // TODO: is there a simpler way to pull default minimizer setup without copy/pasting it? - const defaulter = new webpack.WebpackOptionsDefaulter() - const minimizer = defaulter.defaults['optimization.minimizer']( - config.toConfig() - ) config.merge({ optimization: { minimize: true, minimizer: [ - ...minimizer, + { + apply(compiler) { + const TerserPlugin = require('terser-webpack-plugin') + new TerserPlugin({ + cache: true, + parallel: true, + sourceMap: config.get('devtool') !== false, + terserOptions: { + parse: { + // we want terser to parse ecma 8 code. However, we don't want it + // to apply any minfication steps that turns valid ecma 5 code + // into invalid ecma 5 code. This is why the 'compress' and 'output' + // sections only apply transformations that are ecma 5 safe + // https://github.com/facebook/create-react-app/pull/4234 + ecma: 8 + }, + compress: { + ecma: 5, + warnings: false, + // Disabled because of an issue with Uglify breaking seemingly valid code: + // https://github.com/facebook/create-react-app/issues/2376 + // Pending further investigation: + // https://github.com/mishoo/UglifyJS2/issues/2011 + comparisons: false, + // Disabled because of an issue with Terser breaking valid code: + // https://github.com/facebook/create-react-app/issues/5250 + // Pending futher investigation: + // https://github.com/terser-js/terser/issues/120 + // Fixed in terser 3.10.7 which is not yet pulled by terser-webpack-plugin + inline: 2 + }, + mangle: { + safari10: true + }, + output: { + ecma: 5, + comments: false, + // Turned on because emoji and regex is not minified properly using default + // https://github.com/facebook/create-react-app/issues/2488 + ascii_only: true // eslint-disable-line camelcase + } + } + }).apply(compiler) + } + }, { apply(compiler) { // eslint-disable-next-line import/no-extraneous-dependencies @@ -219,7 +258,7 @@ module.exports = (api, config, type) => { applyLoaders(normalRule, false) function applyLoaders(rule, modules) { - const sourceMap = !isProd + const sourceMap = config.get('devtool') !== false if (extract) { if (type === 'client') { diff --git a/package.json b/package.json index f062633..8f9707c 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "register-service-worker": "^1.2.0", "serialize-javascript": "^1.4.0", "serve-static": "^1.13.2", + "terser-webpack-plugin": "^1.1.0", "time-fix-plugin": "^2.0.4", "url-loader": "^1.1.0", "vue": "^2.5.16",