UNPKG

2.06 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4 */
5"use strict";
6
7const UnsupportedWebAssemblyFeatureError = require("./UnsupportedWebAssemblyFeatureError");
8
9class WasmFinalizeExportsPlugin {
10 apply(compiler) {
11 compiler.hooks.compilation.tap("WasmFinalizeExportsPlugin", compilation => {
12 compilation.hooks.finishModules.tap(
13 "WasmFinalizeExportsPlugin",
14 modules => {
15 for (const module of modules) {
16 // 1. if a WebAssembly module
17 if (module.type.startsWith("webassembly") === true) {
18 const jsIncompatibleExports =
19 module.buildMeta.jsIncompatibleExports;
20
21 if (jsIncompatibleExports === undefined) {
22 continue;
23 }
24
25 for (const reason of module.reasons) {
26 // 2. is referenced by a non-WebAssembly module
27 if (reason.module.type.startsWith("webassembly") === false) {
28 const ref = compilation.getDependencyReference(
29 reason.module,
30 reason.dependency
31 );
32
33 if (!ref) continue;
34
35 const importedNames = ref.importedNames;
36
37 if (Array.isArray(importedNames)) {
38 importedNames.forEach(name => {
39 // 3. and uses a func with an incompatible JS signature
40 if (
41 Object.prototype.hasOwnProperty.call(
42 jsIncompatibleExports,
43 name
44 )
45 ) {
46 // 4. error
47 /** @type {any} */
48 const error = new UnsupportedWebAssemblyFeatureError(
49 `Export "${name}" with ${
50 jsIncompatibleExports[name]
51 } can only be used for direct wasm to wasm dependencies`
52 );
53 error.module = module;
54 error.origin = reason.module;
55 error.originLoc = reason.dependency.loc;
56 error.dependencies = [reason.dependency];
57 compilation.errors.push(error);
58 }
59 });
60 }
61 }
62 }
63 }
64 }
65 }
66 );
67 });
68 }
69}
70
71module.exports = WasmFinalizeExportsPlugin;