source: frontend/node_modules/webpack/lib/web/FetchCompileWasmPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[9af201e]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants");
9const RuntimeGlobals = require("../RuntimeGlobals");
10const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");
11
12/** @typedef {import("../Chunk")} Chunk */
13/** @typedef {import("../Compiler")} Compiler */
14
15/**
16 * Options that influence how synchronous WebAssembly modules are emitted for
17 * the fetch-based wasm loading runtime.
18 * @typedef {object} FetchCompileWasmPluginOptions
19 * @property {boolean=} mangleImports mangle imports
20 */
21
22const PLUGIN_NAME = "FetchCompileWasmPlugin";
23
24/**
25 * Enables synchronous WebAssembly chunk loading that fetches `.wasm` files and
26 * compiles them in browser-like environments.
27 */
28class FetchCompileWasmPlugin {
29 /**
30 * Stores options that affect generated synchronous WebAssembly runtime code.
31 * @param {FetchCompileWasmPluginOptions=} options options
32 */
33 constructor(options = {}) {
34 /** @type {FetchCompileWasmPluginOptions} */
35 this.options = options;
36 }
37
38 /**
39 * Registers compilation hooks that attach the fetch-based synchronous wasm
40 * runtime module to chunks containing sync WebAssembly modules.
41 * @param {Compiler} compiler the compiler instance
42 * @returns {void}
43 */
44 apply(compiler) {
45 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
46 const globalWasmLoading = compilation.outputOptions.wasmLoading;
47 /**
48 * Determines whether the chunk should load synchronous WebAssembly
49 * binaries through the `fetch` backend.
50 * @param {Chunk} chunk chunk
51 * @returns {boolean} true, if wasm loading is enabled for the chunk
52 */
53 const isEnabledForChunk = (chunk) => {
54 const options = chunk.getEntryOptions();
55 const wasmLoading =
56 options && options.wasmLoading !== undefined
57 ? options.wasmLoading
58 : globalWasmLoading;
59 return wasmLoading === "fetch";
60 };
61 /**
62 * Generates the runtime expression that downloads the emitted wasm
63 * binary for a module.
64 * @param {string} path path to the wasm file
65 * @returns {string} code to load the wasm file
66 */
67 const generateLoadBinaryCode = (path) =>
68 `fetch(${RuntimeGlobals.publicPath} + ${path})`;
69
70 compilation.hooks.runtimeRequirementInTree
71 .for(RuntimeGlobals.ensureChunkHandlers)
72 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
73 if (!isEnabledForChunk(chunk)) return;
74 if (
75 !chunkGraph.hasModuleInGraph(
76 chunk,
77 (m) => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC
78 )
79 ) {
80 return;
81 }
82 set.add(RuntimeGlobals.moduleCache);
83 set.add(RuntimeGlobals.publicPath);
84 compilation.addRuntimeModule(
85 chunk,
86 new WasmChunkLoadingRuntimeModule({
87 generateLoadBinaryCode,
88 supportsStreaming: true,
89 mangleImports: this.options.mangleImports,
90 runtimeRequirements: set
91 })
92 );
93 });
94 });
95 }
96}
97
98module.exports = FetchCompileWasmPlugin;
Note: See TracBrowser for help on using the repository browser.