Skip to content

Commit 5951545

Browse files
committed
Merge branch 'jaikme-master'
2 parents e89f02c + 8008ae7 commit 5951545

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/index.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug
88
export default function loader() {}
99

1010
const CACHE = {};
11+
const tapName = 'workerize-loader';
12+
13+
function compilationHook(compiler, handler) {
14+
if (compiler.hooks) {
15+
return compiler.hooks.compilation.tap(tapName, handler);
16+
}
17+
return compiler.plugin('compilation', handler);
18+
}
19+
20+
function parseHook(data, handler) {
21+
if (data.normalModuleFactory.hooks) {
22+
return data.normalModuleFactory.hooks.parser.for('javascript/auto').tap(tapName, handler);
23+
}
24+
return data.normalModuleFactory.plugin('parser', handler);
25+
}
26+
27+
function exportDeclarationHook(parser, handler) {
28+
if (parser.hooks) {
29+
return parser.hooks.exportDeclaration.tap(tapName, handler);
30+
}
31+
return parser.plugin('export declaration', handler);
32+
}
1133

1234
loader.pitch = function(request) {
1335
this.cacheable(false);
@@ -30,26 +52,25 @@ loader.pitch = function(request) {
3052
};
3153

3254
worker.compiler = this._compilation.createChildCompiler('worker', worker.options);
33-
34-
worker.compiler.apply(new WebWorkerTemplatePlugin(worker.options));
55+
56+
(new WebWorkerTemplatePlugin(worker.options)).apply(worker.compiler);
3557

3658
if (this.target!=='webworker' && this.target!=='web') {
37-
worker.compiler.apply(new NodeTargetPlugin());
59+
(new NodeTargetPlugin()).apply(worker.compiler);
3860
}
3961

40-
worker.compiler.apply(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main'));
62+
(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main')).apply(worker.compiler);
4163

4264
const subCache = `subcache ${__dirname} ${request}`;
4365

44-
worker.compiler.plugin('compilation', (compilation, data) => {
66+
compilationHook(worker.compiler, (compilation, data) => {
4567
if (compilation.cache) {
4668
if (!compilation.cache[subCache]) compilation.cache[subCache] = {};
4769

4870
compilation.cache = compilation.cache[subCache];
4971
}
50-
51-
data.normalModuleFactory.plugin('parser', (parser, options) => {
52-
parser.plugin('export declaration', expr => {
72+
parseHook(data, (parser, options) => {
73+
exportDeclarationHook(parser, expr => {
5374
let decl = expr.declaration || expr,
5475
{ compilation, current } = parser.state,
5576
entry = compilation.entries[0].resource;

0 commit comments

Comments
 (0)