From cbdff0db20d8b0dc22cab9d419d42517531ee766 Mon Sep 17 00:00:00 2001 From: Kencana Kesuma Date: Thu, 16 Jul 2020 15:13:13 +0800 Subject: [PATCH] GH-591 fix(sw-src-load): fix service worker being loaded from cdn issue - introduced new cli args to define swPath, default would be '/' - decoupled from using webpack public path, better to be implicit --- packages/cli/lib/index.js | 1 + packages/cli/lib/lib/entry.js | 8 +++----- packages/cli/lib/lib/webpack/webpack-client-config.js | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index b8282cb9f..0fbfd4dfb 100755 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -37,6 +37,7 @@ prog .option('--dest', 'Specify output directory', 'build') .option('--cwd', 'A directory to use instead of $PWD', '.') .option('--sw', 'Generate and attach a Service Worker', true) + .option('--swPath', 'Specify service worker file location, default is root', '/') .option('--json', 'Generate build stats for bundle analysis') .option('--template', 'Path to custom HTML template') .option('--preload', 'Adds preload tags to the document its assets', false) diff --git a/packages/cli/lib/lib/entry.js b/packages/cli/lib/lib/entry.js index 6cfde8078..f017a94b1 100644 --- a/packages/cli/lib/lib/entry.js +++ b/packages/cli/lib/lib/entry.js @@ -1,5 +1,3 @@ -/* global __webpack_public_path__ */ - import * as Preact from 'preact'; const { h, render, hydrate } = Preact; @@ -14,17 +12,17 @@ if (process.env.NODE_ENV === 'development') { // only add a debug sw if webpack service worker is not requested. if (process.env.ADD_SW === undefined && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef - navigator.serviceWorker.register(__webpack_public_path__ + 'sw-debug.js'); + navigator.serviceWorker.register(process.env.SW_PATH + 'sw-debug.js'); } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - __webpack_public_path__ + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - __webpack_public_path__ + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } diff --git a/packages/cli/lib/lib/webpack/webpack-client-config.js b/packages/cli/lib/lib/webpack/webpack-client-config.js index f138e0574..0acbe1a5e 100644 --- a/packages/cli/lib/lib/webpack/webpack-client-config.js +++ b/packages/cli/lib/lib/webpack/webpack-client-config.js @@ -204,6 +204,7 @@ function isProd(config) { 'process.env.ES_BUILD': false, 'process.env.ESM': config.esm, 'process.env.PRERENDER': config.prerender, + 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ], @@ -314,6 +315,7 @@ function isDev(config) { new webpack.DefinePlugin({ 'process.env.ADD_SW': config.sw, 'process.env.PRERENDER': config.prerender, + 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ],