Skip to content

Commit 6e4eef6

Browse files
fengmk2claude
andauthored
refactor(migration): simplify JSON config merging by using direct file content (#378)
Improves the Vite config migration process by directly using JSON/JSONC content in JavaScript context instead of parsing and converting it. Since JSON is valid JavaScript, this approach: 1. Preserves comments in JSONC files (both `//` and `/* */` style) 2. Maintains the original formatting and structure of config files 3. Removes ~130 lines of complex JSON-to-JS conversion code 4. Fixes the TODO for handling JSONC files in the migrator The PR includes tests to verify that comments in JSONC files are properly preserved during migration. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b462d4d commit 6e4eef6

8 files changed

Lines changed: 155 additions & 319 deletions

File tree

crates/vite_migration/src/vite_config.rs

Lines changed: 108 additions & 271 deletions
Large diffs are not rendered by default.

packages/global/snap-tests/migration-auto-create-vite-config/snap.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
2121

2222
export default defineConfig({
2323
fmt: {
24-
printWidth: 100,
25-
tabWidth: 2,
26-
semi: true,
27-
singleQuote: true,
28-
trailingComma: 'es5',
24+
"printWidth": 100,
25+
"tabWidth": 2,
26+
"semi": true,
27+
"singleQuote": true,
28+
"trailingComma": "es5"
2929
},
3030
lint: {
31-
rules: {
32-
'no-unused-vars': 'error',
33-
},
31+
"rules": {
32+
"no-unused-vars": "error"
33+
}
3434
},
3535
});
3636

packages/global/snap-tests/migration-from-tsdown-json-config/snap.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
2020

2121
export default defineConfig({
2222
lib: {
23-
entry: 'src/index.ts',
24-
outDir: 'dist',
25-
format: ['esm', 'cjs'],
26-
dts: true,
27-
inputOptions: {
28-
cwd: './src',
29-
},
23+
"entry": "src/index.ts",
24+
"outDir": "dist",
25+
"format": ["esm", "cjs"],
26+
"dts": true,
27+
"inputOptions": {
28+
"cwd": "./src"
29+
}
3030
},
3131
server: {
3232
port: 3000,
@@ -69,13 +69,13 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
6969

7070
export default defineConfig({
7171
lib: {
72-
entry: 'src/index.ts',
73-
outDir: 'dist',
74-
format: ['esm', 'cjs'],
75-
dts: true,
76-
inputOptions: {
77-
cwd: './src',
78-
},
72+
"entry": "src/index.ts",
73+
"outDir": "dist",
74+
"format": ["esm", "cjs"],
75+
"dts": true,
76+
"inputOptions": {
77+
"cwd": "./src"
78+
}
7979
},
8080
server: {
8181
port: 3000,

packages/global/snap-tests/migration-merge-vite-config-js/snap.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import react from '@vitejs/plugin-react';
1717

1818
export default {
1919
lint: {
20-
rules: {
21-
'no-unused-vars': 'error',
22-
},
20+
"rules": {
21+
"no-unused-vars": "error"
22+
}
2323
},
2424
plugins: [react()],
2525
}

packages/global/snap-tests/migration-merge-vite-config-ts/snap.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
2727

2828
export default defineConfig({
2929
fmt: {
30-
printWidth: 100,
31-
tabWidth: 2,
32-
semi: true,
33-
singleQuote: true,
34-
trailingComma: 'es5',
30+
"printWidth": 100,
31+
"tabWidth": 2,
32+
"semi": true,
33+
"singleQuote": true,
34+
"trailingComma": "es5"
3535
},
3636
lint: {
37-
rules: {
38-
'no-unused-vars': 'error',
39-
},
37+
"rules": {
38+
"no-unused-vars": "error"
39+
}
4040
},
4141
plugins: [react()],
4242
test: {

packages/global/snap-tests/migration-monorepo-pnpm/snap.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
2626

2727
export default defineConfig({
2828
fmt: {
29-
printWidth: 100,
30-
tabWidth: 2,
31-
semi: true,
32-
singleQuote: true,
33-
trailingComma: 'es5',
29+
"printWidth": 100,
30+
"tabWidth": 2,
31+
"semi": true,
32+
"singleQuote": true,
33+
"trailingComma": "es5"
3434
},
3535
lint: {
36-
rules: {
37-
'no-unused-vars': 'error',
38-
},
36+
"rules": {
37+
"no-unused-vars": "error"
38+
}
3939
},
4040
plugins: [react()],
4141
});
@@ -164,9 +164,9 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
164164

165165
export default defineConfig({
166166
lint: {
167-
rules: {
168-
'no-unused-vars': 'warn',
169-
},
167+
"rules": {
168+
"no-unused-vars": "warn"
169+
}
170170
},
171171

172172
});

packages/global/snap-tests/migration-monorepo-yarn4/snap.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import { defineConfig } from '@voidzero-dev/vite-plus';
2020

2121
export default defineConfig({
2222
lint: {
23-
rules: {
24-
'no-unused-vars': 'error',
25-
},
23+
"rules": {
24+
"no-unused-vars": "error"
25+
}
2626
},
2727
plugins: [react()],
2828
});

packages/global/src/migration/migrator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ function mergeViteConfigFiles(projectPath: string): void {
648648
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxlintConfig, 'lint');
649649
}
650650
if (configs.oxfmtConfig) {
651-
// TODO: handle jsonc file
652651
// merge oxfmt config into vite.config.ts
653652
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxfmtConfig, 'fmt');
654653
}

0 commit comments

Comments
 (0)