-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexports.ts
More file actions
231 lines (215 loc) · 6.71 KB
/
exports.ts
File metadata and controls
231 lines (215 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* @fileoverview Package exports field utilities.
*/
import { isArray } from '../arrays/predicates'
import { LOOP_SENTINEL } from '../constants/sentinels'
import { isObject, isPlainObject } from '../objects/predicates'
import { ArrayPrototypePush } from '../primordials/array'
import { ErrorCtor } from '../primordials/error'
import { SetCtor } from '../primordials/map-set'
import { ObjectGetOwnPropertyNames } from '../primordials/object'
import {
StringPrototypeCharCodeAt,
StringPrototypeStartsWith,
} from '../primordials/string'
/**
* Find types definition for a specific subpath in package exports.
*
* @example
* ```typescript
* const exports = { '.': { types: './dist/index.d.ts', import: './dist/index.js' } }
* const types = findTypesForSubpath(exports, './dist/index.js')
* // types === './dist/index.d.ts'
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function findTypesForSubpath(
entryExports: unknown,
subpath: string,
): string | undefined {
const queue = [entryExports]
let pos = 0
while (pos < queue.length) {
if (pos === LOOP_SENTINEL) {
throw new ErrorCtor(
'Detected infinite loop in entry exports crawl of getTypesForSubpath',
)
}
const value = queue[pos++]
if (isArray(value)) {
for (let i = 0, { length } = value; i < length; i += 1) {
const item = value[i]
if (item === subpath) {
return (value as { types?: string }).types
}
if (isObject(item)) {
queue.push(item)
}
}
} else if (isObject(value)) {
const keys = ObjectGetOwnPropertyNames(value)
for (let i = 0, { length } = keys; i < length; i += 1) {
const key = keys[i] as string
const item = value[key]
if (item === subpath) {
return (value as { types?: string }).types
}
if (isObject(item)) {
queue.push(item)
}
}
}
}
return undefined
}
/**
* Get file paths from package exports.
*
* @example
* ```typescript
* const exports = { '.': './dist/index.js', './utils': './dist/utils.js' }
* getExportFilePaths(exports) // ['./dist/index.js', './dist/utils.js']
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function getExportFilePaths(entryExports: unknown): string[] {
if (!isObject(entryExports)) {
return []
}
const paths: string[] = []
// Traverse the exports object to find actual file paths.
for (const key of ObjectGetOwnPropertyNames(entryExports)) {
if (!StringPrototypeStartsWith(key, '.')) {
continue
}
const value = entryExports[key]
if (typeof value === 'string') {
// Direct path export.
ArrayPrototypePush(paths, value)
} else if (isObject(value)) {
// Conditional or nested export.
for (const subKey of ObjectGetOwnPropertyNames(value)) {
const subValue = value[subKey]
if (typeof subValue === 'string') {
ArrayPrototypePush(paths, subValue)
} else if (isArray(subValue)) {
// Array of conditions.
for (const item of subValue) {
if (typeof item === 'string') {
ArrayPrototypePush(paths, item)
} else if (isObject(item)) {
// Nested conditional.
for (const nestedKey of ObjectGetOwnPropertyNames(item)) {
const nestedValue = item[nestedKey]
if (typeof nestedValue === 'string') {
ArrayPrototypePush(paths, nestedValue)
}
}
}
}
}
}
}
}
// Remove duplicates and filter out non-file paths.
return [...new SetCtor(paths)].filter(p => StringPrototypeStartsWith(p, './'))
}
/**
* Get subpaths from package exports.
*
* @example
* ```typescript
* const exports = { '.': './index.js', './utils': './utils.js' }
* getSubpaths(exports) // ['.', './utils']
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function getSubpaths(entryExports: unknown): string[] {
if (!isObject(entryExports)) {
return []
}
// Return the keys of the exports object (the subpaths).
return ObjectGetOwnPropertyNames(entryExports).filter(key =>
StringPrototypeStartsWith(key, '.'),
)
}
/**
* Check if package exports use conditional patterns (e.g., import/require).
*
* @example
* ```typescript
* isConditionalExports({ import: './index.mjs', require: './index.cjs' }) // true
* isConditionalExports({ '.': './index.js' }) // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function isConditionalExports(entryExports: unknown): boolean {
if (!isPlainObject(entryExports)) {
return false
}
const keys = ObjectGetOwnPropertyNames(entryExports)
const { length } = keys
if (!length) {
return false
}
// Conditional entry exports do NOT contain keys starting with '.'.
// Entry exports cannot contain some keys starting with '.' and some not.
// The exports object MUST either be an object of package subpath keys OR
// an object of main entry condition name keys only.
for (let i = 0; i < length; i += 1) {
const key = keys[i] as string
if (key.length > 0 && StringPrototypeCharCodeAt(key, 0) === 46 /*'.'*/) {
return false
}
}
return true
}
/**
* Check if package exports use subpath patterns (keys starting with '.').
*
* @example
* ```typescript
* isSubpathExports({ '.': './index.js', './utils': './utils.js' }) // true
* isSubpathExports({ import: './index.mjs' }) // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function isSubpathExports(entryExports: unknown): boolean {
if (isPlainObject(entryExports)) {
const keys = ObjectGetOwnPropertyNames(entryExports)
for (let i = 0, { length } = keys; i < length; i += 1) {
// Subpath entry exports contain keys starting with '.'.
// Entry exports cannot contain some keys starting with '.' and some not.
// The exports object MUST either be an object of package subpath keys OR
// an object of main entry condition name keys only.
if (keys[i]?.charCodeAt(0) === 46 /*'.'*/) {
return true
}
}
}
return false
}
/**
* Normalize package.json exports field to canonical format.
*
* @example
* ```typescript
* resolvePackageJsonEntryExports('./index.js')
* // { '.': './index.js' }
*
* resolvePackageJsonEntryExports({ '.': './index.js' })
* // { '.': './index.js' }
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function resolvePackageJsonEntryExports(entryExports: unknown): unknown {
// If conditional exports main sugar
// https://nodejs.org/api/packages.html#exports-sugar
if (typeof entryExports === 'string' || isArray(entryExports)) {
return { '.': entryExports }
}
if (isConditionalExports(entryExports)) {
return entryExports
}
return isObject(entryExports) ? entryExports : undefined
}