From 5a6d537d4356de0b65c712efd944486b929694c1 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 26 Jan 2019 18:55:32 -0800 Subject: [PATCH] Generalize minifier (#2577) --- config/.eslintrc.json | 7 +++++++ config/minimizer.js | 29 +++++++++++++++++++++++++++++ demo/webpack.config.js | 20 +++++++------------- gallery/webpack.config.js | 5 ++++- hassio/webpack.config.js | 14 ++++---------- webpack.config.js | 26 ++------------------------ 6 files changed, 53 insertions(+), 48 deletions(-) create mode 100644 config/.eslintrc.json create mode 100644 config/minimizer.js diff --git a/config/.eslintrc.json b/config/.eslintrc.json new file mode 100644 index 0000000000..7ad00f95b3 --- /dev/null +++ b/config/.eslintrc.json @@ -0,0 +1,7 @@ +{ + "extends": "../.eslintrc.json", + "rules": { + "import/no-extraneous-dependencies": 0, + "global-require": 0 + } +} diff --git a/config/minimizer.js b/config/minimizer.js new file mode 100644 index 0000000000..ce16504520 --- /dev/null +++ b/config/minimizer.js @@ -0,0 +1,29 @@ +const BabelMinifyPlugin = require("babel-minify-webpack-plugin"); + +module.exports.minimizer = [ + // Took options from Polymer build tool + // https://github.com/Polymer/tools/blob/master/packages/build/src/js-transform.ts + new BabelMinifyPlugin( + { + // Disable the minify-constant-folding plugin because it has a bug relating + // to invalid substitution of constant values into export specifiers: + // https://github.com/babel/minify/issues/820 + evaluate: false, + + // TODO(aomarks) Find out why we disabled this plugin. + simplifyComparisons: false, + + // Prevent removal of things that babel thinks are unreachable, but sometimes + // gets wrong: https://github.com/Polymer/tools/issues/724 + deadcode: false, + + // Disable the simplify plugin because it can eat some statements preceeding + // loops. https://github.com/babel/minify/issues/824 + simplify: false, + + // This is breaking ES6 output. https://github.com/Polymer/tools/issues/261 + mangle: false, + }, + {} + ), +]; diff --git a/demo/webpack.config.js b/demo/webpack.config.js index f5a3fd1d2d..2e87ffd692 100644 --- a/demo/webpack.config.js +++ b/demo/webpack.config.js @@ -1,9 +1,9 @@ const path = require("path"); const webpack = require("webpack"); -const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const WorkboxPlugin = require("workbox-webpack-plugin"); const { babelLoaderConfig } = require("../config/babel.js"); +const { minimizer } = require("../config/babel.js"); const isProd = process.env.NODE_ENV === "production"; const chunkFilename = isProd ? "chunk.[chunkhash].js" : "[name].chunk.js"; @@ -39,6 +39,9 @@ module.exports = { }, ], }, + optimization: { + minimizer, + }, plugins: [ new webpack.DefinePlugin({ __DEV__: false, @@ -69,19 +72,10 @@ module.exports = { }, ]), isProd && - new UglifyJsPlugin({ - extractComments: true, - sourceMap: true, - uglifyOptions: { - // Disabling because it broke output - mangle: false, - }, + new WorkboxPlugin.GenerateSW({ + swDest: "service_worker_es5.js", + importWorkboxFrom: "local", }), - // isProd && - new WorkboxPlugin.GenerateSW({ - swDest: "service_worker_es5.js", - importWorkboxFrom: "local", - }), ].filter(Boolean), resolve: { extensions: [".ts", ".js", ".json"], diff --git a/gallery/webpack.config.js b/gallery/webpack.config.js index 7b711ac355..032efa198d 100644 --- a/gallery/webpack.config.js +++ b/gallery/webpack.config.js @@ -1,7 +1,7 @@ const path = require("path"); -const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const { babelLoaderConfig } = require("../config/babel.js"); +const { minimizer } = require("../config/babel.js"); const isProd = process.env.NODE_ENV === "production"; const chunkFilename = isProd ? "chunk.[chunkhash].js" : "[name].chunk.js"; @@ -32,6 +32,9 @@ module.exports = { }, ], }, + optimization: { + minimizer, + }, plugins: [ new CopyWebpackPlugin([ "public", diff --git a/hassio/webpack.config.js b/hassio/webpack.config.js index 5872e7f8b7..f4acec7d3a 100644 --- a/hassio/webpack.config.js +++ b/hassio/webpack.config.js @@ -1,7 +1,7 @@ -const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); const CompressionPlugin = require("compression-webpack-plugin"); const config = require("./config.js"); const { babelLoaderConfig } = require("../config/babel.js"); +const { minimizer } = require("../config/babel.js"); const isProdBuild = process.env.NODE_ENV === "production"; const isCI = process.env.CI === "true"; @@ -27,16 +27,10 @@ module.exports = { }, ], }, + optimization: { + minimizer, + }, plugins: [ - isProdBuild && - new UglifyJsPlugin({ - extractComments: true, - sourceMap: true, - uglifyOptions: { - // Disabling because it broke output - mangle: false, - }, - }), isProdBuild && !isCI && new CompressionPlugin({ diff --git a/webpack.config.js b/webpack.config.js index cb618e61d3..46e71154de 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,7 +1,6 @@ const fs = require("fs"); const path = require("path"); const webpack = require("webpack"); -const BabelMinifyPlugin = require("babel-minify-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const WorkboxPlugin = require("workbox-webpack-plugin"); const CompressionPlugin = require("compression-webpack-plugin"); @@ -9,6 +8,7 @@ const HtmlWebpackPlugin = require("html-webpack-plugin"); const zopfli = require("@gfx/zopfli"); const translationMetadata = require("./build-translations/translationMetadata.json"); const { babelLoaderConfig } = require("./config/babel.js"); +const { minimizer } = require("./config/minimizer.js"); const version = fs.readFileSync("setup.py", "utf8").match(/\d{8}\.\d+/); if (!version) { @@ -77,29 +77,7 @@ function createConfig(isProdBuild, latestBuild) { ], }, optimization: { - minimizer: [ - // Took options from Polymer build tool - // https://github.com/Polymer/tools/blob/master/packages/build/src/js-transform.ts - new BabelMinifyPlugin( - { - // Disable the minify-constant-folding plugin because it has a bug relating to - // invalid substitution of constant values into export specifiers: - // https://github.com/babel/minify/issues/820 - evaluate: false, - - // TODO(aomarks) Find out why we disabled this plugin. - simplifyComparisons: false, - - // Disable the simplify plugin because it can eat some statements preceeding - // loops. https://github.com/babel/minify/issues/824 - simplify: false, - - // This is breaking ES6 output. https://github.com/Polymer/tools/issues/261 - mangle: false, - }, - {} - ), - ], + minimizer, }, plugins: [ new webpack.DefinePlugin({