MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/node/ReadFileCompileWasmPlugin.js:40–127  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

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 ]),

Callers

nothing calls this directly

Calls 7

tapMethod · 0.80
indentMethod · 0.80
addRuntimeModuleMethod · 0.80
asStringMethod · 0.45
hasModuleInGraphMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected