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. const importedNames = ref.importedNames;
  29. if (Array.isArray(importedNames)) {
  30. importedNames.forEach(name => {
  31. // 3. and uses a func with an incompatible JS signature
  32. if (
  33. Object.prototype.hasOwnProperty.call(
  34. jsIncompatibleExports,
  35. name
  36. )
  37. ) {
  38. // 4. error
  39. /** @type {any} */
  40. const error = new UnsupportedWebAssemblyFeatureError(
  41. `Export "${name}" with ${
  42. jsIncompatibleExports[name]
  43. } can only be used for direct wasm to wasm dependencies`
  44. );
  45. error.module = module;
  46. error.origin = reason.module;
  47. error.originLoc = reason.dependency.loc;
  48. error.dependencies = [reason.dependency];
  49. compilation.errors.push(error);
  50. }
  51. });
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. );
  59. });
  60. }
  61. }
  62. module.exports = WasmFinalizeExportsPlugin;