| 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").WasmLoadingType} WasmLoadingType */
|
|---|
| 9 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {Set<WasmLoadingType>} WasmLoadingTypes */
|
|---|
| 12 |
|
|---|
| 13 | /** @type {WeakMap<Compiler, Set<WasmLoadingType>>} */
|
|---|
| 14 | const enabledTypes = new WeakMap();
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Returns the set of wasm loading backends that have already been enabled for
|
|---|
| 18 | * the compiler.
|
|---|
| 19 | * @param {Compiler} compiler compiler instance
|
|---|
| 20 | * @returns {WasmLoadingTypes} enabled types
|
|---|
| 21 | */
|
|---|
| 22 | const getEnabledTypes = (compiler) => {
|
|---|
| 23 | let set = enabledTypes.get(compiler);
|
|---|
| 24 | if (set === undefined) {
|
|---|
| 25 | /** @type {WasmLoadingTypes} */
|
|---|
| 26 | set = new Set();
|
|---|
| 27 | enabledTypes.set(compiler, set);
|
|---|
| 28 | }
|
|---|
| 29 | return set;
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Validates and enables named wasm loading backends by applying the plugin
|
|---|
| 34 | * implementations that provide their runtime support.
|
|---|
| 35 | */
|
|---|
| 36 | class EnableWasmLoadingPlugin {
|
|---|
| 37 | /**
|
|---|
| 38 | * Stores the wasm loading backend name that should be enabled for the
|
|---|
| 39 | * compiler.
|
|---|
| 40 | * @param {WasmLoadingType} type library type that should be available
|
|---|
| 41 | */
|
|---|
| 42 | constructor(type) {
|
|---|
| 43 | /** @type {WasmLoadingType} */
|
|---|
| 44 | this.type = type;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Marks a custom or built-in wasm loading type as enabled for the compiler
|
|---|
| 49 | * without applying additional built-in behavior.
|
|---|
| 50 | * @param {Compiler} compiler the compiler instance
|
|---|
| 51 | * @param {WasmLoadingType} type type of library
|
|---|
| 52 | * @returns {void}
|
|---|
| 53 | */
|
|---|
| 54 | static setEnabled(compiler, type) {
|
|---|
| 55 | getEnabledTypes(compiler).add(type);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Verifies that a wasm loading type has been enabled before code generation
|
|---|
| 60 | * attempts to use it.
|
|---|
| 61 | * @param {Compiler} compiler the compiler instance
|
|---|
| 62 | * @param {WasmLoadingType} type type of library
|
|---|
| 63 | * @returns {void}
|
|---|
| 64 | */
|
|---|
| 65 | static checkEnabled(compiler, type) {
|
|---|
| 66 | if (!getEnabledTypes(compiler).has(type)) {
|
|---|
| 67 | throw new Error(
|
|---|
| 68 | `Library type "${type}" is not enabled. ` +
|
|---|
| 69 | "EnableWasmLoadingPlugin need to be used to enable this type of wasm loading. " +
|
|---|
| 70 | 'This usually happens through the "output.enabledWasmLoadingTypes" option. ' +
|
|---|
| 71 | 'If you are using a function as entry which sets "wasmLoading", you need to add all potential library types to "output.enabledWasmLoadingTypes". ' +
|
|---|
| 72 | `These types are enabled: ${[...getEnabledTypes(compiler)].join(", ")}`
|
|---|
| 73 | );
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Enables the requested wasm loading backend once and applies the
|
|---|
| 79 | * environment-specific plugins that provide its parser, generator, and
|
|---|
| 80 | * runtime support.
|
|---|
| 81 | * @param {Compiler} compiler the compiler instance
|
|---|
| 82 | * @returns {void}
|
|---|
| 83 | */
|
|---|
| 84 | apply(compiler) {
|
|---|
| 85 | const { type } = this;
|
|---|
| 86 |
|
|---|
| 87 | // Only enable once
|
|---|
| 88 | const enabled = getEnabledTypes(compiler);
|
|---|
| 89 | if (enabled.has(type)) return;
|
|---|
| 90 | enabled.add(type);
|
|---|
| 91 |
|
|---|
| 92 | if (typeof type === "string") {
|
|---|
| 93 | switch (type) {
|
|---|
| 94 | case "fetch": {
|
|---|
| 95 | if (compiler.options.experiments.syncWebAssembly) {
|
|---|
| 96 | const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
|
|---|
| 97 |
|
|---|
| 98 | new FetchCompileWasmPlugin({
|
|---|
| 99 | mangleImports: compiler.options.optimization.mangleWasmImports
|
|---|
| 100 | }).apply(compiler);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (compiler.options.experiments.asyncWebAssembly) {
|
|---|
| 104 | const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
|
|---|
| 105 |
|
|---|
| 106 | new FetchCompileAsyncWasmPlugin().apply(compiler);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | break;
|
|---|
| 110 | }
|
|---|
| 111 | case "async-node": {
|
|---|
| 112 | if (compiler.options.experiments.syncWebAssembly) {
|
|---|
| 113 | const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
|
|---|
| 114 |
|
|---|
| 115 | new ReadFileCompileWasmPlugin({
|
|---|
| 116 | mangleImports: compiler.options.optimization.mangleWasmImports,
|
|---|
| 117 | import:
|
|---|
| 118 | compiler.options.output.module &&
|
|---|
| 119 | compiler.options.output.environment.dynamicImport
|
|---|
| 120 | }).apply(compiler);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | if (compiler.options.experiments.asyncWebAssembly) {
|
|---|
| 124 | const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
|
|---|
| 125 |
|
|---|
| 126 | new ReadFileCompileAsyncWasmPlugin({
|
|---|
| 127 | import:
|
|---|
| 128 | compiler.options.output.module &&
|
|---|
| 129 | compiler.options.output.environment.dynamicImport
|
|---|
| 130 | }).apply(compiler);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | break;
|
|---|
| 134 | }
|
|---|
| 135 | case "universal": {
|
|---|
| 136 | if (compiler.options.experiments.syncWebAssembly) {
|
|---|
| 137 | throw new Error(
|
|---|
| 138 | "Universal wasm loading type is only supported by asynchronous web assembly."
|
|---|
| 139 | );
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | if (compiler.options.experiments.asyncWebAssembly) {
|
|---|
| 143 | const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
|
|---|
| 144 |
|
|---|
| 145 | new UniversalCompileAsyncWasmPlugin().apply(compiler);
|
|---|
| 146 | }
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | default:
|
|---|
| 150 | throw new Error(`Unsupported wasm loading type ${type}.
|
|---|
| 151 | Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
|
|---|
| 152 | }
|
|---|
| 153 | } else {
|
|---|
| 154 | // TODO support plugin instances here
|
|---|
| 155 | // apply them to the compiler
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | module.exports = EnableWasmLoadingPlugin;
|
|---|