ImportWeakDependency.js 2.2 KB

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