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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 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
8const { WEBASSEMBLY_MODULE_TYPE_ASYNC } = require("../ModuleTypeConstants");
9const RuntimeGlobals = require("../RuntimeGlobals");
10const AsyncWasmCompileRuntimeModule = require("../wasm-async/AsyncWasmCompileRuntimeModule");
11const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
12
13/** @typedef {import("../Chunk")} Chunk */
14/** @typedef {import("../Compiler")} Compiler */
15
16const PLUGIN_NAME = "FetchCompileAsyncWasmPlugin";
17
18/**
19 * Enables asynchronous WebAssembly loading through `fetch` for environments
20 * that can instantiate fetched binaries at runtime.
21 */
22class FetchCompileAsyncWasmPlugin {
23 /**
24 * Registers compilation hooks that attach the async fetch-based wasm runtime
25 * to chunks containing async WebAssembly modules.
26 * @param {Compiler} compiler the compiler instance
27 * @returns {void}
28 */
29 apply(compiler) {
30 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
31 const globalWasmLoading = compilation.outputOptions.wasmLoading;
32 /**
33 * Determines whether the chunk should load async WebAssembly binaries
34 * through the `fetch` backend.
35 * @param {Chunk} chunk chunk
36 * @returns {boolean} true, if wasm loading is enabled for the chunk
37 */
38 const isEnabledForChunk = (chunk) => {
39 const options = chunk.getEntryOptions();
40 const wasmLoading =
41 options && options.wasmLoading !== undefined
42 ? options.wasmLoading
43 : globalWasmLoading;
44 return wasmLoading === "fetch";
45 };
46 /**
47 * Generates the runtime expression that downloads the emitted wasm
48 * binary for an async WebAssembly module.
49 * @param {string} path path to the wasm file
50 * @returns {string} code to load the wasm file
51 */
52 const generateLoadBinaryCode = (path) =>
53 `fetch(${RuntimeGlobals.publicPath} + ${path})`;
54
55 compilation.hooks.runtimeRequirementInTree
56 .for(RuntimeGlobals.instantiateWasm)
57 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
58 if (!isEnabledForChunk(chunk)) return;
59 if (
60 !chunkGraph.hasModuleInGraph(
61 chunk,
62 (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
63 )
64 ) {
65 return;
66 }
67 set.add(RuntimeGlobals.publicPath);
68 compilation.addRuntimeModule(
69 chunk,
70 new AsyncWasmLoadingRuntimeModule({
71 generateLoadBinaryCode,
72 supportsStreaming: true
73 })
74 );
75 });
76
77 compilation.hooks.runtimeRequirementInTree
78 .for(RuntimeGlobals.compileWasm)
79 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
80 if (!isEnabledForChunk(chunk)) return;
81 if (
82 !chunkGraph.hasModuleInGraph(
83 chunk,
84 (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
85 )
86 ) {
87 return;
88 }
89 set.add(RuntimeGlobals.publicPath);
90 compilation.addRuntimeModule(
91 chunk,
92 new AsyncWasmCompileRuntimeModule({
93 generateLoadBinaryCode,
94 supportsStreaming: true
95 })
96 );
97 });
98 });
99 }
100}
101
102module.exports = FetchCompileAsyncWasmPlugin;
Note: See TracBrowser for help on using the repository browser.