Skip to content

Commit b574572

Browse files
committedNov 21, 2024
fix: better format json
1 parent a707fe8 commit b574572

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed
 

‎tsconfig.app.json

+7-21
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,13 @@
44
"tsBuildInfoFile": "node_modules/.cache/tsconfig.app.tsbuildinfo",
55
"baseUrl": "./src",
66
"paths": {
7-
"@components/*": [
8-
"components/*"
9-
],
10-
"@components": [
11-
"components"
12-
],
13-
"@views/*": [
14-
"views/*"
15-
],
16-
"@views": [
17-
"views"
18-
],
19-
"@utils/*": [
20-
"utils/*"
21-
],
22-
"@utils": [
23-
"utils"
24-
],
25-
"@/*": [
26-
"*"
27-
]
7+
"@components/*": [ "components/*" ],
8+
"@components": [ "components" ],
9+
"@views/*": [ "views/*" ],
10+
"@views": [ "views" ],
11+
"@utils/*": [ "utils/*" ],
12+
"@utils": [ "utils" ],
13+
"@/*": [ "*" ]
2814
}
2915
},
3016
"include": ["src/**/*"]

‎vite.config.ts

+41-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ import { fileURLToPath, URL } from 'url'
44
import { relative, join } from 'path'
55

66
const normalizePath = (path: string) => path.replace(/\\{1,2}/g, '/')
7+
8+
const quote = (value: string) => `"${value}"`
9+
10+
function formatJson(str: string): string
11+
function formatJson(obj: object): string
12+
function formatJson(value: string | object) {
13+
if (typeof value === 'string') value = JSON.parse(value)
14+
value = value as object
15+
const indent = 2
16+
const cache = new WeakSet()
17+
const wrapper = (val: string, level: number) => {
18+
return `${' '.repeat(level * indent)}${val}`
19+
}
20+
const stringify = (val: unknown, level: number) => {
21+
// json 数值
22+
if (typeof val === 'string') return wrapper(quote(val), level)
23+
if (typeof val === null || typeof val === 'boolean' || typeof val === 'number') return wrapper(String(val), level)
24+
if (typeof val === 'object') {
25+
if (cache.has(val as object)) throw new Error('Converting circular structure to JSON')
26+
cache.add(val as object)
27+
if (Array.isArray(val)) {
28+
const newline = val.length > 2 && val.some(v => v === null && typeof v !== 'object')
29+
const v: string = val.map<string>((child) => stringify(child, newline ? level + 1 : 0)).join(newline ? ',\n' : ', ')
30+
return `[${newline ? '\n' : ' '}${v}${newline ? wrapper(']', level) : ' ]'}`
31+
} else {
32+
const entries = Object.entries(val as object)
33+
const v: string = entries.map<string>(([key, child]) => {
34+
return `${wrapper(quote(key), level + 1)}: ${stringify(child, level + 1)}`
35+
}).join(',\n')
36+
return `{\n${v}\n${wrapper('}', level)}`
37+
}
38+
}
39+
throw new TypeError(`Unsupported value: ${val}`)
40+
}
41+
42+
return stringify(value, 0)
43+
}
44+
745
const createTsConfigPaths = (alias: Record<string, string>, basePath?: string) => {
846
const tsconfigPaths: Record<string, string[]> = Object.create(null)
947
for (const [name, path] of Object.entries(alias)) {
@@ -12,7 +50,7 @@ const createTsConfigPaths = (alias: Record<string, string>, basePath?: string) =
1250
if (!name.endsWith('*')) tsconfigPaths[normalizePath(join(name, '*'))] = [normalizePath(join(resolvePath, '*'))]
1351
if (name !== '@') tsconfigPaths[name] = [resolvePath]
1452
}
15-
return JSON.stringify(tsconfigPaths, null, 2)
53+
return formatJson(tsconfigPaths)
1654
}
1755
const createPath = (...base: string[]) => {
1856
return (...paths: string[]) => fileURLToPath(
@@ -23,10 +61,10 @@ const src = createPath('src')
2361
const typings = createPath('typings')
2462

2563
const alias = {
26-
'@': src(),
2764
'@components': src('components'),
2865
'@views': src('views'),
29-
'@utils': src('utils')
66+
'@utils': src('utils'),
67+
'@': src()
3068
}
3169
console.log(createTsConfigPaths(alias, src()))
3270
// https://vitejs.dev/config/

0 commit comments

Comments
 (0)
Please sign in to comment.