ImportDependency.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const ModuleDependency = require("./ModuleDependency");
  9. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  10. /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
  11. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  12. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  13. /** @typedef {import("../Module")} Module */
  14. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  15. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  16. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  17. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  18. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  19. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  20. class ImportDependency extends ModuleDependency {
  21. /**
  22. * @param {string} request the request
  23. * @param {Range} range expression range
  24. * @param {(string[][] | null)=} referencedExports list of referenced exports
  25. */
  26. constructor(request, range, referencedExports) {
  27. super(request);
  28. this.range = range;
  29. this.referencedExports = referencedExports;
  30. }
  31. get type() {
  32. return "import()";
  33. }
  34. get category() {
  35. return "esm";
  36. }
  37. /**
  38. * Returns list of exports referenced by this dependency
  39. * @param {ModuleGraph} moduleGraph module graph
  40. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  41. * @returns {(string[] | ReferencedExport)[]} referenced exports
  42. */
  43. getReferencedExports(moduleGraph, runtime) {
  44. return this.referencedExports
  45. ? this.referencedExports.map(e => ({
  46. name: e,
  47. canMangle: false
  48. }))
  49. : Dependency.EXPORTS_OBJECT_REFERENCED;
  50. }
  51. /**
  52. * @param {ObjectSerializerContext} context context
  53. */
  54. serialize(context) {
  55. context.write(this.range);
  56. context.write(this.referencedExports);
  57. super.serialize(context);
  58. }
  59. /**
  60. * @param {ObjectDeserializerContext} context context
  61. */
  62. deserialize(context) {
  63. this.range = context.read();
  64. this.referencedExports = context.read();
  65. super.deserialize(context);
  66. }
  67. }
  68. makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
  69. ImportDependency.Template = class ImportDependencyTemplate extends (
  70. ModuleDependency.Template
  71. ) {
  72. /**
  73. * @param {Dependency} dependency the dependency for which the template should be applied
  74. * @param {ReplaceSource} source the current replace source which can be modified
  75. * @param {DependencyTemplateContext} templateContext the context object
  76. * @returns {void}
  77. */
  78. apply(
  79. dependency,
  80. source,
  81. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  82. ) {
  83. const dep = /** @type {ImportDependency} */ (dependency);
  84. const block = /** @type {AsyncDependenciesBlock} */ (
  85. moduleGraph.getParentBlock(dep)
  86. );
  87. const content = runtimeTemplate.moduleNamespacePromise({
  88. chunkGraph,
  89. block: block,
  90. module: /** @type {Module} */ (moduleGraph.getModule(dep)),
  91. request: dep.request,
  92. strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
  93. message: "import()",
  94. runtimeRequirements
  95. });
  96. source.replace(dep.range[0], dep.range[1] - 1, content);
  97. }
  98. };
  99. module.exports = ImportDependency;