From fca50dba2082de5ec8cd2f2119adde72f86a3659 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 20 Mar 2018 12:32:39 +0900 Subject: [PATCH] Allow to apply arbitrary plugins when compiling ServiceWorker script --- docs/options.md | 4 ++++ lib/index.js | 5 +++++ lib/service-worker.js | 5 +++++ src/index.js | 5 +++++ src/service-worker.js | 3 +++ 5 files changed, 22 insertions(+) diff --git a/docs/options.md b/docs/options.md index bf1d8c98..7c45fc46 100755 --- a/docs/options.md +++ b/docs/options.md @@ -131,6 +131,10 @@ _Example:_ `{ credentials: 'include' }` * **`minify`**: `boolean`. If set to `true` or `false`, the `ServiceWorker`'s output will be minified or not accordingly. If set to something else, the `ServiceWorker` output will be minified **if** you are using `webpack.optimize.UglifyJsPlugin` in your configuration. _Default:_ `null` +* **`plugins`**: `Array`. The plugins which will be applied when compling the `ServiceWorker`'s script. +_Default:_ `undefined` (this option is no-op.) +_Example:_ `[new require('webpack').DefinePlugin({ CAT: 'MEOW' })]` + * **[Deprecated]** `navigateFallbackURL`: `string`. The URL that should be returned from the cache when a requested navigation URL isn't available on the cache or network. Similar to the `AppCache.FALLBACK` option. _Example:_ `navigateFallbackURL: '/'` diff --git a/lib/index.js b/lib/index.js index 1efef856..acfbf0fc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -69,6 +69,11 @@ var OfflinePlugin = (function () { AppCache: false }); + if (options.ServiceWorker && options.ServiceWorker.plugins) { + // plugins are class instances and should not be modified. + this.options.ServiceWorker.plugins = options.ServiceWorker.plugins; + } + this.hash = null; this.assets = null; this.hashesMap = null; diff --git a/lib/service-worker.js b/lib/service-worker.js index 0a383f57..b733ecba 100644 --- a/lib/service-worker.js +++ b/lib/service-worker.js @@ -46,6 +46,7 @@ var ServiceWorker = (function () { // Tool specific properties this.entry = options.entry; + this.plugins = options.plugins; this.scope = options.scope ? options.scope + '' : void 0; this.events = !!options.events; this.navigateFallbackURL = options.navigateFallbackURL; @@ -126,6 +127,10 @@ var ServiceWorker = (function () { }); } + this.plugins.forEach(function (plugin) { + return plugin.apply(childCompiler); + }); + // Needed for HMR. offline-plugin doesn't support it, // but added just in case to prevent other errors var compilationFn = function compilationFn(compilation) { diff --git a/src/index.js b/src/index.js index 8dcfca7f..41c99d5b 100644 --- a/src/index.js +++ b/src/index.js @@ -30,6 +30,11 @@ export default class OfflinePlugin { AppCache: false }); + if (options.ServiceWorker && options.ServiceWorker.plugins) { + // plugins are class instances and should not be modified. + this.options.ServiceWorker.plugins = options.ServiceWorker.plugins; + } + this.hash = null; this.assets = null; this.hashesMap = null; diff --git a/src/service-worker.js b/src/service-worker.js index 983a0ac5..9710ee0d 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -26,6 +26,7 @@ export default class ServiceWorker { // Tool specific properties this.entry = options.entry; + this.plugins = options.plugins; this.scope = options.scope ? options.scope + '' : void 0; this.events = !!options.events; this.navigateFallbackURL = options.navigateFallbackURL; @@ -104,6 +105,8 @@ export default class ServiceWorker { }); } + this.plugins.forEach((plugin) => plugin.apply(childCompiler)); + // Needed for HMR. offline-plugin doesn't support it, // but added just in case to prevent other errors const compilationFn = (compilation) => {