-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Ensure package
exports
conditions are sorted correctly. (#111)
* Update swc. * Add sorting. * Run on repo. * Add tests.
- Loading branch information
Showing
13 changed files
with
104 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { PackageExportPaths } from '../types'; | ||
|
||
// https://nodejs.org/api/packages.html#conditional-exports | ||
const WEIGHTS = { | ||
types: 10, // Types must always be first | ||
misc: 20, | ||
'node-addons': 30, | ||
node: 31, | ||
import: 40, | ||
require: 50, | ||
default: 100, // Default must be last | ||
}; | ||
|
||
export function sortExportConditions(paths: PackageExportPaths): PackageExportPaths { | ||
const pathsList: { weight: number; key: string; value: PackageExportPaths | string }[] = []; | ||
|
||
Object.entries(paths).forEach(([key, value]) => { | ||
pathsList.push({ | ||
weight: key in WEIGHTS ? WEIGHTS[key as 'misc'] : WEIGHTS.misc, | ||
key, | ||
value: typeof value === 'string' ? value : sortExportConditions(value), | ||
}); | ||
}); | ||
|
||
pathsList.sort((a, d) => { | ||
const diff = a.weight - d.weight; | ||
|
||
return diff === 0 ? a.key.localeCompare(d.key) : diff; | ||
}); | ||
|
||
return Object.fromEntries(pathsList.map((path) => [path.key, path.value])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/packemon/tests/helpers/sortExportConditions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { sortExportConditions } from '../../src/helpers/sortExportConditions'; | ||
|
||
describe('sortExportConditions()', () => { | ||
it('sorts in the correct order', () => { | ||
const map = sortExportConditions({ | ||
default: '', | ||
script: '', | ||
import: '', | ||
node: '', | ||
require: '', | ||
types: '', | ||
browser: '', | ||
}); | ||
|
||
expect(Object.keys(map)).toStrictEqual([ | ||
'types', | ||
'browser', | ||
'script', | ||
'node', | ||
'import', | ||
'require', | ||
'default', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters