1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loader;
|
7 | exports.pitch = pitch;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | var _loaderUtils = require("loader-utils");
|
12 |
|
13 | var _schemaUtils = require("schema-utils");
|
14 |
|
15 | var _NodeTargetPlugin = _interopRequireDefault(require("webpack/lib/node/NodeTargetPlugin"));
|
16 |
|
17 | var _SingleEntryPlugin = _interopRequireDefault(require("webpack/lib/SingleEntryPlugin"));
|
18 |
|
19 | var _WebWorkerTemplatePlugin = _interopRequireDefault(require("webpack/lib/webworker/WebWorkerTemplatePlugin"));
|
20 |
|
21 | var _ExternalsPlugin = _interopRequireDefault(require("webpack/lib/ExternalsPlugin"));
|
22 |
|
23 | var _options = _interopRequireDefault(require("./options.json"));
|
24 |
|
25 | var _supportWebpack = _interopRequireDefault(require("./supportWebpack5"));
|
26 |
|
27 | var _supportWebpack2 = _interopRequireDefault(require("./supportWebpack4"));
|
28 |
|
29 | var _utils = require("./utils");
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | let FetchCompileWasmPlugin;
|
34 | let FetchCompileAsyncWasmPlugin;
|
35 |
|
36 |
|
37 | const useWebpack5 = require("webpack/package.json").version.startsWith("5.");
|
38 |
|
39 | if (useWebpack5) {
|
40 |
|
41 | FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmPlugin");
|
42 |
|
43 | FetchCompileAsyncWasmPlugin = require("webpack/lib/web/FetchCompileAsyncWasmPlugin");
|
44 | } else {
|
45 |
|
46 | FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmTemplatePlugin");
|
47 | }
|
48 |
|
49 | function loader() {}
|
50 |
|
51 | function pitch(request) {
|
52 | this.cacheable(false);
|
53 | const options = (0, _loaderUtils.getOptions)(this);
|
54 | (0, _schemaUtils.validate)(_options.default, options, {
|
55 | name: "Worker Loader",
|
56 | baseDataPath: "options"
|
57 | });
|
58 | const workerContext = {};
|
59 | const compilerOptions = this._compiler.options || {};
|
60 | const filename = options.filename ? options.filename : (0, _utils.getDefaultFilename)(compilerOptions.output.filename);
|
61 | const chunkFilename = options.chunkFilename ? options.chunkFilename : (0, _utils.getDefaultChunkFilename)(compilerOptions.output.chunkFilename);
|
62 | const publicPath = options.publicPath ? options.publicPath : compilerOptions.output.publicPath;
|
63 | workerContext.options = {
|
64 | filename,
|
65 | chunkFilename,
|
66 | publicPath,
|
67 | globalObject: "self"
|
68 | };
|
69 | workerContext.compiler = this._compilation.createChildCompiler(`worker-loader ${request}`, workerContext.options);
|
70 | new _WebWorkerTemplatePlugin.default().apply(workerContext.compiler);
|
71 |
|
72 | if (this.target !== "webworker" && this.target !== "web") {
|
73 | new _NodeTargetPlugin.default().apply(workerContext.compiler);
|
74 | }
|
75 |
|
76 | if (FetchCompileWasmPlugin) {
|
77 | new FetchCompileWasmPlugin({
|
78 | mangleImports: compilerOptions.optimization.mangleWasmImports
|
79 | }).apply(workerContext.compiler);
|
80 | }
|
81 |
|
82 | if (FetchCompileAsyncWasmPlugin) {
|
83 | new FetchCompileAsyncWasmPlugin().apply(workerContext.compiler);
|
84 | }
|
85 |
|
86 | if (compilerOptions.externals) {
|
87 | new _ExternalsPlugin.default((0, _utils.getExternalsType)(compilerOptions), compilerOptions.externals).apply(workerContext.compiler);
|
88 | }
|
89 |
|
90 | new _SingleEntryPlugin.default(this.context, `!!${request}`, _path.default.parse(this.resourcePath).name).apply(workerContext.compiler);
|
91 | workerContext.request = request;
|
92 | const cb = this.async();
|
93 |
|
94 | if (workerContext.compiler.cache && typeof workerContext.compiler.cache.get === "function") {
|
95 | (0, _supportWebpack.default)(this, workerContext, options, cb);
|
96 | } else {
|
97 | (0, _supportWebpack2.default)(this, workerContext, options, cb);
|
98 | }
|
99 | } |
\ | No newline at end of file |