ImportDependency.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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("../ModuleGraph")} ModuleGraph */
  14. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  15. class ImportDependency extends ModuleDependency {
  16. /**
  17. * @param {string} request the request
  18. * @param {[number, number]} range expression range
  19. * @param {string[][]=} referencedExports list of referenced exports
  20. */
  21. constructor(request, range, referencedExports) {
  22. super(request);
  23. this.range = range;
  24. this.referencedExports = referencedExports;
  25. }
  26. get type() {
  27. return "import()";
  28. }
  29. get category() {
  30. return "esm";
  31. }
  32. /**
  33. * Returns list of exports referenced by this dependency
  34. * @param {ModuleGraph} moduleGraph module graph
  35. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  36. * @returns {(string[] | ReferencedExport)[]} referenced exports
  37. */
  38. getReferencedExports(moduleGraph, runtime) {
  39. return this.referencedExports
  40. ? this.referencedExports.map(e => ({
  41. name: e,
  42. canMangle: false
  43. }))
  44. : Dependency.EXPORTS_OBJECT_REFERENCED;
  45. }
  46. serialize(context) {
  47. context.write(this.range);
  48. context.write(this.referencedExports);
  49. super.serialize(context);
  50. }
  51. deserialize(context) {
  52. this.range = context.read();
  53. this.referencedExports = context.read();
  54. super.deserialize(context);
  55. }
  56. }
  57. makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
  58. ImportDependency.Template = class ImportDependencyTemplate extends (
  59. ModuleDependency.Template
  60. ) {
  61. /**
  62. * @param {Dependency} dependency the dependency for which the template should be applied
  63. * @param {ReplaceSource} source the current replace source which can be modified
  64. * @param {DependencyTemplateContext} templateContext the context object
  65. * @returns {void}
  66. */
  67. apply(
  68. dependency,
  69. source,
  70. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  71. ) {
  72. const dep = /** @type {ImportDependency} */ (dependency);
  73. const block = /** @type {AsyncDependenciesBlock} */ (
  74. moduleGraph.getParentBlock(dep)
  75. );
  76. const content = runtimeTemplate.moduleNamespacePromise({
  77. chunkGraph,
  78. block: block,
  79. module: moduleGraph.getModule(dep),
  80. request: dep.request,
  81. strict: module.buildMeta.strictHarmonyModule,
  82. message: "import()",
  83. runtimeRequirements
  84. });
  85. source.replace(dep.range[0], dep.range[1] - 1, content);
  86. }
  87. };
  88. module.exports = ImportDependency;