UNPKG

3.22 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getDefaultFilename = getDefaultFilename;
7exports.getDefaultChunkFilename = getDefaultChunkFilename;
8exports.getExternalsType = getExternalsType;
9exports.workerGenerator = workerGenerator;
10exports.sourceURLWebpackRegex = exports.sourceMappingURLRegex = void 0;
11
12var _loaderUtils = require("loader-utils");
13
14function getDefaultFilename(filename) {
15 if (typeof filename === "function") {
16 return filename;
17 }
18
19 return filename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2");
20}
21
22function getDefaultChunkFilename(chunkFilename) {
23 return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2");
24}
25
26function getExternalsType(compilerOptions) {
27 // For webpack@4
28 if (compilerOptions.output.libraryTarget) {
29 return compilerOptions.output.libraryTarget;
30 } // For webpack@5
31
32
33 if (compilerOptions.externalsType) {
34 return compilerOptions.externalsType;
35 }
36
37 if (compilerOptions.output.library) {
38 return compilerOptions.output.library.type;
39 }
40
41 if (compilerOptions.output.module) {
42 return "module";
43 }
44
45 return "var";
46}
47
48function workerGenerator(loaderContext, workerFilename, workerSource, options) {
49 let workerConstructor;
50 let workerOptions;
51
52 if (typeof options.worker === "undefined") {
53 workerConstructor = "Worker";
54 } else if (typeof options.worker === "string") {
55 workerConstructor = options.worker;
56 } else {
57 ({
58 type: workerConstructor,
59 options: workerOptions
60 } = options.worker);
61 }
62
63 const esModule = typeof options.esModule !== "undefined" ? options.esModule : true;
64 const fnName = `${workerConstructor}_fn`;
65
66 if (options.inline) {
67 const InlineWorkerPath = (0, _loaderUtils.stringifyRequest)(loaderContext, `!!${require.resolve("./runtime/inline.js")}`);
68 let fallbackWorkerPath;
69
70 if (options.inline === "fallback") {
71 fallbackWorkerPath = `__webpack_public_path__ + ${JSON.stringify(workerFilename)}`;
72 }
73
74 return `
75${esModule ? `import worker from ${InlineWorkerPath};` : `var worker = require(${InlineWorkerPath});`}
76
77${esModule ? "export default" : "module.exports ="} function ${fnName}() {\n return worker(${JSON.stringify(workerSource)}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify(workerOptions)}, ${fallbackWorkerPath});\n}\n`;
78 }
79
80 return `${esModule ? "export default" : "module.exports ="} function ${fnName}() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(workerFilename)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ""});\n}\n`;
81} // Matches only the last occurrence of sourceMappingURL
82
83
84const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*(.*?(?=[\s'"]|\\n|\*\/|$)(?:\\n)?)\s*/;
85/* eslint-disable prefer-template */
86
87const sourceMappingURLRegex = RegExp("(?:" + "/\\*" + "(?:\\s*\r?\n(?://)?)?" + "(?:" + innerRegex.source + ")" + "\\s*" + "\\*/" + "|" + "//(?:" + innerRegex.source + ")" + ")" + "\\s*");
88exports.sourceMappingURLRegex = sourceMappingURLRegex;
89const sourceURLWebpackRegex = RegExp("\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n");
90/* eslint-enable prefer-template */
91
92exports.sourceURLWebpackRegex = sourceURLWebpackRegex;
\No newline at end of file