WasmFinalizeExportsPlugin.js 2.0 KB

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