resolveMatchedConfigs.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. module.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. // eslint-disable-next-line array-callback-return
  44. configs.map(([request, config]) => {
  45. if (/^\.\.?(\/|$)/.test(request)) {
  46. // relative request
  47. return new Promise(resolve => {
  48. resolver.resolve(
  49. {},
  50. context,
  51. request,
  52. resolveContext,
  53. (err, result) => {
  54. if (err || result === false) {
  55. err = err || new Error(`Can't resolve ${request}`);
  56. compilation.errors.push(
  57. new ModuleNotFoundError(null, err, {
  58. name: `shared module ${request}`
  59. })
  60. );
  61. return resolve(null);
  62. }
  63. resolved.set(/** @type {string} */ (result), config);
  64. resolve(null);
  65. }
  66. );
  67. });
  68. } else if (/^(\/|[A-Za-z]:\\|\\\\)/.test(request)) {
  69. // absolute path
  70. resolved.set(request, config);
  71. } else if (request.endsWith("/")) {
  72. // module request prefix
  73. prefixed.set(request, config);
  74. } else {
  75. // module request
  76. unresolved.set(request, config);
  77. }
  78. })
  79. ).then(() => {
  80. compilation.contextDependencies.addAll(resolveContext.contextDependencies);
  81. compilation.fileDependencies.addAll(resolveContext.fileDependencies);
  82. compilation.missingDependencies.addAll(resolveContext.missingDependencies);
  83. return { resolved, unresolved, prefixed };
  84. });
  85. };