ExternalModuleInitFragmentDependency.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const DependencyTemplate = require("../DependencyTemplate");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const ExternalModuleInitFragment = require("./ExternalModuleInitFragment");
  9. const NullDependency = require("./NullDependency");
  10. /** @import { ReplaceSource } from "webpack-sources" */
  11. /** @import Dependency from "../Dependency" */
  12. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  13. /**
  14. * @import {
  15. * ArrayImportSpecifiers
  16. * } from "../dependencies/ExternalModuleInitFragment"
  17. */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, ArrayImportSpecifiers, string | undefined]>} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, ArrayImportSpecifiers, string | undefined]>} ObjectSerializerContext */
  20. class ExternalModuleInitFragmentDependency extends NullDependency {
  21. /**
  22. * Creates an instance of ExternalModuleInitFragmentDependency.
  23. * @param {string} module module
  24. * @param {ArrayImportSpecifiers} importSpecifiers import specifiers
  25. * @param {string | undefined} defaultImport default import
  26. */
  27. constructor(module, importSpecifiers, defaultImport) {
  28. super();
  29. /** @type {string} */
  30. this.importedModule = module;
  31. /** @type {ArrayImportSpecifiers} */
  32. this.specifiers = importSpecifiers;
  33. /** @type {string | undefined} */
  34. this.default = defaultImport;
  35. }
  36. /**
  37. * Serializes this instance into the provided serializer context.
  38. * @param {ObjectSerializerContext} context context
  39. */
  40. serialize(context) {
  41. context
  42. .write(this.importedModule)
  43. .write(this.specifiers)
  44. .write(this.default);
  45. }
  46. /**
  47. * Restores this instance from the provided deserializer context.
  48. * @param {ObjectDeserializerContext} context context
  49. */
  50. deserialize(context) {
  51. this.importedModule = context.read();
  52. const c1 = context.rest;
  53. this.specifiers = c1.read();
  54. this.default = c1.rest.read();
  55. }
  56. }
  57. makeSerializable(
  58. ExternalModuleInitFragmentDependency,
  59. "webpack/lib/dependencies/ExternalModuleInitFragmentDependency"
  60. );
  61. ExternalModuleInitFragmentDependency.Template = class ExternalModuleInitFragmentDependencyTemplate extends (
  62. DependencyTemplate
  63. ) {
  64. /**
  65. * Applies the plugin by registering its hooks on the compiler.
  66. * @param {Dependency} dependency the dependency for which the template should be applied
  67. * @param {ReplaceSource} source the current replace source which can be modified
  68. * @param {DependencyTemplateContext} templateContext the context object
  69. * @returns {void}
  70. */
  71. apply(dependency, source, templateContext) {
  72. const dep =
  73. /** @type {ExternalModuleInitFragmentDependency} */
  74. (dependency);
  75. const { chunkInitFragments, runtimeTemplate } = templateContext;
  76. chunkInitFragments.push(
  77. new ExternalModuleInitFragment(
  78. `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
  79. dep.importedModule
  80. }`,
  81. dep.specifiers,
  82. dep.default
  83. )
  84. );
  85. }
  86. };
  87. module.exports = ExternalModuleInitFragmentDependency;