Skip to content

Commit 15e1140

Browse files
authored
fix: stop forcing exports "types" to be first (#398)
1 parent e3f2370 commit 15e1140

4 files changed

Lines changed: 50 additions & 51 deletions

File tree

index.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -390,27 +390,21 @@ const sortScripts = onObject((scripts, packageJson) => {
390390
})
391391

392392
/*
393-
- Move `types` and versioned type condition to top
394393
- Move `default` condition to bottom
395394
*/
396395
const sortConditions = (conditions) => {
397-
const {
398-
typesConditions = [],
399-
defaultConditions = [],
400-
restConditions = [],
401-
} = objectGroupBy(conditions, (condition) => {
402-
if (condition === 'types' || condition.startsWith('types@')) {
403-
return 'typesConditions'
404-
}
405-
406-
if (condition === 'default') {
407-
return 'defaultConditions'
408-
}
396+
const { defaultConditions = [], restConditions = [] } = objectGroupBy(
397+
conditions,
398+
(condition) => {
399+
if (condition === 'default') {
400+
return 'defaultConditions'
401+
}
409402

410-
return 'restConditions'
411-
})
403+
return 'restConditions'
404+
},
405+
)
412406

413-
return [...typesConditions, ...restConditions, ...defaultConditions]
407+
return [...restConditions, ...defaultConditions]
414408
}
415409

416410
const sortExports = onObject((exports) => {

tests/exports.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ for (const deep of [false, true]) {
88
const exports = {
99
unknown: './unknown.unknown',
1010
'./path-not-really-makes-no-sense': {},
11-
types: './types.d.ts',
12-
'types@<=1': './v1/types.d.ts',
1311
}
1412

15-
test(`'types' condition should be first${titleSuffix}`, macro.sortObject, {
13+
test(`paths (starting .) should be first${titleSuffix}`, macro.sortObject, {
1614
path: 'exports',
1715
expect: 'snapshot',
1816
value: deep ? { './deep': exports } : exports,
@@ -21,14 +19,17 @@ for (const deep of [false, true]) {
2119

2220
{
2321
const exports = {
24-
unknown: './unknown.unknown',
22+
'unknown-3': './unknown.unknown3',
2523
'./path-not-really-makes-no-sense': {},
26-
'types@<=1': './v1/types.d.ts',
24+
'unknown-1': './unknown.unknown1',
25+
default: './whatever/index.js',
2726
types: './types.d.ts',
27+
'unknown-2': './unknown.unknown2',
28+
'types@<=1': './v1/types.d.ts',
2829
}
2930

3031
test(
31-
`'types' condition should be first${titleSuffix} 2`,
32+
`keys that are not paths or 'default' should retain order, including those that start 'types...' ${titleSuffix}`,
3233
macro.sortObject,
3334
{
3435
path: 'exports',
@@ -41,8 +42,8 @@ for (const deep of [false, true]) {
4142
{
4243
const exports = {
4344
unknown: './unknown.unknown',
44-
'./path-not-really-makes-no-sense': {},
4545
default: './default.js',
46+
'./path-not-really-makes-no-sense': {},
4647
}
4748

4849
test(`'default' condition should be last${titleSuffix}`, macro.sortObject, {

tests/snapshots/exports.js.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,53 @@ The actual snapshot is saved in `exports.js.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

7-
## 'types' condition should be first
7+
## paths (starting .) should be first
88

99
> Should sort `exports` as object.
1010
1111
{
1212
input: `{␊
1313
"exports": {␊
1414
"unknown": "./unknown.unknown",␊
15-
"./path-not-really-makes-no-sense": {},␊
16-
"types": "./types.d.ts",␊
17-
"types@<=1": "./v1/types.d.ts"␊
15+
"./path-not-really-makes-no-sense": {}␊
1816
}␊
1917
}`,
2018
options: undefined,
2119
output: `{␊
2220
"exports": {␊
2321
"./path-not-really-makes-no-sense": {},␊
24-
"types": "./types.d.ts",␊
25-
"types@<=1": "./v1/types.d.ts",␊
2622
"unknown": "./unknown.unknown"␊
2723
}␊
2824
}`,
2925
pretty: true,
3026
}
3127

32-
## 'types' condition should be first 2
28+
## keys that are not paths or 'default' should retain order, including those that start 'types...'
3329

3430
> Should sort `exports` as object.
3531
3632
{
3733
input: `{␊
3834
"exports": {␊
39-
"unknown": "./unknown.unknown",␊
35+
"unknown-3": "./unknown.unknown3",␊
4036
"./path-not-really-makes-no-sense": {},␊
41-
"types@<=1": "./v1/types.d.ts",␊
42-
"types": "./types.d.ts"␊
37+
"unknown-1": "./unknown.unknown1",␊
38+
"default": "./whatever/index.js",␊
39+
"types": "./types.d.ts",␊
40+
"unknown-2": "./unknown.unknown2",␊
41+
"types@<=1": "./v1/types.d.ts"␊
4342
}␊
4443
}`,
4544
options: undefined,
4645
output: `{␊
4746
"exports": {␊
4847
"./path-not-really-makes-no-sense": {},␊
49-
"types@<=1": "./v1/types.d.ts",␊
48+
"unknown-3": "./unknown.unknown3",␊
49+
"unknown-1": "./unknown.unknown1",␊
5050
"types": "./types.d.ts",␊
51-
"unknown": "./unknown.unknown"␊
51+
"unknown-2": "./unknown.unknown2",␊
52+
"types@<=1": "./v1/types.d.ts",␊
53+
"default": "./whatever/index.js"␊
5254
}␊
5355
}`,
5456
pretty: true,
@@ -62,8 +64,8 @@ Generated by [AVA](https://avajs.dev).
6264
input: `{␊
6365
"exports": {␊
6466
"unknown": "./unknown.unknown",␊
65-
"./path-not-really-makes-no-sense": {},␊
66-
"default": "./default.js"
67+
"default": "./default.js",␊
68+
"./path-not-really-makes-no-sense": {}
6769
}␊
6870
}`,
6971
options: undefined,
@@ -77,7 +79,7 @@ Generated by [AVA](https://avajs.dev).
7779
pretty: true,
7880
}
7981

80-
## 'types' condition should be first(deep)
82+
## paths (starting .) should be first(deep)
8183

8284
> Should sort `exports` as object.
8385
@@ -86,9 +88,7 @@ Generated by [AVA](https://avajs.dev).
8688
"exports": {␊
8789
"./deep": {␊
8890
"unknown": "./unknown.unknown",␊
89-
"./path-not-really-makes-no-sense": {},␊
90-
"types": "./types.d.ts",␊
91-
"types@<=1": "./v1/types.d.ts"␊
91+
"./path-not-really-makes-no-sense": {}␊
9292
}␊
9393
}␊
9494
}`,
@@ -97,27 +97,28 @@ Generated by [AVA](https://avajs.dev).
9797
"exports": {␊
9898
"./deep": {␊
9999
"./path-not-really-makes-no-sense": {},␊
100-
"types": "./types.d.ts",␊
101-
"types@<=1": "./v1/types.d.ts",␊
102100
"unknown": "./unknown.unknown"␊
103101
}␊
104102
}␊
105103
}`,
106104
pretty: true,
107105
}
108106

109-
## 'types' condition should be first(deep) 2
107+
## keys that are not paths or 'default' should retain order, including those that start 'types...' (deep)
110108

111109
> Should sort `exports` as object.
112110
113111
{
114112
input: `{␊
115113
"exports": {␊
116114
"./deep": {␊
117-
"unknown": "./unknown.unknown",␊
115+
"unknown-3": "./unknown.unknown3",␊
118116
"./path-not-really-makes-no-sense": {},␊
119-
"types@<=1": "./v1/types.d.ts",␊
120-
"types": "./types.d.ts"␊
117+
"unknown-1": "./unknown.unknown1",␊
118+
"default": "./whatever/index.js",␊
119+
"types": "./types.d.ts",␊
120+
"unknown-2": "./unknown.unknown2",␊
121+
"types@<=1": "./v1/types.d.ts"␊
121122
}␊
122123
}␊
123124
}`,
@@ -126,9 +127,12 @@ Generated by [AVA](https://avajs.dev).
126127
"exports": {␊
127128
"./deep": {␊
128129
"./path-not-really-makes-no-sense": {},␊
129-
"types@<=1": "./v1/types.d.ts",␊
130+
"unknown-3": "./unknown.unknown3",␊
131+
"unknown-1": "./unknown.unknown1",␊
130132
"types": "./types.d.ts",␊
131-
"unknown": "./unknown.unknown"␊
133+
"unknown-2": "./unknown.unknown2",␊
134+
"types@<=1": "./v1/types.d.ts",␊
135+
"default": "./whatever/index.js"␊
132136
}␊
133137
}␊
134138
}`,
@@ -144,8 +148,8 @@ Generated by [AVA](https://avajs.dev).
144148
"exports": {␊
145149
"./deep": {␊
146150
"unknown": "./unknown.unknown",␊
147-
"./path-not-really-makes-no-sense": {},␊
148-
"default": "./default.js"
151+
"default": "./default.js",␊
152+
"./path-not-really-makes-no-sense": {}
149153
}␊
150154
}␊
151155
}`,

tests/snapshots/exports.js.snap

165 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)