Skip to content

Commit

Permalink
Allow to apply arbitrary plugins when compiling ServiceWorker script
Browse files Browse the repository at this point in the history
  • Loading branch information
akihikodaki committed Mar 20, 2018
1 parent 5448d0b commit fca50db
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/'`

Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit fca50db

Please sign in to comment.