resolveMatchedConfigs.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ModuleNotFoundError = require("../ModuleNotFoundError");
  7. const LazySet = require("../util/LazySet");
  8. /** @typedef {import("../Compilation")} Compilation */
  9. /** @typedef {import("../ResolverFactory").ResolveOptionsWithDependencyType} ResolveOptionsWithDependencyType */
  10. /**
  11. * @template T
  12. * @typedef {object} MatchedConfigs
  13. * @property {Map<string, T>} resolved
  14. * @property {Map<string, T>} unresolved
  15. * @property {Map<string, T>} prefixed
  16. */
  17. /** @type {ResolveOptionsWithDependencyType} */
  18. const RESOLVE_OPTIONS = { dependencyType: "esm" };
  19. /**
  20. * @template T
  21. * @param {Compilation} compilation the compilation
  22. * @param {[string, T][]} configs to be processed configs
  23. * @returns {Promise<MatchedConfigs<T>>} resolved matchers
  24. */
  25. exports.resolveMatchedConfigs = (compilation, configs) => {
  26. /** @type {Map<string, T>} */
  27. const resolved = new Map();
  28. /** @type {Map<string, T>} */
  29. const unresolved = new Map();
  30. /** @type {Map<string, T>} */
  31. const prefixed = new Map();
  32. const resolveContext = {
  33. /** @type {LazySet<string>} */
  34. fileDependencies: new LazySet(),
  35. /** @type {LazySet<string>} */
  36. contextDependencies: new LazySet(),
  37. /** @type {LazySet<string>} */
  38. missingDependencies: new LazySet()
  39. };
  40. const resolver = compilation.resolverFactory.get("normal", RESOLVE_OPTIONS);
  41. const context = compilation.compiler.context;
  42. return Promise.all(
  43. configs.map(([request, config]) => {
  44. if (/^\.\.?(\/|$)/.test(request)) {
  45. // relative request
  46. return new Promise(resolve => {
  47. resolver.resolve(
  48. {},
  49. context,
  50. request,
  51. resolveContext,
  52. (err, result) => {
  53. if (err || result === false) {
  54. err = err || new Error(`Can't resolve ${request}`);
  55. compilation.errors.push(
  56. new ModuleNotFoundError(null, err, {
  57. name: `shared module ${request}`
  58. })
  59. );
  60. return resolve(null);
  61. }
  62. resolved.set(/** @type {string} */ (result), config);
  63. resolve(null);
  64. }
  65. );
  66. });
  67. } else if (/^(\/|[A-Za-z]:\\|\\\\)/.test(request)) {
  68. // absolute path
  69. resolved.set(request, config);
  70. } else if (request.endsWith("/")) {
  71. // module request prefix
  72. prefixed.set(request, config);
  73. } else {
  74. // module request
  75. unresolved.set(request, config);
  76. }
  77. })
  78. ).then(() => {
  79. compilation.contextDependencies.addAll(resolveContext.contextDependencies);
  80. compilation.fileDependencies.addAll(resolveContext.fileDependencies);
  81. compilation.missingDependencies.addAll(resolveContext.missingDependencies);
  82. return { resolved, unresolved, prefixed };
  83. });
  84. };