AsyncWasmModule.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const NormalModule = require("../NormalModule");
  6. const makeSerializable = require("../util/makeSerializable");
  7. /** @import Module from "../Module" */
  8. /** @import { NormalModuleCreateData } from "../NormalModule" */
  9. /** @import { ImportPhaseName } from "../dependencies/ImportPhase" */
  10. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[ImportPhaseName | undefined]>} ObjectDeserializerContext */
  11. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[ImportPhaseName | undefined]>} ObjectSerializerContext */
  12. /**
  13. * Module class for `webassembly/async` modules. Wasm-specific properties should live here instead of `NormalModule`.
  14. */
  15. class AsyncWasmModule extends NormalModule {
  16. /**
  17. * @param {NormalModuleCreateData & { phase: ImportPhaseName | undefined }} options options object
  18. */
  19. constructor(options) {
  20. super(options);
  21. /** @type {ImportPhaseName | undefined} */
  22. this.phase = options.phase;
  23. }
  24. /**
  25. * Returns the unique identifier used to reference this module.
  26. * @returns {string} a unique identifier of the module
  27. */
  28. identifier() {
  29. let str = super.identifier();
  30. if (this.phase) {
  31. str = `${str}|${this.phase}`;
  32. }
  33. return str;
  34. }
  35. /**
  36. * Assuming this module is in the cache. Update the (cached) module with
  37. * the fresh module from the factory. Usually updates internal references
  38. * and properties.
  39. * @param {Module} module fresh module
  40. * @returns {void}
  41. */
  42. updateCacheModule(module) {
  43. super.updateCacheModule(module);
  44. const m = /** @type {AsyncWasmModule} */ (module);
  45. this.phase = m.phase;
  46. }
  47. /**
  48. * Serializes this instance into the provided serializer context.
  49. * @param {ObjectSerializerContext} context context
  50. */
  51. serialize(context) {
  52. context.write(this.phase);
  53. super.serialize(context);
  54. }
  55. /**
  56. * Restores this instance from the provided deserializer context.
  57. * @param {ObjectDeserializerContext} context context
  58. */
  59. deserialize(context) {
  60. this.phase = context.read();
  61. super.deserialize(context.rest);
  62. }
  63. }
  64. makeSerializable(AsyncWasmModule, "webpack/lib/wasm-async/AsyncWasmModule");
  65. module.exports = AsyncWasmModule;