1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const enabledTypes = new WeakMap();
|
14 |
|
15 | const getEnabledTypes = compiler => {
|
16 | let set = enabledTypes.get(compiler);
|
17 | if (set === undefined) {
|
18 | set = new Set();
|
19 | enabledTypes.set(compiler, set);
|
20 | }
|
21 | return set;
|
22 | };
|
23 |
|
24 | class EnableWasmLoadingPlugin {
|
25 | |
26 |
|
27 |
|
28 | constructor(type) {
|
29 | this.type = type;
|
30 | }
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | static setEnabled(compiler, type) {
|
38 | getEnabledTypes(compiler).add(type);
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | static checkEnabled(compiler, type) {
|
47 | if (!getEnabledTypes(compiler).has(type)) {
|
48 | throw new Error(
|
49 | `Library type "${type}" is not enabled. ` +
|
50 | "EnableWasmLoadingPlugin need to be used to enable this type of wasm loading. " +
|
51 | 'This usually happens through the "output.enabledWasmLoadingTypes" option. ' +
|
52 | 'If you are using a function as entry which sets "wasmLoading", you need to add all potential library types to "output.enabledWasmLoadingTypes". ' +
|
53 | "These types are enabled: " +
|
54 | Array.from(getEnabledTypes(compiler)).join(", ")
|
55 | );
|
56 | }
|
57 | }
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 | apply(compiler) {
|
65 | const { type } = this;
|
66 |
|
67 |
|
68 | const enabled = getEnabledTypes(compiler);
|
69 | if (enabled.has(type)) return;
|
70 | enabled.add(type);
|
71 |
|
72 | if (typeof type === "string") {
|
73 | switch (type) {
|
74 | case "fetch": {
|
75 |
|
76 | const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
|
77 | const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
|
78 | new FetchCompileWasmPlugin({
|
79 | mangleImports: compiler.options.optimization.mangleWasmImports
|
80 | }).apply(compiler);
|
81 | new FetchCompileAsyncWasmPlugin().apply(compiler);
|
82 | break;
|
83 | }
|
84 | case "async-node": {
|
85 |
|
86 | const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
|
87 |
|
88 | const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
|
89 | new ReadFileCompileWasmPlugin({
|
90 | mangleImports: compiler.options.optimization.mangleWasmImports
|
91 | }).apply(compiler);
|
92 | new ReadFileCompileAsyncWasmPlugin({ type }).apply(compiler);
|
93 | break;
|
94 | }
|
95 | case "async-node-module": {
|
96 |
|
97 | const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
|
98 | new ReadFileCompileAsyncWasmPlugin({ type, import: true }).apply(
|
99 | compiler
|
100 | );
|
101 | break;
|
102 | }
|
103 | case "universal":
|
104 | throw new Error(
|
105 | "Universal WebAssembly Loading is not implemented yet"
|
106 | );
|
107 | default:
|
108 | throw new Error(`Unsupported wasm loading type ${type}.
|
109 | Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
|
110 | }
|
111 | } else {
|
112 |
|
113 |
|
114 | }
|
115 | }
|
116 | }
|
117 |
|
118 | module.exports = EnableWasmLoadingPlugin;
|