ExternalModuleDependency.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../Dependency")} Dependency */
  11. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
  14. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  15. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  16. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  17. /** @typedef {import("../util/Hash")} Hash */
  18. class ExternalModuleDependency extends CachedConstDependency {
  19. /**
  20. * @param {string} module module
  21. * @param {{ name: string, value: string }[]} importSpecifiers import specifiers
  22. * @param {string | undefined} defaultImport default import
  23. * @param {string} expression expression
  24. * @param {Range} range range
  25. * @param {string} identifier identifier
  26. */
  27. constructor(
  28. module,
  29. importSpecifiers,
  30. defaultImport,
  31. expression,
  32. range,
  33. identifier
  34. ) {
  35. super(expression, range, identifier);
  36. this.importedModule = module;
  37. this.specifiers = importSpecifiers;
  38. this.default = defaultImport;
  39. }
  40. /**
  41. * @returns {string} hash update
  42. */
  43. _createHashUpdate() {
  44. return `${this.importedModule}${JSON.stringify(this.specifiers)}${
  45. this.default || "null"
  46. }${super._createHashUpdate()}`;
  47. }
  48. /**
  49. * @param {ObjectSerializerContext} context context
  50. */
  51. serialize(context) {
  52. super.serialize(context);
  53. const { write } = context;
  54. write(this.importedModule);
  55. write(this.specifiers);
  56. write(this.default);
  57. }
  58. /**
  59. * @param {ObjectDeserializerContext} context context
  60. */
  61. deserialize(context) {
  62. super.deserialize(context);
  63. const { read } = context;
  64. this.importedModule = read();
  65. this.specifiers = read();
  66. this.default = read();
  67. }
  68. }
  69. makeSerializable(
  70. ExternalModuleDependency,
  71. "webpack/lib/dependencies/ExternalModuleDependency"
  72. );
  73. ExternalModuleDependency.Template = class ExternalModuleDependencyTemplate extends (
  74. CachedConstDependency.Template
  75. ) {
  76. /**
  77. * @param {Dependency} dependency the dependency for which the template should be applied
  78. * @param {ReplaceSource} source the current replace source which can be modified
  79. * @param {DependencyTemplateContext} templateContext the context object
  80. * @returns {void}
  81. */
  82. apply(dependency, source, templateContext) {
  83. super.apply(dependency, source, templateContext);
  84. const dep = /** @type {ExternalModuleDependency} */ (dependency);
  85. const { chunkInitFragments, runtimeTemplate } = templateContext;
  86. chunkInitFragments.push(
  87. new ExternalModuleInitFragment(
  88. `${runtimeTemplate.supportNodePrefixForCoreModules() ? "node:" : ""}${
  89. dep.importedModule
  90. }`,
  91. dep.specifiers,
  92. dep.default
  93. )
  94. );
  95. }
  96. };
  97. module.exports = ExternalModuleDependency;