WebAssemblyImportDependency.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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").ReferencedExports} ReferencedExports */
  11. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  12. /** @typedef {import("../WebpackError")} WebpackError */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  15. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  16. class WebAssemblyImportDependency extends ModuleDependency {
  17. /**
  18. * Creates an instance of WebAssemblyImportDependency.
  19. * @param {string} request the request
  20. * @param {string} name the imported name
  21. * @param {ModuleImportDescription} description the WASM ast node
  22. * @param {false | string} onlyDirectImport if only direct imports are allowed
  23. */
  24. constructor(request, name, description, onlyDirectImport) {
  25. super(request);
  26. /** @type {string} */
  27. this.name = name;
  28. /** @type {ModuleImportDescription} */
  29. this.description = description;
  30. /** @type {false | string} */
  31. this.onlyDirectImport = onlyDirectImport;
  32. }
  33. get type() {
  34. return "wasm import";
  35. }
  36. get category() {
  37. return "wasm";
  38. }
  39. /**
  40. * Returns list of exports referenced by this dependency
  41. * @param {ModuleGraph} moduleGraph module graph
  42. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  43. * @returns {ReferencedExports} referenced exports
  44. */
  45. getReferencedExports(moduleGraph, runtime) {
  46. return [[this.name]];
  47. }
  48. /**
  49. * Returns errors.
  50. * @param {ModuleGraph} moduleGraph module graph
  51. * @returns {WebpackError[] | null | undefined} errors
  52. */
  53. getErrors(moduleGraph) {
  54. const module = moduleGraph.getModule(this);
  55. if (
  56. this.onlyDirectImport &&
  57. module &&
  58. !module.type.startsWith("webassembly")
  59. ) {
  60. return [
  61. new UnsupportedWebAssemblyFeatureError(
  62. `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
  63. )
  64. ];
  65. }
  66. }
  67. /**
  68. * Serializes this instance into the provided serializer context.
  69. * @param {ObjectSerializerContext} context context
  70. */
  71. serialize(context) {
  72. const { write } = context;
  73. write(this.name);
  74. write(this.description);
  75. write(this.onlyDirectImport);
  76. super.serialize(context);
  77. }
  78. /**
  79. * Restores this instance from the provided deserializer context.
  80. * @param {ObjectDeserializerContext} context context
  81. */
  82. deserialize(context) {
  83. const { read } = context;
  84. this.name = read();
  85. this.description = read();
  86. this.onlyDirectImport = read();
  87. super.deserialize(context);
  88. }
  89. }
  90. makeSerializable(
  91. WebAssemblyImportDependency,
  92. "webpack/lib/dependencies/WebAssemblyImportDependency"
  93. );
  94. module.exports = WebAssemblyImportDependency;