[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
| 9 | /** @typedef {import("../../declarations/WebpackOptions").WasmLoadingType} WasmLoadingType */
|
---|
| 10 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 11 |
|
---|
| 12 | /** @type {WeakMap<Compiler, Set<WasmLoadingType>>} */
|
---|
| 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 | * @param {WasmLoadingType} type library type that should be available
|
---|
| 27 | */
|
---|
| 28 | constructor(type) {
|
---|
| 29 | this.type = type;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @param {Compiler} compiler the compiler instance
|
---|
| 34 | * @param {WasmLoadingType} type type of library
|
---|
| 35 | * @returns {void}
|
---|
| 36 | */
|
---|
| 37 | static setEnabled(compiler, type) {
|
---|
| 38 | getEnabledTypes(compiler).add(type);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param {Compiler} compiler the compiler instance
|
---|
| 43 | * @param {WasmLoadingType} type type of library
|
---|
| 44 | * @returns {void}
|
---|
| 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 | * Apply the plugin
|
---|
| 61 | * @param {Compiler} compiler the compiler instance
|
---|
| 62 | * @returns {void}
|
---|
| 63 | */
|
---|
| 64 | apply(compiler) {
|
---|
| 65 | const { type } = this;
|
---|
| 66 |
|
---|
| 67 | // Only enable once
|
---|
| 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 | // TODO webpack 6 remove FetchCompileWasmPlugin
|
---|
| 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 | // TODO webpack 6 remove ReadFileCompileWasmPlugin
|
---|
| 86 | const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
|
---|
| 87 | // @ts-expect-error typescript bug for duplicate require
|
---|
| 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 | // @ts-expect-error typescript bug for duplicate require
|
---|
| 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 | // TODO support plugin instances here
|
---|
| 113 | // apply them to the compiler
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | module.exports = EnableWasmLoadingPlugin;
|
---|