ImportMetaResolveDependency.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const makeSerializable = require("../util/makeSerializable");
  8. const ModuleDependency = require("./ModuleDependency");
  9. const URLDependency = require("./URLDependency");
  10. /** @import { ReplaceSource } from "webpack-sources" */
  11. /** @import Dependency from "../Dependency" */
  12. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  13. // `import.meta.resolve(specifier)` is the string form of
  14. // `new URL(specifier, import.meta.url)`, so it reuses the URL asset pipeline
  15. // but renders to the resolved URL string (`.href`) over the whole call.
  16. class ImportMetaResolveDependency extends URLDependency {
  17. get type() {
  18. return "import.meta.resolve()";
  19. }
  20. }
  21. ImportMetaResolveDependency.Template = class ImportMetaResolveDependencyTemplate extends (
  22. ModuleDependency.Template
  23. ) {
  24. /**
  25. * Applies the plugin by registering its hooks on the compiler.
  26. * @param {Dependency} dependency the dependency for which the template should be applied
  27. * @param {ReplaceSource} source the current replace source which can be modified
  28. * @param {DependencyTemplateContext} templateContext the context object
  29. * @returns {void}
  30. */
  31. apply(dependency, source, templateContext) {
  32. const {
  33. chunkGraph,
  34. moduleGraph,
  35. runtimeRequirements,
  36. runtimeTemplate,
  37. runtime
  38. } = templateContext;
  39. const dep = /** @type {ImportMetaResolveDependency} */ (dependency);
  40. const connection = moduleGraph.getConnection(dep);
  41. // Skip rendering the dependency when it is conditional and inactive.
  42. if (connection && !connection.isTargetActive(runtime)) {
  43. source.replace(
  44. dep.outerRange[0],
  45. dep.outerRange[1] - 1,
  46. "/* unused asset import */ undefined"
  47. );
  48. return;
  49. }
  50. runtimeRequirements.add(RuntimeGlobals.require);
  51. const moduleRaw = runtimeTemplate.moduleRaw({
  52. chunkGraph,
  53. module: moduleGraph.getModule(dep),
  54. request: dep.request,
  55. runtimeRequirements,
  56. weak: false
  57. });
  58. if (dep.relative) {
  59. runtimeRequirements.add(RuntimeGlobals.relativeUrl);
  60. source.replace(
  61. dep.outerRange[0],
  62. dep.outerRange[1] - 1,
  63. `(new ${RuntimeGlobals.relativeUrl}(/* asset import */ ${moduleRaw})).href`
  64. );
  65. } else {
  66. runtimeRequirements.add(RuntimeGlobals.baseURI);
  67. source.replace(
  68. dep.outerRange[0],
  69. dep.outerRange[1] - 1,
  70. `(new URL(/* asset import */ ${moduleRaw}, ${RuntimeGlobals.baseURI})).href`
  71. );
  72. }
  73. }
  74. };
  75. makeSerializable(
  76. ImportMetaResolveDependency,
  77. "webpack/lib/dependencies/ImportMetaResolveDependency"
  78. );
  79. module.exports = ImportMetaResolveDependency;