ExternalModuleDependency.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const CachedConstDependency = require("./CachedConstDependency");
  8. const ExternalModuleInitFragment = require("./ExternalModuleInitFragment");
  9. /** @import { ReplaceSource } from "webpack-sources" */
  10. /** @import Dependency from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @import { Range } from "../javascript/JavascriptParser" */
  13. /**
  14. * @import {
  15. * ArrayImportSpecifiers
  16. * } from "../dependencies/ExternalModuleInitFragment"
  17. */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<(string | ArrayImportSpecifiers | undefined)[]>} ObjectDeserializerContext */
  19. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<(string | ArrayImportSpecifiers | undefined)[]>} ObjectSerializerContext */
  20. class ExternalModuleDependency extends CachedConstDependency {
  21. /**
  22. * Creates an instance of ExternalModuleDependency.
  23. * @param {string} module module
  24. * @param {ArrayImportSpecifiers} importSpecifiers import specifiers
  25. * @param {string | undefined} defaultImport default import
  26. * @param {string} expression expression
  27. * @param {Range | null} range range
  28. * @param {string} identifier identifier
  29. * @param {number=} place place where we inject the expression
  30. */
  31. constructor(
  32. module,
  33. importSpecifiers,
  34. defaultImport,
  35. expression,
  36. range,
  37. identifier,
  38. place = CachedConstDependency.PLACE_MODULE
  39. ) {
  40. super(expression, range, identifier, place);
  41. /** @type {string} */
  42. this.importedModule = module;
  43. /** @type {ArrayImportSpecifiers} */
  44. this.specifiers = importSpecifiers;
  45. /** @type {string | undefined} */
  46. this.default = defaultImport;
  47. }
  48. /**
  49. * Create hash update.
  50. * @returns {string} hash update
  51. */
  52. _createHashUpdate() {
  53. return `${this.importedModule}${JSON.stringify(this.specifiers)}${
  54. this.default || "null"
  55. }${super._createHashUpdate()}`;
  56. }
  57. /**
  58. * Serializes this instance into the provided serializer context.
  59. * @param {ObjectSerializerContext} context context
  60. */
  61. serialize(context) {
  62. super.serialize(context);
  63. const { write } = context;
  64. write(this.importedModule);
  65. write(this.specifiers);
  66. write(this.default);
  67. }
  68. /**
  69. * Restores this instance from the provided deserializer context.
  70. * @param {ObjectDeserializerContext} context context
  71. */
  72. deserialize(context) {
  73. super.deserialize(context);
  74. const { read } = context;
  75. this.importedModule = /** @type {string} */ (read());
  76. this.specifiers = /** @type {ArrayImportSpecifiers} */ (read());
  77. this.default = /** @type {string | undefined} */ (read());
  78. }
  79. }
  80. makeSerializable(
  81. ExternalModuleDependency,
  82. "webpack/lib/dependencies/ExternalModuleDependency"
  83. );
  84. ExternalModuleDependency.Template = class ExternalModuleDependencyTemplate extends (
  85. CachedConstDependency.Template
  86. ) {
  87. /**
  88. * Applies the plugin by registering its hooks on the compiler.
  89. * @param {Dependency} dependency the dependency for which the template should be applied
  90. * @param {ReplaceSource} source the current replace source which can be modified
  91. * @param {DependencyTemplateContext} templateContext the context object
  92. * @returns {void}
  93. */
  94. apply(dependency, source, templateContext) {
  95. super.apply(dependency, source, templateContext);
  96. const dep = /** @type {ExternalModuleDependency} */ (dependency);
  97. const { chunkInitFragments, runtimeTemplate } = templateContext;
  98. chunkInitFragments.push(
  99. new ExternalModuleInitFragment(
  100. `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
  101. dep.importedModule
  102. }`,
  103. dep.specifiers,
  104. dep.default
  105. )
  106. );
  107. }
  108. };
  109. module.exports = ExternalModuleDependency;