source: frontend/node_modules/webpack/lib/node/ReadFileCompileAsyncWasmPlugin.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: 4.4 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_ASYNC } = require("../ModuleTypeConstants");
9const RuntimeGlobals = require("../RuntimeGlobals");
10const Template = require("../Template");
11const AsyncWasmCompileRuntimeModule = require("../wasm-async/AsyncWasmCompileRuntimeModule");
12const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule");
13
14/** @typedef {import("../Chunk")} Chunk */
15/** @typedef {import("../Compiler")} Compiler */
16
17/**
18 * Defines the read file compile async wasm plugin options type used by this module.
19 * @typedef {object} ReadFileCompileAsyncWasmPluginOptions
20 * @property {boolean=} import use import?
21 */
22
23const PLUGIN_NAME = "ReadFileCompileAsyncWasmPlugin";
24
25class ReadFileCompileAsyncWasmPlugin {
26 /**
27 * Creates an instance of ReadFileCompileAsyncWasmPlugin.
28 * @param {ReadFileCompileAsyncWasmPluginOptions=} options options object
29 */
30 constructor({ import: useImport = false } = {}) {
31 /** @type {boolean} */
32 this._import = useImport;
33 }
34
35 /**
36 * Applies the plugin by registering its hooks on the compiler.
37 * @param {Compiler} compiler the compiler instance
38 * @returns {void}
39 */
40 apply(compiler) {
41 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
42 const globalWasmLoading = compilation.outputOptions.wasmLoading;
43 /**
44 * Checks whether this read file compile async wasm plugin is enabled for chunk.
45 * @param {Chunk} chunk chunk
46 * @returns {boolean} true, if wasm loading is enabled for the chunk
47 */
48 const isEnabledForChunk = (chunk) => {
49 const options = chunk.getEntryOptions();
50 const wasmLoading =
51 options && options.wasmLoading !== undefined
52 ? options.wasmLoading
53 : globalWasmLoading;
54 return wasmLoading === "async-node";
55 };
56
57 /**
58 * @type {(path: string) => string} callback to generate code to load the wasm file
59 */
60 const generateLoadBinaryCode = this._import
61 ? (path) =>
62 Template.asString([
63 "Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
64 Template.indent([
65 `readFile(new URL(${path}, ${compilation.outputOptions.importMetaName}.url), (err, buffer) => {`,
66 Template.indent([
67 "if (err) return reject(err);",
68 "",
69 "// Fake fetch response",
70 "resolve({",
71 Template.indent(["arrayBuffer() { return buffer; }"]),
72 "});"
73 ]),
74 "});"
75 ]),
76 "}))"
77 ])
78 : (path) =>
79 Template.asString([
80 "new Promise(function (resolve, reject) {",
81 Template.indent([
82 "try {",
83 Template.indent([
84 `var { readFile } = require(${compilation.runtimeTemplate.renderNodePrefixForCoreModule("fs")});`,
85 `var { join } = require(${compilation.runtimeTemplate.renderNodePrefixForCoreModule("path")});`,
86 "",
87 `readFile(join(__dirname, ${path}), function(err, buffer){`,
88 Template.indent([
89 "if (err) return reject(err);",
90 "",
91 "// Fake fetch response",
92 "resolve({",
93 Template.indent(["arrayBuffer() { return buffer; }"]),
94 "});"
95 ]),
96 "});"
97 ]),
98 "} catch (err) { reject(err); }"
99 ]),
100 "})"
101 ]);
102
103 compilation.hooks.runtimeRequirementInTree
104 .for(RuntimeGlobals.instantiateWasm)
105 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
106 if (!isEnabledForChunk(chunk)) return;
107 if (
108 !chunkGraph.hasModuleInGraph(
109 chunk,
110 (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
111 )
112 ) {
113 return;
114 }
115 compilation.addRuntimeModule(
116 chunk,
117 new AsyncWasmLoadingRuntimeModule({
118 generateLoadBinaryCode,
119 supportsStreaming: false
120 })
121 );
122 });
123
124 compilation.hooks.runtimeRequirementInTree
125 .for(RuntimeGlobals.compileWasm)
126 .tap(PLUGIN_NAME, (chunk, set, { chunkGraph }) => {
127 if (!isEnabledForChunk(chunk)) return;
128 if (
129 !chunkGraph.hasModuleInGraph(
130 chunk,
131 (m) => m.type === WEBASSEMBLY_MODULE_TYPE_ASYNC
132 )
133 ) {
134 return;
135 }
136 compilation.addRuntimeModule(
137 chunk,
138 new AsyncWasmCompileRuntimeModule({
139 generateLoadBinaryCode,
140 supportsStreaming: false
141 })
142 );
143 });
144 });
145 }
146}
147
148module.exports = ReadFileCompileAsyncWasmPlugin;
Note: See TracBrowser for help on using the repository browser.