index.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. export type Options = {
  2. /**
  3. * When true, adds the "browser" conditions.
  4. * Otherwise the "node" condition is enabled.
  5. * @default false
  6. */
  7. browser?: boolean;
  8. /**
  9. * Any custom conditions to match.
  10. * @note Array order does not matter. Priority is determined by the key-order of conditions defined within a package's imports/exports mapping.
  11. * @default []
  12. */
  13. conditions?: readonly string[];
  14. /**
  15. * When true, adds the "require" condition.
  16. * Otherwise the "import" condition is enabled.
  17. * @default false
  18. */
  19. require?: boolean;
  20. /**
  21. * Prevents "require", "import", "browser", and/or "node" conditions from being added automatically.
  22. * When enabled, only `options.conditions` are added alongside the "default" condition.
  23. * @important Enabling this deviates from Node.js default behavior.
  24. * @default false
  25. */
  26. unsafe?: boolean;
  27. }
  28. export function resolve<T=Package>(pkg: T, entry?: string, options?: Options): Imports.Output | Exports.Output | void;
  29. export function imports<T=Package>(pkg: T, entry?: string, options?: Options): Imports.Output | void;
  30. export function exports<T=Package>(pkg: T, target: string, options?: Options): Exports.Output | void;
  31. export function legacy<T=Package>(pkg: T, options: { browser: true, fields?: readonly string[] }): Browser | void;
  32. export function legacy<T=Package>(pkg: T, options: { browser: string, fields?: readonly string[] }): string | false | void;
  33. export function legacy<T=Package>(pkg: T, options: { browser: false, fields?: readonly string[] }): string | void;
  34. export function legacy<T=Package>(pkg: T, options?: {
  35. browser?: boolean | string;
  36. fields?: readonly string[];
  37. }): Browser | string;
  38. // ---
  39. /**
  40. * A resolve condition
  41. * @example "node", "default", "production"
  42. */
  43. export type Condition = string;
  44. /** An internal file path */
  45. export type Path = `./${string}`;
  46. export type Imports = {
  47. [entry: Imports.Entry]: Imports.Value;
  48. }
  49. export namespace Imports {
  50. export type Entry = `#${string}`;
  51. type External = string;
  52. /** strings are dependency names OR internal paths */
  53. export type Value = External | Path | null | {
  54. [c: Condition]: Value;
  55. } | Value[];
  56. export type Output = Array<External|Path>;
  57. }
  58. export type Exports = Path | {
  59. [path: Exports.Entry]: Exports.Value;
  60. [cond: Condition]: Exports.Value;
  61. }
  62. export namespace Exports {
  63. /** Allows "." and "./{name}" */
  64. export type Entry = `.${string}`;
  65. /** strings must be internal paths */
  66. export type Value = Path | null | {
  67. [c: Condition]: Value;
  68. } | Value[];
  69. export type Output = Path[];
  70. }
  71. export type Package = {
  72. name: string;
  73. version?: string;
  74. module?: string;
  75. main?: string;
  76. imports?: Imports;
  77. exports?: Exports;
  78. browser?: Browser;
  79. [key: string]: any;
  80. }
  81. export type Browser = string[] | string | {
  82. [file: Path | string]: string | false;
  83. }