Skip to content

Commit d43a7e1

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

File tree

22 files changed

+3082
-0
lines changed

22 files changed

+3082
-0
lines changed

docs/options.md

Lines changed: 4 additions & 0 deletions
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:_ `[]`
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/default-options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ exports['default'] = {
5353
scope: null,
5454
events: false,
5555
minify: null,
56+
plugins: [],
5657

5758
prefetchRequest: {
5859
credentials: 'same-origin',

lib/index.js

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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/default-options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default {
4141
scope: null,
4242
events: false,
4343
minify: null,
44+
plugins: [],
4445

4546
prefetchRequest: {
4647
credentials: 'same-origin',

src/index.js

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CACHE MANIFEST
2+
#ver:da39a3ee5e6b4b0d3255bfef95601890afd80709
3+
4+
CACHE:
5+
../external.js
6+
7+
NETWORK:
8+
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<html manifest="manifest.appcache"></html>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // identity function for calling harmony imports with the correct context
37+
/******/ __webpack_require__.i = function(value) { return value; };
38+
/******/
39+
/******/ // define getter function for harmony exports
40+
/******/ __webpack_require__.d = function(exports, name, getter) {
41+
/******/ if(!__webpack_require__.o(exports, name)) {
42+
/******/ Object.defineProperty(exports, name, {
43+
/******/ configurable: false,
44+
/******/ enumerable: true,
45+
/******/ get: getter
46+
/******/ });
47+
/******/ }
48+
/******/ };
49+
/******/
50+
/******/ // getDefaultExport function for compatibility with non-harmony modules
51+
/******/ __webpack_require__.n = function(module) {
52+
/******/ var getter = module && module.__esModule ?
53+
/******/ function getDefault() { return module['default']; } :
54+
/******/ function getModuleExports() { return module; };
55+
/******/ __webpack_require__.d(getter, 'a', getter);
56+
/******/ return getter;
57+
/******/ };
58+
/******/
59+
/******/ // Object.prototype.hasOwnProperty.call
60+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
61+
/******/
62+
/******/ // __webpack_public_path__
63+
/******/ __webpack_require__.p = "";
64+
/******/
65+
/******/ // Load entry module and return exports
66+
/******/ return __webpack_require__(__webpack_require__.s = 0);
67+
/******/ })
68+
/************************************************************************/
69+
/******/ ([
70+
/* 0 */
71+
/***/ (function(module, exports) {
72+
73+
74+
75+
/***/ })
76+
/******/ ]);

0 commit comments

Comments
 (0)