From fe829aa850b702588ccf92dbec370cedd3fd88a0 Mon Sep 17 00:00:00 2001 From: Equim Date: Sun, 25 Feb 2018 01:29:45 +0800 Subject: [PATCH] build: also uglify service worker --- build/service-worker-prod.js | 4 ++-- build/webpack.prod.conf.js | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/build/service-worker-prod.js b/build/service-worker-prod.js index 1179ec20..8b8493e6 100644 --- a/build/service-worker-prod.js +++ b/build/service-worker-prod.js @@ -4,7 +4,7 @@ // Check to make sure service workers are supported in the current browser, // and that the current page is accessed from a secure origin. Using a // service worker from an insecure origin will trigger JS console errors. - const isLocalhost = Boolean(window.location.hostname === 'localhost' || + var isLocalhost = Boolean(window.location.hostname === 'localhost' || // [::1] is the IPv6 localhost address. window.location.hostname === '[::1]' || // 127.0.0.1/8 is considered localhost for IPv4. @@ -26,7 +26,7 @@ // i.e. whether there's an existing service worker. if (navigator.serviceWorker.controller) { // The updatefound event implies that registration.installing is set - const installingWorker = registration.installing; + var installingWorker = registration.installing; installingWorker.onstatechange = function() { switch (installingWorker.state) { diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index 7b4b21c4..9751b880 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -84,8 +84,16 @@ var webpackConfig = merge(baseWebpackConfig, { }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: 'dependency', - serviceWorkerLoader: `` + serviceWorkerLoader: (() => { + let sw = fs.readFileSync(path.join(__dirname, './service-worker-prod.js'), 'utf-8') + + let result = UglifyJS.minify(sw) + if (result.error == null) { + sw = result.code + } + + return '' + })() }), // split vendor js into its own file new webpack.optimize.CommonsChunkPlugin({