WebAssemblyImportDependency.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const UnsupportedWebAssemblyFeatureError = require("../wasm-sync/UnsupportedWebAssemblyFeatureError");
  8. const ModuleDependency = require("./ModuleDependency");
  9. /** @typedef {import("@webassemblyjs/ast").ModuleImportDescription} ModuleImportDescription */
  10. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  11. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  12. /** @typedef {import("../WebpackError")} WebpackError */
  13. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  14. class WebAssemblyImportDependency extends ModuleDependency {
  15. /**
  16. * @param {string} request the request
  17. * @param {string} name the imported name
  18. * @param {ModuleImportDescription} description the WASM ast node
  19. * @param {false | string} onlyDirectImport if only direct imports are allowed
  20. */
  21. constructor(request, name, description, onlyDirectImport) {
  22. super(request);
  23. /** @type {string} */
  24. this.name = name;
  25. /** @type {ModuleImportDescription} */
  26. this.description = description;
  27. /** @type {false | string} */
  28. this.onlyDirectImport = onlyDirectImport;
  29. }
  30. get type() {
  31. return "wasm import";
  32. }
  33. get category() {
  34. return "wasm";
  35. }
  36. /**
  37. * Returns list of exports referenced by this dependency
  38. * @param {ModuleGraph} moduleGraph module graph
  39. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  40. * @returns {(string[] | ReferencedExport)[]} referenced exports
  41. */
  42. getReferencedExports(moduleGraph, runtime) {
  43. return [[this.name]];
  44. }
  45. /**
  46. * Returns errors
  47. * @param {ModuleGraph} moduleGraph module graph
  48. * @returns {WebpackError[]} errors
  49. */
  50. getErrors(moduleGraph) {
  51. const module = moduleGraph.getModule(this);
  52. if (
  53. this.onlyDirectImport &&
  54. module &&
  55. !module.type.startsWith("webassembly")
  56. ) {
  57. return [
  58. new UnsupportedWebAssemblyFeatureError(
  59. `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
  60. )
  61. ];
  62. }
  63. }
  64. serialize(context) {
  65. const { write } = context;
  66. write(this.name);
  67. write(this.description);
  68. write(this.onlyDirectImport);
  69. super.serialize(context);
  70. }
  71. deserialize(context) {
  72. const { read } = context;
  73. this.name = read();
  74. this.description = read();
  75. this.onlyDirectImport = read();
  76. super.deserialize(context);
  77. }
  78. }
  79. makeSerializable(
  80. WebAssemblyImportDependency,
  81. "webpack/lib/dependencies/WebAssemblyImportDependency"
  82. );
  83. module.exports = WebAssemblyImportDependency;