Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,45 @@ const sortScripts = onObject((scripts, packageJson) => {
return sortObjectKeys(scripts, order)
})

const sortPathLikeObjectWithWildcards = onObject((object) => {
// Replace all '*' with the highest possible unicode character
// To force all wildcards to be at the end, but relative to
// the path they are in
const wildcard = '\u{10FFFF}'
const sortableWildcardPaths = new Map()
const sortablePath = (path) => {
if (sortableWildcardPaths.has(path)) return sortableWildcardPaths.get(path)
const wildcardWeightedPath = path.replace(/\*/g, wildcard)
sortableWildcardPaths.set(path, wildcardWeightedPath)
return wildcardWeightedPath
}
return sortObjectKeys(object, (a, b) => {
return sortablePath(a).localeCompare(sortablePath(b))
})
})

const sortExportsOrImports = onObject((exportOrImports) => {
const exportsWithSortedChildren = Object.fromEntries(
Object.entries(exportOrImports).map(([key, value]) => {
return [key, sortExportsOrImports(value)]
}),
)

const keys = Object.keys(exportsWithSortedChildren)
let isPathLikeObject = true
for (const key of keys) {
const keyIsPathLike = key.startsWith('.') || key.startsWith('#')
isPathLikeObject = isPathLikeObject && keyIsPathLike
}

if (isPathLikeObject) {
return sortPathLikeObjectWithWildcards(exportsWithSortedChildren)
}

// Object is likely condition object
return exportOrImports
})

// fields marked `vscode` are for `Visual Studio Code extension manifest` only
// https://code.visualstudio.com/api/references/extension-manifest
// Supported fields:
Expand Down Expand Up @@ -306,8 +345,8 @@ const fields = [
/* vscode */ { key: 'publisher' },
{ key: 'sideEffects' },
{ key: 'type' },
{ key: 'imports' },
{ key: 'exports' },
{ key: 'imports', over: sortExportsOrImports },
{ key: 'exports', over: sortExportsOrImports },
{ key: 'main' },
{ key: 'svelte' },
{ key: 'umd:main' },
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"semantic-release": "semantic-release",
"test": "ava && dtslint --localTs node_modules/typescript/lib",
"test-coverage": "nyc ava",
"test-watch": "ava --watch",
"update-snapshots": "ava --update-snapshots"
},
"commitlint": {
Expand Down
37 changes: 37 additions & 0 deletions tests/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,40 @@ test('pnpm', macro.sortObject, {
},
},
})

test('imports', macro.sortObject, {
path: 'imports',
value: {
'#c': './index.js',
'#c/sub': './index.js',
'#c/*': './wild/*.js',
'#a': './sub/index.js',
'#b/sub/*': './wild/*.js',
'#b/*': './wild/*.js',
'#b/sub': './wild/sub-module.js',
},
expect: {
'#a': './sub/index.js',
'#b/sub': './wild/sub-module.js',
'#b/sub/*': './wild/*.js',
'#b/*': './wild/*.js',
'#c': './index.js',
'#c/sub': './index.js',
'#c/*': './wild/*.js',
},
})
test('exports level 1', macro.sortObject, {
path: 'exports',
value: {
'./sub': './sub/index.js',
'./a-wildcard/*': './wild/*.js',
'./a-wildcard/sub': './wild/sub-module.js',
'.': './index.js',
},
expect: {
'.': './index.js',
'./a-wildcard/sub': './wild/sub-module.js',
'./a-wildcard/*': './wild/*.js',
'./sub': './sub/index.js',
},
})