index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function resolveModuleName(request, issuer, compilerOptions, moduleResolutionHost, parentResolver) {
  2. const pnp = require(`pnpapi`);
  3. const [, prefix = ``, packageName = ``, rest] = request.match(/^(!(?:.*!)+)?((?!\.{0,2}\/)(?:@[^\/]+\/)?[^\/]+)?(.*)/);
  4. let failedLookupLocations = [];
  5. // First we try the resolution on "@types/package-name" starting from the project root
  6. if (packageName) {
  7. const typesPackagePath = `@types/${packageName.replace(/\//g, `__`)}${rest}`;
  8. let unqualified;
  9. try {
  10. unqualified = pnp.resolveToUnqualified(typesPackagePath, issuer, {considerBuiltins: false});
  11. } catch (error) {}
  12. if (unqualified) {
  13. // TypeScript checks whether the directory of the candidate is a directory
  14. // which may cause issues w/ zip loading (since the zip archive is still
  15. // reported as a file). To workaround this we add a trailing slash, which
  16. // causes TypeScript to assume the parent is a directory.
  17. if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified))
  18. unqualified += `/`;
  19. const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost);
  20. if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) {
  21. return finalResolution;
  22. } else {
  23. failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations);
  24. }
  25. }
  26. }
  27. // Then we try on "package-name", this time starting from the package that makes the request
  28. if (true) {
  29. const regularPackagePath = `${packageName || ``}${rest}`;
  30. let unqualified;
  31. try {
  32. unqualified = pnp.resolveToUnqualified(regularPackagePath, issuer, {considerBuiltins: false});
  33. } catch (error) {}
  34. if (unqualified) {
  35. // TypeScript checks whether the directory of the candidate is a directory
  36. // which may cause issues w/ zip loading (since the zip archive is still
  37. // reported as a file). To workaround this we add a trailing slash, which
  38. // causes TypeScript to assume the parent is a directory.
  39. if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified))
  40. unqualified += `/`;
  41. const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost);
  42. if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) {
  43. return finalResolution;
  44. } else {
  45. failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations);
  46. }
  47. }
  48. }
  49. return {
  50. resolvedModule: undefined,
  51. resolvedTypeReferenceDirective: undefined,
  52. failedLookupLocations,
  53. };
  54. }
  55. module.exports.resolveModuleName = process.versions.pnp
  56. ? resolveModuleName
  57. : (moduleName, containingFile, compilerOptions, compilerHost, resolveModuleName) =>
  58. resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost);