Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
[API] Add a template callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Nov 11, 2016
1 parent e1d180f commit e26124f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ Path to the actual service worker implementation.
Relative (from the webpack's config `output.path`) output path for emitted script.
- `excludes`, *array*, default `['**/.*', '**/*.map']`:
Excludes matches assets from being added to the `serviceWorkerOption.assets` variable.
- `relativePaths`, *boolean*, default `true`:
When set to `true`, all cache asset paths are generated relatively to the ServiceWorker
file and `publichPath` option is ignored.
- `publicPath`, *string*, default `''`:
specifies the public URL address of the output files when referenced in a browser.
- `template`, *function*, default noop:
This callback function can be used to inject statically generated service worker.
It's taking a `serviceWorkerOption` argument and must return a promise.

### `runtime(options)`

Expand Down
44 changes: 22 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,12 @@ export default class ServiceWorkerPlugin {
warnings = [];

constructor(options) {
if (options.relativePaths && options.publicPath) {
this.warnings.push(
new Error(`ServiceWorkerPlugin: publicPath is used in conjunction with relativePaths,
relativePaths was set by the ServiceWorkerPlugin to false.`),
);
}

this.options = Object.assign({
publicPath: '',
relativePaths: true,
excludes: ['**/.*', '**/*.map'],
entry: null,
filename: 'sw.js',
template: () => Promise.resolve(''),
}, options);

this.options.filename = this.options.filename.replace(/^\//, '');
Expand Down Expand Up @@ -87,9 +80,7 @@ export default class ServiceWorkerPlugin {
});

compiler.plugin('emit', (compilation, callback) => {
this.handleEmit(compilation, compiler);

callback();
this.handleEmit(compilation, compiler, callback);
});
}

Expand Down Expand Up @@ -127,7 +118,7 @@ export default class ServiceWorkerPlugin {
});
}

handleEmit(compilation, compiler) {
handleEmit(compilation, compiler, callback) {
const asset = compilation.assets[this.options.filename];

if (!asset) {
Expand Down Expand Up @@ -157,18 +148,27 @@ export default class ServiceWorkerPlugin {
return plugin instanceof webpack.optimize.UglifyJsPlugin;
});

const data = JSON.stringify({
const serviceWorkerOption = {
assets,
}, null, minify ? 0 : 2);
};

const source = `
var serviceWorkerOption = ${data};
${asset.source()}
`.trim();
const templatePromise = this.options.template(serviceWorkerOption);

compilation.assets[this.options.filename] = {
source: () => source,
size: () => Buffer.byteLength(source, 'utf8'),
};
templatePromise.then((template) => {
const serviceWorkerOptionInline = JSON.stringify(serviceWorkerOption, null, minify ? 0 : 2);

const source = `
var serviceWorkerOption = ${serviceWorkerOptionInline};
${template}
${asset.source()}
`.trim();

compilation.assets[this.options.filename] = {
source: () => source,
size: () => Buffer.byteLength(source, 'utf8'),
};

callback();
});
}
}

0 comments on commit e26124f

Please sign in to comment.