Skip to content

Commit

Permalink
Merge pull request #357 from ooflorent/webpack_hooks
Browse files Browse the repository at this point in the history
Use webpack hooks
  • Loading branch information
NekR authored Mar 16, 2018
2 parents 9e7b435 + daddd83 commit 5448d0b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 53 deletions.
59 changes: 39 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,28 @@ var OfflinePlugin = (function () {
_this2.resolveToolPaths(tool, key, compiler);
});

compiler.plugin('normal-module-factory', function (nmf) {
nmf.plugin('after-resolve', function (result, callback) {
var resource = _path3['default'].resolve(compiler.context, result.resource);
var afterResolveFn = function afterResolveFn(result, callback) {
var resource = _path3['default'].resolve(compiler.context, result.resource);

if (resource !== runtimePath) {
return callback(null, result);
}
if (resource !== runtimePath) {
callback(null, result);
return;
}

var data = {
autoUpdate: _this2.autoUpdate
};
var data = {
autoUpdate: _this2.autoUpdate
};

_this2.useTools(function (tool, key) {
data[key] = tool.getConfig(_this2);
});
_this2.useTools(function (tool, key) {
data[key] = tool.getConfig(_this2);
});

result.loaders.push(_path3['default'].join(__dirname, 'misc/runtime-loader.js') + '?' + JSON.stringify(data));
result.loaders.push(_path3['default'].join(__dirname, 'misc/runtime-loader.js') + '?' + JSON.stringify(data));

callback(null, result);
});
});
callback(null, result);
};

compiler.plugin('make', function (compilation, callback) {
var makeFn = function makeFn(compilation, callback) {
if (_this2.warnings.length) {
[].push.apply(compilation.warnings, _this2.warnings);
}
Expand All @@ -252,9 +251,9 @@ var OfflinePlugin = (function () {
})['catch'](function (e) {
throw e || new Error('Something went wrong');
});
});
};

compiler.plugin('emit', function (compilation, callback) {
var emitFn = function emitFn(compilation, callback) {
var runtimeTemplatePath = _path3['default'].resolve(__dirname, '../tpls/runtime-template.js');
var hasRuntime = true;

Expand Down Expand Up @@ -295,7 +294,27 @@ var OfflinePlugin = (function () {
}, function () {
callback(new Error('Something went wrong'));
});
});
};

