[79a0317] | 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 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 13 | /** @typedef {import("../Module")} Module */
|
---|
| 14 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
| 15 |
|
---|
| 16 | class WasmFinalizeExportsPlugin {
|
---|
| 17 | /**
|
---|
| 18 | * Apply the plugin
|
---|
| 19 | * @param {Compiler} compiler the compiler instance
|
---|
| 20 | * @returns {void}
|
---|
| 21 | */
|
---|
| 22 | apply(compiler) {
|
---|
| 23 | compiler.hooks.compilation.tap("WasmFinalizeExportsPlugin", compilation => {
|
---|
| 24 | compilation.hooks.finishModules.tap(
|
---|
| 25 | "WasmFinalizeExportsPlugin",
|
---|
| 26 | 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)
|
---|
| 76 | .loc
|
---|
| 77 | )}.`
|
---|
| 78 | );
|
---|
| 79 | error.module = module;
|
---|
| 80 | compilation.errors.push(error);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | );
|
---|
| 89 | });
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | module.exports = WasmFinalizeExportsPlugin;
|
---|