source: trip-planner-front/node_modules/webpack/lib/node/ReadFileCompileAsyncWasmPlugin.js@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[6a3a178]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const Template = require("../Template");
10const AsyncWasmChunkLoadingRuntimeModule = require("../wasm-async/AsyncWasmChunkLoadingRuntimeModule");
11
12/** @typedef {import("../Compiler")} Compiler */
13
14class ReadFileCompileAsyncWasmPlugin {
15 constructor({ type = "async-node", import: useImport = false } = {}) {
16 this._type = type;
17 this._import = useImport;
18 }
19 /**
20 * Apply the plugin
21 * @param {Compiler} compiler the compiler instance
22 * @returns {void}
23 */
24 apply(compiler) {
25 compiler.hooks.thisCompilation.tap(
26 "ReadFileCompileAsyncWasmPlugin",
27 compilation => {
28 const globalWasmLoading = compilation.outputOptions.wasmLoading;
29 const isEnabledForChunk = chunk => {
30 const options = chunk.getEntryOptions();
31 const wasmLoading =
32 options && options.wasmLoading !== undefined
33 ? options.wasmLoading
34 : globalWasmLoading;
35 return wasmLoading === this._type;
36 };
37 const generateLoadBinaryCode = this._import
38 ? path =>
39 Template.asString([
40 "Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {",
41 Template.indent([
42 `readFile(new URL(${path}, import.meta.url), (err, buffer) => {`,
43 Template.indent([
44 "if (err) return reject(err);",
45 "",
46 "// Fake fetch response",
47 "resolve({",
48 Template.indent(["arrayBuffer() { return buffer; }"]),
49 "});"
50 ]),
51 "});"
52 ]),
53 "}))"
54 ])
55 : path =>
56 Template.asString([
57 "new Promise(function (resolve, reject) {",
58 Template.indent([
59 "try {",
60 Template.indent([
61 "var { readFile } = require('fs');",
62 "var { join } = require('path');",
63 "",
64 `readFile(join(__dirname, ${path}), function(err, buffer){`,
65 Template.indent([
66 "if (err) return reject(err);",
67 "",
68 "// Fake fetch response",
69 "resolve({",
70 Template.indent(["arrayBuffer() { return buffer; }"]),
71 "});"
72 ]),
73 "});"
74 ]),
75 "} catch (err) { reject(err); }"
76 ]),
77 "})"
78 ]);
79
80 compilation.hooks.runtimeRequirementInTree
81 .for(RuntimeGlobals.instantiateWasm)
82 .tap("ReadFileCompileAsyncWasmPlugin", (chunk, set) => {
83 if (!isEnabledForChunk(chunk)) return;
84 const chunkGraph = compilation.chunkGraph;
85 if (
86 !chunkGraph.hasModuleInGraph(
87 chunk,
88 m => m.type === "webassembly/async"
89 )
90 ) {
91 return;
92 }
93 set.add(RuntimeGlobals.publicPath);
94 compilation.addRuntimeModule(
95 chunk,
96 new AsyncWasmChunkLoadingRuntimeModule({
97 generateLoadBinaryCode,
98 supportsStreaming: false
99 })
100 );
101 });
102 }
103 );
104 }
105}
106
107module.exports = ReadFileCompileAsyncWasmPlugin;
Note: See TracBrowser for help on using the repository browser.