Skip to content

Commit fca50db

Browse files
committed
Allow to apply arbitrary plugins when compiling ServiceWorker script
1 parent 5448d0b commit fca50db

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

docs/options.md

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ _Example:_ `{ credentials: 'include' }`
131131
* **`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.
132132
_Default:_ `null`
133133

134+
* **`plugins`**: `Array`. The plugins which will be applied when compling the `ServiceWorker`'s script.
135+
_Default:_ `undefined` (this option is no-op.)
136+
_Example:_ `[new require('webpack').DefinePlugin({ CAT: 'MEOW' })]`
137+
134138
* **[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.
135139
_Example:_ `navigateFallbackURL: '/'`
136140

lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ var OfflinePlugin = (function () {
6969
AppCache: false
7070
});
7171

72+
if (options.ServiceWorker && options.ServiceWorker.plugins) {
73+
// plugins are class instances and should not be modified.
74+
this.options.ServiceWorker.plugins = options.ServiceWorker.plugins;
75+
}
76+
7277
this.hash = null;
7378
this.assets = null;
7479
this.hashesMap = null;

lib/service-worker.js

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var ServiceWorker = (function () {
4646

4747
// Tool specific properties
4848
this.entry = options.entry;
49+
this.plugins = options.plugins;
4950
this.scope = options.scope ? options.scope + '' : void 0;
5051
this.events = !!options.events;
5152
this.navigateFallbackURL = options.navigateFallbackURL;
@@ -126,6 +127,10 @@ var ServiceWorker = (function () {
126127
});
127128
}
128129

130+
this.plugins.forEach(function (plugin) {
131+
return plugin.apply(childCompiler);
132+
});
133+
129134
// Needed for HMR. offline-plugin doesn't support it,
130135
// but added just in case to prevent other errors
131136
var compilationFn = function compilationFn(compilation) {

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export default class OfflinePlugin {
3030
AppCache: false
3131
});
3232

33+
if (options.ServiceWorker && options.ServiceWorker.plugins) {
34+
// plugins are class instances and should not be modified.
35+
this.options.ServiceWorker.plugins = options.ServiceWorker.plugins;
36+
}
37+
3338
this.hash = null;
3439
this.assets = null;
3540
this.hashesMap = null;

src/service-worker.js

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default class ServiceWorker {
2626

2727
// Tool specific properties
2828
this.entry = options.entry;
29+
this.plugins = options.plugins;
2930
this.scope = options.scope ? options.scope + '' : void 0;
3031
this.events = !!options.events;
3132
this.navigateFallbackURL = options.navigateFallbackURL;
@@ -104,6 +105,8 @@ export default class ServiceWorker {
104105
});
105106
}
106107

108+
this.plugins.forEach((plugin) => plugin.apply(childCompiler));
109+
107110
// Needed for HMR. offline-plugin doesn't support it,
108111
// but added just in case to prevent other errors
109112
const compilationFn = (compilation) => {

0 commit comments

Comments
 (0)