Skip to content

Commit

Permalink
Improve ServiceWorker entry generation
Browse files Browse the repository at this point in the history
Use `compilation.namedChunks` instead of `compilation.assets` to pick
corrent entry and then `chunk.files` to pick correct output file from
`compilation.assets`

Refs #10
  • Loading branch information
NekR committed Oct 21, 2015
1 parent b67a654 commit e72e74c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
25 changes: 16 additions & 9 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,29 @@ var ServiceWorker = (function () {

if (this.entry) {
var _name = plugin.entryPrefix + this.ENTRY_NAME;
var entry = compilation.assets[_name];
var entry = compilation.namedChunks[_name];

if (!entry) {
_name = _name + '.js';
entry = compilation.assets[_name];
}
compilation.errors.push(new Error('OfflinePlugin: ServiceWorker entry is not found in output chunks'));

if (!entry) {
compilation.errors.push(new Error('OfflinePlugin: Something went wrong with ServiceWorker entry'));
return;
}

entry = entry.source();
delete compilation.assets[_name];
if (entry.files.length > 1) {
compilation.warnings.push(new Error('OfflinePlugin: ServiceWorker entry has more than one output file, only first will be used'));
}

var entryAsset = entry.files[0];

entryAsset = compilation.assets[entryAsset];
entryAsset = entryAsset.source();

entry.files.forEach(function (file) {
delete compilation.assets[file];
});

source += '\n\n' + entry;
entry.files = [this.output];
source += '\n\n' + entryAsset;
}

compilation.assets[this.output] = (0, _miscGetSource2['default'])(source);
Expand Down
31 changes: 20 additions & 11 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,34 @@ export default class ServiceWorker {
let source = this.getDataTemplate(plugin.caches, plugin, minify);

if (this.entry) {
let name = plugin.entryPrefix + this.ENTRY_NAME;
let entry = compilation.assets[name]

if (!entry) {
name = name + '.js';
entry = compilation.assets[name]
}
const name = plugin.entryPrefix + this.ENTRY_NAME;
const entry = compilation.namedChunks[name];

if (!entry) {
compilation.errors.push(
new Error('OfflinePlugin: Something went wrong with ServiceWorker entry')
new Error('OfflinePlugin: ServiceWorker entry is not found in output chunks')
);

return;
}

entry = entry.source();
delete compilation.assets[name];
if (entry.files.length > 1) {
compilation.warnings.push(
new Error('OfflinePlugin: ServiceWorker entry has more than one output file, only first will be used')
);
}

let entryAsset = entry.files[0];

entryAsset = compilation.assets[entryAsset];
entryAsset = entryAsset.source();

entry.files.forEach((file) => {
delete compilation.assets[file];
});

source += '\n\n' + entry;
entry.files = [this.output];
source += '\n\n' + entryAsset;
}

compilation.assets[this.output] = getSource(source);
Expand Down

0 comments on commit e72e74c

Please sign in to comment.