source: imaps-frontend/node_modules/webpack/lib/wasm/EnableWasmLoadingPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.1 KB
Line 
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>>} */
13const enabledTypes = new WeakMap();
14
15/**
16 * @param {Compiler} compiler compiler instance
17 * @returns {Set<WasmLoadingType>} enabled types
18 */
19const getEnabledTypes = compiler => {
20 let set = enabledTypes.get(compiler);
21 if (set === undefined) {
22 set = new Set();
23 enabledTypes.set(compiler, set);
24 }
25 return set;
26};
27
28class EnableWasmLoadingPlugin {
29 /**
30 * @param {WasmLoadingType} type library type that should be available
31 */
32 constructor(type) {
33 this.type = type;
34 }
35
36 /**
37 * @param {Compiler} compiler the compiler instance
38 * @param {WasmLoadingType} type type of library
39 * @returns {void}
40 */
41 static setEnabled(compiler, type) {
42 getEnabledTypes(compiler).add(type);
43 }
44
45 /**
46 * @param {Compiler} compiler the compiler instance
47 * @param {WasmLoadingType} type type of library
48 * @returns {void}
49 */
50 static checkEnabled(compiler, type) {
51 if (!getEnabledTypes(compiler).has(type)) {
52 throw new Error(
53 `Library type "${type}" is not enabled. ` +
54 "EnableWasmLoadingPlugin need to be used to enable this type of wasm loading. " +
55 'This usually happens through the "output.enabledWasmLoadingTypes" option. ' +
56 'If you are using a function as entry which sets "wasmLoading", you need to add all potential library types to "output.enabledWasmLoadingTypes". ' +
57 `These types are enabled: ${Array.from(
58 getEnabledTypes(compiler)
59 ).join(", ")}`
60 );
61 }
62 }
63
64 /**
65 * Apply the plugin
66 * @param {Compiler} compiler the compiler instance
67 * @returns {void}
68 */
69 apply(compiler) {
70 const { type } = this;
71
72 // Only enable once
73 const enabled = getEnabledTypes(compiler);
74 if (enabled.has(type)) return;
75 enabled.add(type);
76
77 if (typeof type === "string") {
78 switch (type) {
79 case "fetch": {
80 if (compiler.options.experiments.syncWebAssembly) {
81 // TODO webpack 6 remove FetchCompileWasmPlugin
82 const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
83 new FetchCompileWasmPlugin({
84 mangleImports: compiler.options.optimization.mangleWasmImports
85 }).apply(compiler);
86 }
87
88 if (compiler.options.experiments.asyncWebAssembly) {
89 const FetchCompileAsyncWasmPlugin = require("../web/FetchCompileAsyncWasmPlugin");
90 new FetchCompileAsyncWasmPlugin().apply(compiler);
91 }
92
93 break;
94 }
95 case "async-node": {
96 if (compiler.options.experiments.syncWebAssembly) {
97 // TODO webpack 6 remove ReadFileCompileWasmPlugin
98 const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
99 new ReadFileCompileWasmPlugin({
100 mangleImports: compiler.options.optimization.mangleWasmImports,
101 import:
102 compiler.options.output.environment.module &&
103 compiler.options.output.environment.dynamicImport
104 }).apply(compiler);
105 }
106
107 if (compiler.options.experiments.asyncWebAssembly) {
108 const ReadFileCompileAsyncWasmPlugin = require("../node/ReadFileCompileAsyncWasmPlugin");
109 new ReadFileCompileAsyncWasmPlugin({
110 import:
111 compiler.options.output.environment.module &&
112 compiler.options.output.environment.dynamicImport
113 }).apply(compiler);
114 }
115
116 break;
117 }
118 case "universal": {
119 const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
120 new UniversalCompileAsyncWasmPlugin().apply(compiler);
121 break;
122 }
123 default:
124 throw new Error(`Unsupported wasm loading type ${type}.
125Plugins which provide custom wasm loading types must call EnableWasmLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
126 }
127 } else {
128 // TODO support plugin instances here
129 // apply them to the compiler
130 }
131 }
132}
133
134module.exports = EnableWasmLoadingPlugin;
Note: See TracBrowser for help on using the repository browser.