source: frontend/node_modules/webpack/lib/node/ReadFileCompileWasmPlugin.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.9 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 Template = require("../Template");
11const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule");
12
13/** @typedef {import("../Chunk")} Chunk */
14/** @typedef {import("../Compiler")} Compiler */
15
16/**
17 * Defines the read file compile wasm plugin options type used by this module.
18 * @typedef {object} ReadFileCompileWasmPluginOptions
19 * @property {boolean=} mangleImports mangle imports
20 * @property {boolean=} import use import?
21 */
22
23const PLUGIN_NAME = "ReadFileCompileWasmPlugin";
24
25class ReadFileCompileWasmPlugin {
26 /**
27 * Creates an instance of ReadFileCompileWasmPlugin.
28 * @param {ReadFileCompileWasmPluginOptions=} options options object
29 */
30 constructor(options = {}) {
31 /** @type {ReadFileCompileWasmPluginOptions} */
32 this.options = options;
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 wasm plugin is enabled for chunk.
45 * @param {Chunk} chunk chunk
46 * @returns {boolean} true, when 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.options.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 `var { readFile } = require(${compilation.runtimeTemplate.renderNodePrefixForCoreModule("fs")});`,
83 `var { join } = require(${compilation.runtimeTemplate.renderNodePrefixForCoreModule("path")});`,
84 "",
85 "try {",
86 Template.indent([
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.ensureChunkHandlers)
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_SYNC
111 )
112 ) {
113 return;
114 }
115 set.add(RuntimeGlobals.moduleCache);
116 compilation.addRuntimeModule(
117 chunk,
118 new WasmChunkLoadingRuntimeModule({
119 generateLoadBinaryCode,
120 supportsStreaming: false,
121 mangleImports: this.options.mangleImports,
122 runtimeRequirements: set
123 })
124 );
125 });
126 });
127 }
128}
129
130module.exports = ReadFileCompileWasmPlugin;
Note: See TracBrowser for help on using the repository browser.