Skip to content

Commit 0b82fee

Browse files
authoredJun 14, 2022
fix: sort exports map (#999)
1 parent 6f9cf21 commit 0b82fee

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed
 

‎src/check-project/manifests/typed-esm.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { semanticReleaseConfig } from '../semantic-release-config.js'
22
import mergeOptions from 'merge-options'
33
import {
44
sortFields,
5+
sortExportsMap,
56
constructManifest
67
} from '../utils.js'
78

@@ -39,9 +40,10 @@ export async function typedESMManifest (manifest, branchName, repoUrl, homePage
3940
'!dist/test',
4041
'!**/*.tsbuildinfo'
4142
],
42-
exports: sortFields(
43+
exports: sortExportsMap(
4344
merge({
4445
'.': {
46+
types: './src/index.d.ts',
4547
import: './src/index.js'
4648
}
4749
}, manifest.exports)

‎src/check-project/manifests/typescript.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { semanticReleaseConfig } from '../semantic-release-config.js'
44
import mergeOptions from 'merge-options'
55
import {
66
sortFields,
7+
sortExportsMap,
78
constructManifest
89
} from '../utils.js'
910

@@ -26,9 +27,10 @@ export async function typescriptManifest (manifest, branchName, repoUrl, homePag
2627
'!dist/test',
2728
'!**/*.tsbuildinfo'
2829
],
29-
exports: sortFields(
30+
exports: sortExportsMap(
3031
merge({
3132
'.': {
33+
types: './src/index.d.ts',
3234
import: './dist/src/index.js'
3335
}
3436
}, manifest.exports)

‎src/check-project/utils.js

+28
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ export function sortFields (obj) {
160160
return output
161161
}
162162

163+
/**
164+
* @param {Record<string, any>} obj
165+
*/
166+
export function sortExportsMap (obj) {
167+
/** @type {Record<string, any>} */
168+
const output = {}
169+
const sorted = sortFields(obj) ?? {}
170+
171+
for (const key of Object.keys(sorted)) {
172+
const entry = sorted[key]
173+
let types = entry.types
174+
175+
if (!types) {
176+
types = entry.import.replace('.js', '.d.ts')
177+
}
178+
179+
delete entry.types
180+
181+
output[key] = {
182+
// types field must be first - https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#package-json-exports-imports-and-self-referencing
183+
types,
184+
...entry
185+
}
186+
}
187+
188+
return output
189+
}
190+
163191
/**
164192
* @param {any} manifest
165193
*/

0 commit comments

Comments
 (0)
Please sign in to comment.