if (compiler.hooks) {
(function () {
var plugin = { name: 'OfflinePlugin' };

compiler.hooks.normalModuleFactory.tap(plugin, function (nmf) {
nmf.hooks.afterResolve.tapAsync(plugin, afterResolveFn);
});

compiler.hooks.make.tapAsync(plugin, makeFn);
compiler.hooks.emit.tapAsync(plugin, emitFn);
})();
} else {
compiler.plugin('normal-module-factory', function (nmf) {
nmf.plugin('after-resolve', afterResolveFn);
});

compiler.plugin('make', makeFn);
compiler.plugin('emit', emitFn);
}
}
}, {
key: 'setAssets',
Expand Down
17 changes: 12 additions & 5 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var ServiceWorker = (function () {
var entry = loader + '!' + this.entry;

childCompiler.context = compiler.context;
childCompiler.apply(new _webpackLibSingleEntryPlugin2['default'](compiler.context, entry, name));
new _webpackLibSingleEntryPlugin2['default'](compiler.context, entry, name).apply(childCompiler);

if (this.minify === true) {
if (!_miscGetUglifyPlugin2['default']) {
Expand All @@ -110,7 +110,7 @@ var ServiceWorker = (function () {
}
};

childCompiler.apply(new _miscGetUglifyPlugin2['default'](options));
new _miscGetUglifyPlugin2['default'](options).apply(childCompiler);
} else if (this.minify !== false && _miscGetUglifyPlugin2['default']) {
// Do not perform auto-minification if UglifyJsPlugin isn't installed

Expand All @@ -119,7 +119,7 @@ var ServiceWorker = (function () {
var options = (0, _deepExtend2['default'])({}, plugin.options);

options.test = new RegExp(name);
childCompiler.apply(new _miscGetUglifyPlugin2['default'](options));
new _miscGetUglifyPlugin2['default'](options).apply(childCompiler);

return true;
}
Expand All @@ -128,15 +128,22 @@ var ServiceWorker = (function () {

// Needed for HMR. offline-plugin doesn't support it,
// but added just in case to prevent other errors
childCompiler.plugin('compilation', function (compilation) {
var compilationFn = function compilationFn(compilation) {
if (compilation.cache) {
if (!compilation.cache[name]) {
compilation.cache[name] = {};
}

compilation.cache = compilation.cache[name];
}
});
};

if (childCompiler.hooks) {
var _plugin = { name: 'OfflinePlugin' };
childCompiler.hooks.compilation.tap(_plugin, compilationFn);
} else {
childCompiler.plugin('compilation', compilationFn);
}

return new Promise(function (resolve, reject) {
childCompiler.runAsChild(function (err, entries, childCompilation) {
Expand Down
63 changes: 40 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,32 +206,31 @@ export default class OfflinePlugin {
this.resolveToolPaths(tool, key, compiler);
});

compiler.plugin('normal-module-factory', (nmf) => {
nmf.plugin('after-resolve', (result, callback) => {
const resource = path.resolve(compiler.context, result.resource);
const afterResolveFn = (result, callback) => {
const resource = path.resolve(compiler.context, result.resource);

if (resource !== runtimePath) {
return callback(null, result);
}
if (resource !== runtimePath) {
callback(null, result);
return;
}

const data = {
autoUpdate: this.autoUpdate
};
const data = {
autoUpdate: this.autoUpdate
};

this.useTools((tool, key) => {
data[key] = tool.getConfig(this);
});
this.useTools((tool, key) => {
data[key] = tool.getConfig(this);
});

result.loaders.push(
path.join(__dirname, 'misc/runtime-loader.js') +
'?' + JSON.stringify(data)
);
result.loaders.push(
path.join(__dirname, 'misc/runtime-loader.js') +
'?' + JSON.stringify(data)
);

callback(null, result);
});
});
callback(null, result);
};

compiler.plugin('make', (compilation, callback) => {
const makeFn = (compilation, callback) => {
if (this.warnings.length) {
[].push.apply(compilation.warnings, this.warnings);
}
Expand All @@ -247,9 +246,9 @@ export default class OfflinePlugin {
}).catch((e) => {
throw (e || new Error('Something went wrong'));
});
});
};

compiler.plugin('emit', (compilation, callback) => {
const emitFn = (compilation, callback) => {
const runtimeTemplatePath = path.resolve(__dirname, '../tpls/runtime-template.js');
let hasRuntime = true;

Expand Down Expand Up @@ -294,7 +293,25 @@ export default class OfflinePlugin {
}, () => {
callback(new Error('Something went wrong'));
});
});
};

if (compiler.hooks) {
const plugin = { name: 'OfflinePlugin' };

compiler.hooks.normalModuleFactory.tap(plugin, (nmf) => {
nmf.hooks.afterResolve.tapAsync(plugin, afterResolveFn);
});

compiler.hooks.make.tapAsync(plugin, makeFn);
compiler.hooks.emit.tapAsync(plugin, emitFn);
} else {
compiler.plugin('normal-module-factory', (nmf) => {
nmf.plugin('after-resolve', afterResolveFn);
});

compiler.plugin('make', makeFn);
compiler.plugin('emit', emitFn);
}
}

setAssets(compilation) {
Expand Down
17 changes: 12 additions & 5 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class ServiceWorker {
const entry = loader + '!' + this.entry;

childCompiler.context = compiler.context;
childCompiler.apply(new SingleEntryPlugin(compiler.context, entry, name));
new SingleEntryPlugin(compiler.context, entry, name).apply(childCompiler);

if (this.minify === true) {
if (!UglifyJsPlugin) {
Expand All @@ -88,7 +88,7 @@ export default class ServiceWorker {
}
};

childCompiler.apply(new UglifyJsPlugin(options));
new UglifyJsPlugin(options).apply(childCompiler);
} else if (this.minify !== false && UglifyJsPlugin) {
// Do not perform auto-minification if UglifyJsPlugin isn't installed

Expand All @@ -97,7 +97,7 @@ export default class ServiceWorker {
const options = deepExtend({}, plugin.options);

options.test = new RegExp(name);
childCompiler.apply(new UglifyJsPlugin(options));
new UglifyJsPlugin(options).apply(childCompiler);

return true;
}
Expand All @@ -106,15 +106,22 @@ export default class ServiceWorker {

// Needed for HMR. offline-plugin doesn't support it,
// but added just in case to prevent other errors
childCompiler.plugin('compilation', function (compilation) {
const compilationFn = (compilation) => {
if (compilation.cache) {
if (!compilation.cache[name]) {
compilation.cache[name] = {};
}

compilation.cache = compilation.cache[name];
}
});
};

if (childCompiler.hooks) {
const plugin = { name: 'OfflinePlugin' };
childCompiler.hooks.compilation.tap(plugin, compilationFn);
} else {
childCompiler.plugin('compilation', compilationFn);
}

return new Promise((resolve, reject) => {
childCompiler.runAsChild((err, entries, childCompilation) => {
Expand Down

0 comments on commit 5448d0b

Please sign in to comment.