ImportEagerDependency.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  9. /** @typedef {import("../Dependency")} Dependency */
  10. /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
  11. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  12. /** @typedef {import("../Module")} Module */
  13. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  14. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  15. /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
  16. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  17. class ImportEagerDependency extends ImportDependency {
  18. /**
  19. * @param {string} request the request
  20. * @param {Range} range expression range
  21. * @param {(string[][] | null)=} referencedExports list of referenced exports
  22. * @param {ImportAttributes=} attributes import attributes
  23. */
  24. constructor(request, range, referencedExports, attributes) {
  25. super(request, range, referencedExports, attributes);
  26. }
  27. get type() {
  28. return "import() eager";
  29. }
  30. get category() {
  31. return "esm";
  32. }
  33. }
  34. makeSerializable(
  35. ImportEagerDependency,
  36. "webpack/lib/dependencies/ImportEagerDependency"
  37. );
  38. ImportEagerDependency.Template = class ImportEagerDependencyTemplate extends (
  39. ImportDependency.Template
  40. ) {
  41. /**
  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. runtimeRequirements
  60. });
  61. source.replace(dep.range[0], dep.range[1] - 1, content);
  62. }
  63. };
  64. module.exports = ImportEagerDependency;