Skip to content

Commit 5448d0b

Browse files
authored
Merge pull request #357 from ooflorent/webpack_hooks
Use webpack hooks
2 parents 9e7b435 + daddd83 commit 5448d0b

4 files changed

Lines changed: 103 additions & 53 deletions

File tree

lib/index.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,29 +214,28 @@ var OfflinePlugin = (function () {
214214
_this2.resolveToolPaths(tool, key, compiler);
215215
});
216216

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

221-
if (resource !== runtimePath) {
222-
return callback(null, result);
223-
}
220+
if (resource !== runtimePath) {
221+
callback(null, result);
222+
return;
223+
}
224224

225-
var data = {
226-
autoUpdate: _this2.autoUpdate
227-
};
225+
var data = {
226+
autoUpdate: _this2.autoUpdate
227+
};
228228

229-
_this2.useTools(function (tool, key) {
230-
data[key] = tool.getConfig(_this2);
231-
});
229+
_this2.useTools(function (tool, key) {
230+
data[key] = tool.getConfig(_this2);
231+
});
232232

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

235-
callback(null, result);
236-
});
237-
});
235+
callback(null, result);
236+
};
238237

239-
compiler.plugin('make', function (compilation, callback) {
238+
var makeFn = function makeFn(compilation, callback) {
240239
if (_this2.warnings.length) {
241240
[].push.apply(compilation.warnings, _this2.warnings);
242241
}
@@ -252,9 +251,9 @@ var OfflinePlugin = (function () {
252251
})['catch'](function (e) {
253252
throw e || new Error('Something went wrong');
254253
});
255-
});
254+
};
256255

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

@@ -295,7 +294,27 @@ var OfflinePlugin = (function () {
295294
}, function () {
296295
callback(new Error('Something went wrong'));
297296
});
298-
});
297+
};
298+
299+
if (compiler.hooks) {
300+
(function () {
301+
var plugin = { name: 'OfflinePlugin' };
302+
303+
compiler.hooks.normalModuleFactory.tap(plugin, function (nmf) {
304+
nmf.hooks.afterResolve.tapAsync(plugin, afterResolveFn);
305+
});
306+
307+
compiler.hooks.make.tapAsync(plugin, makeFn);
308+
compiler.hooks.emit.tapAsync(plugin, emitFn);
309+
})();
310+
} else {
311+
compiler.plugin('normal-module-factory', function (nmf) {
312+
nmf.plugin('after-resolve', afterResolveFn);
313+
});
314+
315+
compiler.plugin('make', makeFn);
316+
compiler.plugin('emit', emitFn);
317+
}
299318
}
300319
}, {
301320
key: 'setAssets',

lib/service-worker.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var ServiceWorker = (function () {
8787
var entry = loader + '!' + this.entry;
8888

8989
childCompiler.context = compiler.context;
90-
childCompiler.apply(new _webpackLibSingleEntryPlugin2['default'](compiler.context, entry, name));
90+
new _webpackLibSingleEntryPlugin2['default'](compiler.context, entry, name).apply(childCompiler);
9191

9292
if (this.minify === true) {
9393
if (!_miscGetUglifyPlugin2['default']) {
@@ -110,7 +110,7 @@ var ServiceWorker = (function () {
110110
}
111111
};
112112

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

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

121121
options.test = new RegExp(name);
122-
childCompiler.apply(new _miscGetUglifyPlugin2['default'](options));
122+
new _miscGetUglifyPlugin2['default'](options).apply(childCompiler);
123123

124124
return true;
125125
}
@@ -128,15 +128,22 @@ var ServiceWorker = (function () {
128128

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

137137
compilation.cache = compilation.cache[name];
138138
}
139-
});
139+
};
140+
141+
if (childCompiler.hooks) {
142+
var _plugin = { name: 'OfflinePlugin' };
143+
childCompiler.hooks.compilation.tap(_plugin, compilationFn);
144+
} else {
145+
childCompiler.plugin('compilation', compilationFn);
146+
}
140147

141148
return new Promise(function (resolve, reject) {
142149
childCompiler.runAsChild(function (err, entries, childCompilation) {

src/index.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,32 +206,31 @@ export default class OfflinePlugin {
206206
this.resolveToolPaths(tool, key, compiler);
207207
});
208208

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

213-
if (resource !== runtimePath) {
214-
return callback(null, result);
215-
}
212+
if (resource !== runtimePath) {
213+
callback(null, result);
214+
return;
215+
}
216216

217-
const data = {
218-
autoUpdate: this.autoUpdate
219-
};
217+
const data = {
218+
autoUpdate: this.autoUpdate
219+
};
220220

221-
this.useTools((tool, key) => {
222-
data[key] = tool.getConfig(this);
223-
});
221+
this.useTools((tool, key) => {
222+
data[key] = tool.getConfig(this);
223+
});
224224

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

230-
callback(null, result);
231-
});
232-
});
230+
callback(null, result);
231+
};
233232

234-
compiler.plugin('make', (compilation, callback) => {
233+
const makeFn = (compilation, callback) => {
235234
if (this.warnings.length) {
236235
[].push.apply(compilation.warnings, this.warnings);
237236
}
@@ -247,9 +246,9 @@ export default class OfflinePlugin {
247246
}).catch((e) => {
248247
throw (e || new Error('Something went wrong'));
249248
});
250-
});
249+
};
251250

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

@@ -294,7 +293,25 @@ export default class OfflinePlugin {
294293
}, () => {
295294
callback(new Error('Something went wrong'));
296295
});
297-
});
296+
};
297+
298+
if (compiler.hooks) {
299+
const plugin = { name: 'OfflinePlugin' };
300+
301+
compiler.hooks.normalModuleFactory.tap(plugin, (nmf) => {
302+
nmf.hooks.afterResolve.tapAsync(plugin, afterResolveFn);
303+
});
304+
305+
compiler.hooks.make.tapAsync(plugin, makeFn);
306+
compiler.hooks.emit.tapAsync(plugin, emitFn);
307+
} else {
308+
compiler.plugin('normal-module-factory', (nmf) => {
309+
nmf.plugin('after-resolve', afterResolveFn);
310+
});
311+
312+
compiler.plugin('make', makeFn);
313+
compiler.plugin('emit', emitFn);
314+
}
298315
}
299316

300317
setAssets(compilation) {

src/service-worker.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class ServiceWorker {
6565
const entry = loader + '!' + this.entry;
6666

6767
childCompiler.context = compiler.context;
68-
childCompiler.apply(new SingleEntryPlugin(compiler.context, entry, name));
68+
new SingleEntryPlugin(compiler.context, entry, name).apply(childCompiler);
6969

7070
if (this.minify === true) {
7171
if (!UglifyJsPlugin) {
@@ -88,7 +88,7 @@ export default class ServiceWorker {
8888
}
8989
};
9090

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

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

9999
options.test = new RegExp(name);
100-
childCompiler.apply(new UglifyJsPlugin(options));
100+
new UglifyJsPlugin(options).apply(childCompiler);
101101

102102
return true;
103103
}
@@ -106,15 +106,22 @@ export default class ServiceWorker {
106106

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

115115
compilation.cache = compilation.cache[name];
116116
}
117-
});
117+
};
118+
119+
if (childCompiler.hooks) {
120+
const plugin = { name: 'OfflinePlugin' };
121+
childCompiler.hooks.compilation.tap(plugin, compilationFn);
122+
} else {
123+
childCompiler.plugin('compilation', compilationFn);
124+
}
118125

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

0 commit comments

Comments
 (0)