ImportEagerDependency.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const makeSerializable = require("../util/makeSerializable");
  7. const ImportDependency = require("./ImportDependency");
  8. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import Dependency, { RawReferencedExports } from "../Dependency" */
  10. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  11. /** @import Module, { BuildMeta } from "../Module" */
  12. /** @import { ImportAttributes, Range } from "../javascript/JavascriptParser" */
  13. /** @import { ImportPhaseType } from "./ImportPhase" */
  14. class ImportEagerDependency extends ImportDependency {
  15. /**
  16. * Creates an instance of ImportEagerDependency.
  17. * @param {string} request the request
  18. * @param {Range} range expression range
  19. * @param {RawReferencedExports | null} referencedExports list of referenced exports
  20. * @param {ImportPhaseType} phase import phase
  21. * @param {ImportAttributes=} attributes import attributes
  22. */
  23. constructor(request, range, referencedExports, phase, attributes) {
  24. super(request, range, referencedExports, phase, attributes);
  25. }
  26. get type() {
  27. return "import() eager";
  28. }
  29. get category() {
  30. return "esm";
  31. }
  32. }
  33. makeSerializable(
  34. ImportEagerDependency,
  35. "webpack/lib/dependencies/ImportEagerDependency"
  36. );
  37. ImportEagerDependency.Template = class ImportEagerDependencyTemplate extends (
  38. ImportDependency.Template
  39. ) {
  40. /**
  41. * Applies the plugin by registering its hooks on the compiler.
  42. * @param {Dependency} dependency the dependency for which the template should be applied
  43. * @param {ReplaceSource} source the current replace source which can be modified
  44. * @param {DependencyTemplateContext} templateContext the context object
  45. * @returns {void}
  46. */
  47. apply(
  48. dependency,
  49. source,
  50. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  51. ) {
  52. const dep = /** @type {ImportEagerDependency} */ (dependency);
  53. const content = runtimeTemplate.moduleNamespacePromise({
  54. chunkGraph,
  55. module: /** @type {Module} */ (moduleGraph.getModule(dep)),
  56. request: dep.request,
  57. strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
  58. message: "import() eager",
  59. dependency: dep,
  60. runtimeRequirements
  61. });
  62. source.replace(dep.range[0], dep.range[1] - 1, content);
  63. }
  64. };
  65. module.exports = ImportEagerDependency;