ImportWeakDependency.js 2.6 KB

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