From d1a7a9426cc0af1e965b017dd45a5ff5dbdeb318 Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Sun, 25 Feb 2018 03:26:15 +0100 Subject: [PATCH] fix(index): add `webpack >= v4.0.0` support (#128) --- src/index.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 8e222d3..68c7ec0 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ export function pitch(request) { const cb = this.async(); const filename = loaderUtils.interpolateName(this, options.name || '[hash].worker.js', { - context: options.context || this.options.context, + context: options.context || this.rootContext || this.options.context, regExp: options.regExp, }); @@ -62,13 +62,23 @@ export function pitch(request) { const subCache = `subcache ${__dirname} ${request}`; - worker.compiler.plugin('compilation', (compilation) => { + worker.compilation = (compilation) => { if (compilation.cache) { - if (!compilation.cache[subCache]) compilation.cache[subCache] = {}; + if (!compilation.cache[subCache]) { + compilation.cache[subCache] = {}; + } compilation.cache = compilation.cache[subCache]; } - }); + }; + + if (worker.compiler.hooks) { + const plugin = { name: 'WorkerLoader' }; + + worker.compiler.hooks.compilation.tap(plugin, worker.compilation); + } else { + worker.compiler.plugin('compilation', worker.compilation); + } worker.compiler.runAsChild((err, entries, compilation) => { if (err) return cb(err);