diff --git a/dts.snapshot.json b/dts.snapshot.json index dd16b03..aee8e3c 100644 --- a/dts.snapshot.json +++ b/dts.snapshot.json @@ -6,8 +6,8 @@ ] }, "index.d.mts": { - "Options": "interface Options {\n include?: FilterPattern\n exclude?: FilterPattern\n includeNonExport?: boolean\n saveTo?: string\n}", "DtsSnapshot": "declare function DtsSnapshot(_: Options): Plugin", + "Options": "interface Options {\n include?: FilterPattern\n exclude?: FilterPattern\n includeNonExport?: boolean\n saveTo?: string\n}", "#exports": [ "DtsSnapshot", "Options" diff --git a/src/api.ts b/src/api.ts index 631e8ec..e8cc79f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,6 +4,7 @@ import { getPath } from '@dprint/typescript' import { walk } from 'estree-walker' import MagicString from 'magic-string' import { parseSync } from 'rolldown/experimental' +import { sortObjectKeys } from './utils.ts' import type { Node, Span, TSTypeAnnotation } from '@oxc-project/types' const multilineCommentsRE = /\/\*.*?\*\//gs @@ -101,7 +102,7 @@ export function snapshot( } } - return result + return sortObjectKeys(result) function nodeToString(node: Node) { return node.type === 'Identifier' diff --git a/src/index.ts b/src/index.ts index d9ef311..0d6eff2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { writeFile } from 'node:fs/promises' import { createFilter, type FilterPattern } from 'unplugin-utils' import { snapshot } from './api.ts' +import { sortObjectKeys } from './utils.ts' import type { Plugin } from 'rolldown' const RE_DTS = /\.d\.[cm]?ts$/ @@ -61,7 +62,7 @@ export function DtsSnapshot({ } } - const code = `${JSON.stringify(result, null, 2)}\n` + const code = `${JSON.stringify(sortObjectKeys(result), null, 2)}\n` await writeFile(saveTo, code) }, }, diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..473a11c --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,5 @@ +export function sortObjectKeys>(obj: T): T { + return Object.fromEntries( + Object.entries(obj).toSorted(([a], [b]) => a.localeCompare(b)), + ) as T +} diff --git a/tsdown.config.ts b/tsdown.config.ts index b19f688..6c6a976 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from 'tsdown' import { DtsSnapshot } from './src/index.ts' export default defineConfig({ - entry: ['src/*.ts'], + entry: ['src/index.ts', 'src/api.ts'], platform: 'node', exports: true, inlineOnly: ['estree-walker'],