ImportWeakDependency.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 ImportWeakDependency 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. 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. * @param {Dependency} dependency the dependency for which the template should be applied
  41. * @param {ReplaceSource} source the current replace source which can be modified
  42. * @param {DependencyTemplateContext} templateContext the context object
  43. * @returns {void}
  44. */
  45. apply(
  46. dependency,
  47. source,
  48. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  49. ) {
  50. const dep = /** @type {ImportWeakDependency} */ (dependency);
  51. const content = runtimeTemplate.moduleNamespacePromise({
  52. chunkGraph,
  53. module: /** @type {Module} */ (moduleGraph.getModule(dep)),
  54. request: dep.request,
  55. strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
  56. message: "import() weak",
  57. weak: true,
  58. runtimeRequirements
  59. });
  60. source.replace(dep.range[0], dep.range[1] - 1, content);
  61. }
  62. };
  63. module.exports = ImportWeakDependency;