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 Apr 26, 2018
1 parent 75557ad commit 66855a5
Show file tree
Hide file tree
Showing 22 changed files with 3,082 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:_ `[]`
_Example:_ `[new require('webpack').DefinePlugin({ CAT: 'MEOW' })]`

#### `AppCache: Object | null | false`

Settings for the `AppCache` cache. Use `null` or `false` to disable `AppCache` generation.
Expand Down
1 change: 1 addition & 0 deletions lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exports['default'] = {
events: false,
minify: null,
forceInstall: false,
plugins: [],

updateViaCache: 'imports',

Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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.prefetchRequest = this.validatePrefetch(options.prefetchRequest);
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
1 change: 1 addition & 0 deletions src/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default {
events: false,
minify: null,
forceInstall: false,
plugins: [],

updateViaCache: 'imports',

Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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.prefetchRequest = this.validatePrefetch(options.prefetchRequest);
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CACHE MANIFEST
#ver:da39a3ee5e6b4b0d3255bfef95601890afd80709

CACHE:
../external.js

NETWORK:
*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<html manifest="manifest.appcache"></html>
76 changes: 76 additions & 0 deletions tests/legacy/fixtures/sw-plugins/__expected/webpack2/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {



/***/ })
/******/ ]);

0 comments on commit 66855a5

Please sign in to comment.