This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
AddWorkerEntryPointPlugin.ts
52 lines (44 loc) · 1.96 KB
/
AddWorkerEntryPointPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type * as webpack from 'webpack';
export interface IAddWorkerEntryPointPluginOptions {
id: string;
entry: string;
filename: string;
chunkFilename?: string;
plugins: webpack.WebpackPluginInstance[];
}
function getCompilerHook(compiler: webpack.Compiler, { id, entry, filename, chunkFilename, plugins }: IAddWorkerEntryPointPluginOptions) {
const webpack = compiler.webpack ?? require('webpack');
return function (compilation: webpack.Compilation, callback: (error?: Error | null | false) => void) {
const outputOptions = {
filename,
chunkFilename,
publicPath: compilation.outputOptions.publicPath,
// HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
globalObject: 'this',
};
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
new webpack.webworker.WebWorkerTemplatePlugin(),
new webpack.LoaderTargetPlugin('webworker'),
]);
const SingleEntryPlugin = webpack.EntryPlugin ?? webpack.SingleEntryPlugin;
new SingleEntryPlugin(compiler.context, entry, 'main').apply(childCompiler);
plugins.forEach((plugin) => plugin.apply(childCompiler));
childCompiler.runAsChild((err?: Error) => callback(err));
}
}
export class AddWorkerEntryPointPlugin implements webpack.WebpackPluginInstance {
private readonly options: IAddWorkerEntryPointPluginOptions;
constructor({ id, entry, filename, chunkFilename = undefined, plugins }: IAddWorkerEntryPointPluginOptions) {
this.options = { id, entry, filename, chunkFilename, plugins };
}
apply(compiler: webpack.Compiler) {
const webpack = compiler.webpack ?? require('webpack');
const compilerHook = getCompilerHook(compiler, this.options);
const majorVersion = webpack.version.split('.')[0]
if (parseInt(majorVersion) < 4) {
(<any>compiler).plugin('make', compilerHook);
} else {
compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
}
}
}