source: frontend/node_modules/webpack/lib/wasm-sync/WasmFinalizeExportsPlugin.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: 2.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const formatLocation = require("../util/formatLocation");
9const UnsupportedWebAssemblyFeatureError = require("./UnsupportedWebAssemblyFeatureError");
10
11/** @typedef {import("../Compiler")} Compiler */
12/** @typedef {import("../Dependency")} Dependency */
13/** @typedef {import("../Module")} Module */
14/** @typedef {import("../Module").BuildMeta} BuildMeta */
15
16const PLUGIN_NAME = "WasmFinalizeExportsPlugin";
17
18class WasmFinalizeExportsPlugin {
19 /**
20 * Applies the plugin by registering its hooks on the compiler.
21 * @param {Compiler} compiler the compiler instance
22 * @returns {void}
23 */
24 apply(compiler) {
25 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
26 compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => {
27 for (const module of modules) {
28 // 1. if a WebAssembly module
29 if (module.type.startsWith("webassembly") === true) {
30 const jsIncompatibleExports =
31 /** @type {BuildMeta} */
32 (module.buildMeta).jsIncompatibleExports;
33
34 if (jsIncompatibleExports === undefined) {
35 continue;
36 }
37
38 for (const connection of compilation.moduleGraph.getIncomingConnections(
39 module
40 )) {
41 // 2. is active and referenced by a non-WebAssembly module
42 if (
43 connection.isTargetActive(undefined) &&
44 /** @type {Module} */
45 (connection.originModule).type.startsWith("webassembly") ===
46 false
47 ) {
48 const referencedExports =
49 compilation.getDependencyReferencedExports(
50 /** @type {Dependency} */ (connection.dependency),
51 undefined
52 );
53
54 for (const info of referencedExports) {
55 const names = Array.isArray(info) ? info : info.name;
56 if (names.length === 0) continue;
57 const name = names[0];
58 if (typeof name === "object") continue;
59 // 3. and uses a func with an incompatible JS signature
60 if (
61 Object.prototype.hasOwnProperty.call(
62 jsIncompatibleExports,
63 name
64 )
65 ) {
66 // 4. error
67 const error = new UnsupportedWebAssemblyFeatureError(
68 `Export "${name}" with ${jsIncompatibleExports[name]} can only be used for direct wasm to wasm dependencies\n` +
69 `It's used from ${
70 /** @type {Module} */
71 (connection.originModule).readableIdentifier(
72 compilation.requestShortener
73 )
74 } at ${formatLocation(
75 /** @type {Dependency} */ (connection.dependency).loc
76 )}.`
77 );
78 error.module = module;
79 compilation.errors.push(error);
80 }
81 }
82 }
83 }
84 }
85 }
86 });
87 });
88 }
89}
90
91module.exports = WasmFinalizeExportsPlugin;
Note: See TracBrowser for help on using the repository browser.