Skip to content

Commit

Permalink
fix: sort exports map (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 14, 2022
1 parent 6f9cf21 commit 0b82fee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/check-project/manifests/typed-esm.js
Expand Up @@ -2,6 +2,7 @@ import { semanticReleaseConfig } from '../semantic-release-config.js'
import mergeOptions from 'merge-options'
import {
sortFields,
sortExportsMap,
constructManifest
} from '../utils.js'

Expand Down Expand Up @@ -39,9 +40,10 @@ export async function typedESMManifest (manifest, branchName, repoUrl, homePage
'!dist/test',
'!**/*.tsbuildinfo'
],
exports: sortFields(
exports: sortExportsMap(
merge({
'.': {
types: './src/index.d.ts',
import: './src/index.js'
}
}, manifest.exports)
Expand Down
4 changes: 3 additions & 1 deletion src/check-project/manifests/typescript.js
Expand Up @@ -4,6 +4,7 @@ import { semanticReleaseConfig } from '../semantic-release-config.js'
import mergeOptions from 'merge-options'
import {
sortFields,
sortExportsMap,
constructManifest
} from '../utils.js'

Expand All @@ -26,9 +27,10 @@ export async function typescriptManifest (manifest, branchName, repoUrl, homePag
'!dist/test',
'!**/*.tsbuildinfo'
],
exports: sortFields(
exports: sortExportsMap(
merge({
'.': {
types: './src/index.d.ts',
import: './dist/src/index.js'
}
}, manifest.exports)
Expand Down
28 changes: 28 additions & 0 deletions src/check-project/utils.js
Expand Up @@ -160,6 +160,34 @@ export function sortFields (obj) {
return output
}

/**
* @param {Record<string, any>} obj
*/
export function sortExportsMap (obj) {
/** @type {Record<string, any>} */
const output = {}
const sorted = sortFields(obj) ?? {}

for (const key of Object.keys(sorted)) {
const entry = sorted[key]
let types = entry.types

if (!types) {
types = entry.import.replace('.js', '.d.ts')
}

delete entry.types

output[key] = {
// types field must be first - https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#package-json-exports-imports-and-self-referencing
types,
...entry
}
}

return output
}

/**
* @param {any} manifest
*/
Expand Down

0 comments on commit 0b82fee

Please sign in to comment.