index.d.mts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Options to configure module resolution.
  3. */
  4. type ResolveOptions = {
  5. /**
  6. * A URL, path, or array of URLs/paths from which to resolve the module.
  7. * If not provided, resolution starts from the current working directory.
  8. * You can use `import.meta.url` to mimic the behavior of `import.meta.resolve()`.
  9. * For better performance, use a `file://` URL or path that ends with `/`.
  10. */
  11. from?: string | URL | (string | URL)[];
  12. /**
  13. * Resolve cache (enabled by default with a shared global object).
  14. * Can be set to `false` to disable or a custom `Map` to bring your own cache object.
  15. */
  16. cache?: boolean | Map<string, unknown>;
  17. /**
  18. * Additional file extensions to check.
  19. * For better performance, use explicit extensions and avoid this option.
  20. */
  21. extensions?: string[];
  22. /**
  23. * Conditions to apply when resolving package exports.
  24. * Defaults to `["node", "import"]`.
  25. * Conditions are applied without order.
  26. */
  27. conditions?: string[];
  28. /**
  29. * Path suffixes to check.
  30. * For better performance, use explicit paths and avoid this option.
  31. * Example: `["", "/index"]`
  32. */
  33. suffixes?: string[];
  34. /**
  35. * If set to `true` and the module cannot be resolved,
  36. * the resolver returns `undefined` instead of throwing an error.
  37. */
  38. try?: boolean;
  39. };
  40. type ResolverOptions = Omit<ResolveOptions, "try">;
  41. type ResolveRes<Opts extends ResolveOptions> = Opts["try"] extends true ? string | undefined : string;
  42. /**
  43. * Synchronously resolves a module url based on the options provided.
  44. *
  45. * @param {string} input - The identifier or path of the module to resolve.
  46. * @param {ResolveOptions} [options] - Options to resolve the module. See {@link ResolveOptions}.
  47. * @returns {string} The resolved URL as a string.
  48. */
  49. declare function resolveModuleURL<O extends ResolveOptions>(input: string | URL, options?: O): ResolveRes<O>;
  50. /**
  51. * Synchronously resolves a module then converts it to a file path
  52. *
  53. * (throws error if reolved path is not file:// scheme)
  54. *
  55. * @param {string} id - The identifier or path of the module to resolve.
  56. * @param {ResolveOptions} [options] - Options to resolve the module. See {@link ResolveOptions}.
  57. * @returns {string} The resolved URL as a string.
  58. */
  59. declare function resolveModulePath<O extends ResolveOptions>(id: string | URL, options?: O): ResolveRes<O>;
  60. declare function createResolver(defaults?: ResolverOptions): {
  61. resolveModuleURL: <O extends ResolveOptions>(id: string | URL, opts: ResolveOptions) => ResolveRes<O>;
  62. resolveModulePath: <O extends ResolveOptions>(id: string | URL, opts: ResolveOptions) => ResolveRes<O>;
  63. clearResolveCache: () => void;
  64. };
  65. declare function clearResolveCache(): void;
  66. export { type ResolveOptions, type ResolverOptions, clearResolveCache, createResolver, resolveModulePath, resolveModuleURL };