ImportWeakDependency.js 2.3 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. /** @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 ImportWeakDependency extends ImportDependency {
  15. /**
  16. * Creates an instance of ImportWeakDependency.
  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. /** @type {boolean} */
  26. this.weak = true;
  27. }
  28. get type() {
  29. return "import() weak";
  30. }
  31. }
  32. makeSerializable(
  33. ImportWeakDependency,
  34. "webpack/lib/dependencies/ImportWeakDependency"
  35. );
  36. ImportWeakDependency.Template = class ImportDependencyTemplate extends (
  37. ImportDependency.Template
  38. ) {
  39. /**
  40. * Applies the plugin by registering its hooks on the compiler.
  41. * @param {Dependency} dependency the dependency for which the template should be applied
  42. * @param {ReplaceSource} source the current replace source which can be modified
  43. * @param {DependencyTemplateContext} templateContext the context object
  44. * @returns {void}
  45. */
  46. apply(
  47. dependency,
  48. source,
  49. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  50. ) {
  51. const dep = /** @type {ImportWeakDependency} */ (dependency);
  52. const content = runtimeTemplate.moduleNamespacePromise({
  53. chunkGraph,
  54. module: /** @type {Module} */ (moduleGraph.getModule(dep)),
  55. request: dep.request,
  56. strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
  57. message: "import() weak",
  58. weak: true,
  59. dependency: dep,
  60. runtimeRequirements
  61. });
  62. source.replace(dep.range[0], dep.range[1] - 1, content);
  63. }
  64. };
  65. module.exports = ImportWeakDependency;