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

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 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_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 * @typedef {object} FetchCompileWasmPluginOptions
17 * @property {boolean} [mangleImports] mangle imports
18 */
19
20// TODO webpack 6 remove
21
22const PLUGIN_NAME = "FetchCompileWasmPlugin";
23
24class FetchCompileWasmPlugin {
25 /**
26 * @param {FetchCompileWasmPluginOptions} [options] options
27 */
28 constructor(options = {}) {
29 this.options = options;
30 }
31
32 /**
33 * Apply the plugin
34 * @param {Compiler} compiler the compiler instance
35 * @returns {void}
36 */
37 apply(compiler) {
38 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
39 const globalWasmLoading = compilation.outputOptions.wasmLoading;
40 /**
41 * @param {Chunk} chunk chunk
42 * @returns {boolean} true, if wasm loading is enabled for the chunk
43 */
44 const isEnabledForChunk = chunk => {
45 const options = chunk.getEntryOptions();
46 const wasmLoading =
47 options && options.wasmLoading !== undefined
48 ? options.wasmLoading
49 : globalWasmLoading;
50 return wasmLoading === "fetch";
51 };
52 /**
53 * @param {string} path path to the wasm file
54 * @returns {string} code to load the wasm file
55 */
56 const generateLoadBinaryCode = path =>
57 `fetch(${RuntimeGlobals.publicPath} + ${path})`;
58
59 compilation.hooks.runtimeRequirementInTree
60 .for(RuntimeGlobals.ensureChunkHandlers)
61 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
62 if (!isEnabledForChunk(chunk)) return;
63 if (
64 !chunkGraph.hasModuleInGraph(
65 chunk,
66 m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC
67 )
68 ) {
69 return;
70 }
71 set.add(RuntimeGlobals.moduleCache);
72 set.add(RuntimeGlobals.publicPath);
73 compilation.addRuntimeModule(
74 chunk,
75 new WasmChunkLoadingRuntimeModule({
76 generateLoadBinaryCode,
77 supportsStreaming: true,
78 mangleImports: this.options.mangleImports,
79 runtimeRequirements: set
80 })
81 );
82 });
83 });
84 }
85}
86
87module.exports = FetchCompileWasmPlugin;
Note: See TracBrowser for help on using the repository browser.