Skip to content

Commit a4e9826

Browse files
committed
refactor: extract sortConditions, make it easier for keithamus#350
1 parent 156b3d7 commit a4e9826

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

index.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,32 +261,41 @@ const sortScripts = onObject((scripts, packageJson) => {
261261
return sortObjectKeys(scripts, order)
262262
})
263263

264-
const sortExports = onObject((exports) => {
265-
const { paths = [], conditions = [] } = objectGroupBy(
266-
Object.keys(exports),
267-
(key) => (key.startsWith('.') ? 'paths' : 'conditions'),
268-
)
264+
const sortConditions = (conditions) => {
265+
const result = [...conditions]
269266

270267
// Move `types` to top
271268
{
272-
const index = conditions.indexOf('types')
269+
const index = result.indexOf('types')
273270
if (index > 0) {
274-
conditions.splice(index, 1)
275-
conditions.unshift('types')
271+
result.splice(index, 1)
272+
result.unshift('types')
276273
}
277274
}
278275

279276
// Move `default` to bottom
280277
{
281-
const index = conditions.indexOf('default')
278+
const index = result.indexOf('default')
282279
if (index !== -1) {
283-
conditions.splice(index, 1)
284-
conditions.push('default')
280+
result.splice(index, 1)
281+
result.push('default')
285282
}
286283
}
287284

285+
return result
286+
}
287+
288+
const sortExports = onObject((exports) => {
289+
const { paths = [], conditions = [] } = objectGroupBy(
290+
Object.keys(exports),
291+
(key) => (key.startsWith('.') ? 'paths' : 'conditions'),
292+
)
293+
288294
return Object.fromEntries(
289-
[...paths, ...conditions].map((key) => [key, sortExports(exports[key])]),
295+
[...paths, ...sortConditions(conditions)].map((key) => [
296+
key,
297+
sortExports(exports[key]),
298+
]),
290299
)
291300
})
292301

0 commit comments

Comments
 (0)