WebAssemblyImportDependency.js 3.0 KB

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