Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to apply arbitrary plugins when compiling ServiceWorker script #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done in ServiceWorker.js file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot be because it refers to options, not this.options.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds wrong. ServiceWorker options shouldn't be touched in index.js, they all are handled in ServiceWorker.js file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to clarify a bit more. It uses deepExtend to set up this.options, and addTool passes it to ServiceWorker.js. However, deepExtend breaks this.options.ServiceWorker.plugins and ServiceWorker.js can do nothing with it when received. Therefore it has to be fixed beforehand.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see what you mean now. It should be handled in some other way though. I'll merge this and do the change to this part myself.

(tests seem to fail now because master has been updated)

// 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) {



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