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

Method apply

lib/wasm-sync/WasmFinalizeExportsPlugin.js:25–89  ·  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

23 * @returns {void}
24 */
25 apply(compiler) {
26 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
27 compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => {
28 for (const module of modules) {
29 // 1. if a WebAssembly module
30 if (module.type.startsWith("webassembly") === true) {
31 const jsIncompatibleExports =
32 /** @type {SyncWasmModuleBuildMeta} */
33 (module.buildMeta).jsIncompatibleExports;
34
35 if (jsIncompatibleExports === undefined) {
36 continue;
37 }
38
39 for (const connection of compilation.moduleGraph.getIncomingConnections(
40 module
41 )) {
42 // 2. is active and referenced by a non-WebAssembly module
43 if (
44 connection.isTargetActive(undefined) &&
45 /** @type {Module} */
46 (connection.originModule).type.startsWith("webassembly") ===
47 false
48 ) {
49 const referencedExports =
50 compilation.getDependencyReferencedExports(
51 /** @type {Dependency} */ (connection.dependency),
52 undefined
53 );
54
55 for (const info of referencedExports) {
56 const names = Array.isArray(info) ? info : info.name;
57 if (names.length === 0) continue;
58 const name = names[0];
59 if (typeof name === "object") continue;
60 // 3. and uses a func with an incompatible JS signature
61 if (
62 Object.prototype.hasOwnProperty.call(
63 jsIncompatibleExports,
64 name
65 )
66 ) {
67 // 4. error
68 const error = new UnsupportedWebAssemblyFeatureError(
69 `Export "${name}" with ${jsIncompatibleExports[name]} can only be used for direct wasm to wasm dependencies\n` +
70 `It's used from ${
71 /** @type {Module} */
72 (connection.originModule).readableIdentifier(
73 compilation.requestShortener
74 )
75 } at ${formatLocation(
76 /** @type {Dependency} */ (connection.dependency).loc
77 )}.`
78 );
79 error.module = module;
80 compilation.errors.push(error);
81 }
82 }

Callers

nothing calls this directly

Calls 9

formatLocationFunction · 0.85
tapMethod · 0.80
isTargetActiveMethod · 0.80
isArrayMethod · 0.80
callMethod · 0.45
readableIdentifierMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected