WebAssemblyImportDependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /** @import { ModuleImportDescription } from "@webassemblyjs/ast" */
  10. /** @import { ReferencedExports } from "../Dependency" */
  11. /** @import ModuleGraph from "../ModuleGraph" */
  12. /** @import WebpackError from "../errors/WebpackError" */
  13. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, ModuleImportDescription, false | string]>} ObjectDeserializerContext */
  14. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, ModuleImportDescription, false | string]>} ObjectSerializerContext */
  15. /** @import { RuntimeSpec } from "../util/runtime" */
  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. // wasm import glue needs the real binding, never an inlined literal
  47. return [{ name: [this.name], canInline: false }];
  48. }
  49. /**
  50. * Returns errors.
  51. * @param {ModuleGraph} moduleGraph module graph
  52. * @returns {WebpackError[] | null | undefined} errors
  53. */
  54. getErrors(moduleGraph) {
  55. const module = moduleGraph.getModule(this);
  56. if (
  57. this.onlyDirectImport &&
  58. module &&
  59. !module.type.startsWith("webassembly")
  60. ) {
  61. return [
  62. new UnsupportedWebAssemblyFeatureError(
  63. `Import "${this.name}" from "${this.request}" with ${this.onlyDirectImport} can only be used for direct wasm to wasm dependencies`
  64. )
  65. ];
  66. }
  67. }
  68. /**
  69. * Serializes this instance into the provided serializer context.
  70. * @param {ObjectSerializerContext} context context
  71. */
  72. serialize(context) {
  73. context
  74. .write(this.name)
  75. .write(this.description)
  76. .write(this.onlyDirectImport);
  77. super.serialize(context);
  78. }
  79. /**
  80. * Restores this instance from the provided deserializer context.
  81. * @param {ObjectDeserializerContext} context context
  82. */
  83. deserialize(context) {
  84. this.name = context.read();
  85. const c1 = context.rest;
  86. this.description = c1.read();
  87. const c2 = c1.rest;
  88. this.onlyDirectImport = c2.read();
  89. super.deserialize(c2.rest);
  90. }
  91. }
  92. makeSerializable(
  93. WebAssemblyImportDependency,
  94. "webpack/lib/dependencies/WebAssemblyImportDependency"
  95. );
  96. module.exports = WebAssemblyImportDependency;