Skip to content

Commit b0b3429

Browse files
Doctor-wusxzzautofix-ci[bot]
authored
feat(migrate): use ast-grep for config file (#620)
Co-authored-by: Kevin Deng <sxzz@sxzz.moe> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a653d49 commit b0b3429

File tree

10 files changed

+1145
-132
lines changed

10 files changed

+1145
-132
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"@vitest/coverage-v8": "catalog:dev",
120120
"@vueuse/core": "catalog:docs",
121121
"bumpp": "catalog:dev",
122+
"dedent": "catalog:dev",
122123
"eslint": "catalog:dev",
123124
"is-in-ci": "catalog:prod",
124125
"lightningcss": "catalog:dev",

packages/migrate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@antfu/ni": "catalog:migrate",
52+
"@ast-grep/napi": "catalog:migrate",
5253
"ansis": "catalog:prod",
5354
"cac": "catalog:prod",
5455
"consola": "catalog:migrate",
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`comprehensive transformation > should transform complex tsup config > code 1`] = `
4+
"import { defineConfig } from 'tsdown'
5+
import icons from 'unplugin-icons/rolldown'
6+
7+
export default defineConfig({
8+
entry: ['src/index.ts'],
9+
format: ['esm', 'cjs'],
10+
plugins: [icons()],
11+
unbundle: true,
12+
copy: 'public',
13+
splitting: true,
14+
metafile: true,
15+
clean: false,
16+
dts: false,
17+
target: false,
18+
})"
19+
`;
20+
21+
exports[`comprehensive transformation > should transform complex tsup config > warnings 1`] = `
22+
[
23+
"The \`splitting\` option is currently unsupported in tsdown. Code splitting is always enabled and cannot be disabled.",
24+
"The \`metafile\` option is not available in tsdown. Consider using Vite DevTools as an alternative.",
25+
]
26+
`;
27+
28+
exports[`default values > should add clean: false when not present > code 1`] = `
29+
"export default {
30+
entry: ['src/index.ts'],
31+
format: 'cjs',
32+
clean: false,
33+
dts: false,
34+
target: false,
35+
}"
36+
`;
37+
38+
exports[`default values > should add dts: false when not present > code 1`] = `
39+
"export default {
40+
entry: ['src/index.ts'],
41+
format: 'cjs',
42+
clean: false,
43+
dts: false,
44+
target: false,
45+
}"
46+
`;
47+
48+
exports[`default values > should add format: "cjs" when not present > code 1`] = `
49+
"export default {
50+
entry: ['src/index.ts'],
51+
format: 'cjs',
52+
clean: false,
53+
dts: false,
54+
target: false,
55+
}"
56+
`;
57+
58+
exports[`default values > should add target: false when not present > code 1`] = `
59+
"export default {
60+
entry: ['src/index.ts'],
61+
format: 'cjs',
62+
clean: false,
63+
dts: false,
64+
target: false,
65+
}"
66+
`;
67+
68+
exports[`default values > should not add format when present > code 1`] = `
69+
"export default {
70+
format: ['esm', 'cjs'],
71+
clean: false,
72+
dts: false,
73+
target: false,
74+
}"
75+
`;
76+
77+
exports[`option transformations > bundle: false should transform to unbundle: true > code 1`] = `
78+
"export default {
79+
unbundle: true,
80+
format: 'cjs',
81+
clean: false,
82+
dts: false,
83+
target: false,
84+
}"
85+
`;
86+
87+
exports[`option transformations > bundle: true should be removed > code 1`] = `
88+
"export default {
89+
entry: ['src/index.ts'],
90+
format: 'cjs',
91+
clean: false,
92+
dts: false,
93+
target: false,
94+
}"
95+
`;
96+
97+
exports[`option transformations > entryPoints should transform to entry > code 1`] = `
98+
"export default {
99+
entry: ['src/index.ts'],
100+
format: 'cjs',
101+
clean: false,
102+
dts: false,
103+
target: false,
104+
}"
105+
`;
106+
107+
exports[`option transformations > publicDir should transform to copy > code 1`] = `
108+
"export default {
109+
copy: 'public',
110+
format: 'cjs',
111+
clean: false,
112+
dts: false,
113+
target: false,
114+
}"
115+
`;
116+
117+
exports[`option transformations > removeNodeProtocol should transform to nodeProtocol: "strip" > code 1`] = `
118+
"export default {
119+
nodeProtocol: 'strip',
120+
format: 'cjs',
121+
clean: false,
122+
dts: false,
123+
target: false,
124+
}"
125+
`;
126+
127+
exports[`option transformations > splitting should emit warning > code 1`] = `
128+
"export default {
129+
splitting: true,
130+
format: 'cjs',
131+
clean: false,
132+
dts: false,
133+
target: false,
134+
}"
135+
`;
136+
137+
exports[`option transformations > splitting should emit warning > warnings 1`] = `
138+
[
139+
"The \`splitting\` option is currently unsupported in tsdown. Code splitting is always enabled and cannot be disabled.",
140+
]
141+
`;
142+
143+
exports[`plugin migrations > esbuildPlugins should transform to plugins > code 1`] = `
144+
"export default {
145+
plugins: [somePlugin()],
146+
format: 'cjs',
147+
clean: false,
148+
dts: false,
149+
target: false,
150+
}"
151+
`;
152+
153+
exports[`plugin migrations > plugins option should emit warning (experimental in tsup) > code 1`] = `
154+
"export default {
155+
plugins: [somePlugin()],
156+
format: 'cjs',
157+
clean: false,
158+
dts: false,
159+
target: false,
160+
}"
161+
`;
162+
163+
exports[`plugin migrations > plugins option should emit warning (experimental in tsup) > warnings 1`] = `
164+
[
165+
"The \`plugins\` option in tsup is experimental. Please migrate your plugins manually.",
166+
]
167+
`;
168+
169+
exports[`plugin migrations > unplugin-*/esbuild should transform to unplugin-*/rolldown > code 1`] = `
170+
"import icons from 'unplugin-icons/rolldown'
171+
import vue from 'unplugin-vue/rolldown'
172+
173+
export default {
174+
plugins: [icons(), vue()],
175+
format: 'cjs',
176+
clean: false,
177+
dts: false,
178+
target: false,
179+
}"
180+
`;
181+
182+
exports[`warning options > cjsInterop should emit warning > code 1`] = `
183+
"export default {
184+
cjsDefault: true,
185+
format: 'cjs',
186+
clean: false,
187+
dts: false,
188+
target: false,
189+
}"
190+
`;
191+
192+
exports[`warning options > experimentalDts should emit warning > code 1`] = `
193+
"export default {
194+
experimentalDts: true,
195+
format: 'cjs',
196+
clean: false,
197+
dts: false,
198+
target: false,
199+
}"
200+
`;
201+
202+
exports[`warning options > experimentalDts should emit warning > warnings 1`] = `
203+
[
204+
"The \`experimentalDts\` option is not supported in tsdown. Use the \`dts\` option instead.",
205+
]
206+
`;
207+
208+
exports[`warning options > injectStyle should emit warning (not implemented) > code 1`] = `
209+
"export default {
210+
injectStyle: true,
211+
format: 'cjs',
212+
clean: false,
213+
dts: false,
214+
target: false,
215+
}"
216+
`;
217+
218+
exports[`warning options > injectStyle should emit warning (not implemented) > warnings 1`] = `
219+
[
220+
"The \`injectStyle\` option has not yet been implemented in tsdown.",
221+
]
222+
`;
223+
224+
exports[`warning options > legacyOutput should emit warning > code 1`] = `
225+
"export default {
226+
legacyOutput: true,
227+
format: 'cjs',
228+
clean: false,
229+
dts: false,
230+
target: false,
231+
}"
232+
`;
233+
234+
exports[`warning options > legacyOutput should emit warning > warnings 1`] = `
235+
[
236+
"The \`legacyOutput\` option is not supported in tsdown.",
237+
]
238+
`;
239+
240+
exports[`warning options > metafile should emit warning (use Vite DevTools) > code 1`] = `
241+
"export default {
242+
metafile: true,
243+
format: 'cjs',
244+
clean: false,
245+
dts: false,
246+
target: false,
247+
}"
248+
`;
249+
250+
exports[`warning options > metafile should emit warning (use Vite DevTools) > warnings 1`] = `
251+
[
252+
"The \`metafile\` option is not available in tsdown. Consider using Vite DevTools as an alternative.",
253+
]
254+
`;
255+
256+
exports[`warning options > swc should emit warning (use oxc) > code 1`] = `
257+
"export default {
258+
swc: true,
259+
format: 'cjs',
260+
clean: false,
261+
dts: false,
262+
target: false,
263+
}"
264+
`;
265+
266+
exports[`warning options > swc should emit warning (use oxc) > warnings 1`] = `
267+
[
268+
"The \`swc\` option is not supported in tsdown. Please use oxc instead.",
269+
]
270+
`;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { existsSync } from 'node:fs'
2+
import { readFile, writeFile } from 'node:fs/promises'
3+
import consola from 'consola'
4+
import { createPatch } from 'diff'
5+
import { detectIndentation } from '../../../../src/utils/format.ts'
6+
import pkg from '../../package.json' with { type: 'json' }
7+
import { outputDiff, renameKey } from '../utils.ts'
8+
9+
const DEP_FIELDS = {
10+
dependencies: `^${pkg.version}`,
11+
devDependencies: `^${pkg.version}`,
12+
optionalDependencies: `^${pkg.version}`,
13+
peerDependencies: '*',
14+
peerDependenciesMeta: null,
15+
} as const
16+
17+
export async function migratePackageJson(dryRun?: boolean): Promise<boolean> {
18+
if (!existsSync('package.json')) {
19+
consola.error('No package.json found')
20+
return false
21+
}
22+
23+
const pkgRaw = await readFile('package.json', 'utf8')
24+
let pkg = JSON.parse(pkgRaw)
25+
let found = false
26+
27+
for (const [field, semver] of Object.entries(DEP_FIELDS)) {
28+
if (pkg[field]?.tsup) {
29+
consola.info(`Migrating \`${field}\` to tsdown.`)
30+
found = true
31+
pkg[field] = renameKey(pkg[field], 'tsup', 'tsdown', semver)
32+
}
33+
}
34+
35+
if (pkg.scripts) {
36+
for (const key of Object.keys(pkg.scripts)) {
37+
if (pkg.scripts[key].includes('tsup')) {
38+
consola.info(`Migrating \`${key}\` script to tsdown`)
39+
found = true
40+
pkg.scripts[key] = pkg.scripts[key].replaceAll(
41+
/tsup(?:-node)?/g,
42+
'tsdown',
43+
)
44+
}
45+
}
46+
}
47+
if (pkg.tsup) {
48+
consola.info('Migrating `tsup` field in package.json to `tsdown`.')
49+
found = true
50+
pkg = renameKey(pkg, 'tsup', 'tsdown')
51+
}
52+
53+
if (!found) {
54+
consola.warn('No tsup-related fields found in package.json')
55+
return false
56+
}
57+
58+
const eol = pkgRaw.endsWith('\n') ? '\n' : ''
59+
const newPkgRaw = `${JSON.stringify(pkg, null, detectIndentation(pkgRaw))}${eol}`
60+
if (dryRun) {
61+
consola.info('[dry-run] package.json:')
62+
outputDiff(createPatch('package.json', pkgRaw, newPkgRaw))
63+
} else {
64+
await writeFile('package.json', newPkgRaw)
65+
consola.success('Migrated `package.json`')
66+
}
67+
return true
68+
}

0 commit comments

Comments
 (0)