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