ImportWeakDependency.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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("../ModuleGraph")} ModuleGraph */
  13. class ImportWeakDependency extends ImportDependency {
  14. /**
  15. * @param {string} request the request
  16. * @param {[number, number]} range expression range
  17. * @param {string[][]=} referencedExports list of referenced exports
  18. */
  19. constructor(request, range, referencedExports) {
  20. super(request, range, referencedExports);
  21. this.weak = true;
  22. }
  23. get type() {
  24. return "import() weak";
  25. }
  26. }
  27. makeSerializable(
  28. ImportWeakDependency,
  29. "webpack/lib/dependencies/ImportWeakDependency"
  30. );
  31. ImportWeakDependency.Template = class ImportDependencyTemplate extends (
  32. ImportDependency.Template
  33. ) {
  34. /**
  35. * @param {Dependency} dependency the dependency for which the template should be applied
  36. * @param {ReplaceSource} source the current replace source which can be modified
  37. * @param {DependencyTemplateContext} templateContext the context object
  38. * @returns {void}
  39. */
  40. apply(
  41. dependency,
  42. source,
  43. { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
  44. ) {
  45. const dep = /** @type {ImportWeakDependency} */ (dependency);
  46. const content = runtimeTemplate.moduleNamespacePromise({
  47. chunkGraph,
  48. module: moduleGraph.getModule(dep),
  49. request: dep.request,
  50. strict: module.buildMeta.strictHarmonyModule,
  51. message: "import() weak",
  52. weak: true,
  53. runtimeRequirements
  54. });
  55. source.replace(dep.range[0], dep.range[1] - 1, content);
  56. }
  57. };
  58. module.exports = ImportWeakDependency;