Skip to content

Commit fa50b03

Browse files
authored
fix(json): don't json.stringify arrays (#18541)
1 parent d002e7d commit fa50b03

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

packages/vite/src/node/__tests__/plugins/json.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ describe('transform', () => {
3535
return (plugin.transform! as Function)(input, 'test.json').code
3636
}
3737

38+
test("namedExports: true, stringify: 'auto' should not transformed an array input", () => {
39+
const actualSmall = transform(
40+
'[{"a":1,"b":2}]',
41+
{ namedExports: true, stringify: 'auto' },
42+
false,
43+
)
44+
expect(actualSmall).toMatchInlineSnapshot(`
45+
"export default [
46+
{
47+
a: 1,
48+
b: 2
49+
}
50+
];"
51+
`)
52+
})
53+
54+
test('namedExports: true, stringify: true should not transformed an array input', () => {
55+
const actualSmall = transform(
56+
'[{"a":1,"b":2}]',
57+
{ namedExports: true, stringify: true },
58+
false,
59+
)
60+
expect(actualSmall).toMatchInlineSnapshot(
61+
`"export default JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
62+
)
63+
})
64+
3865
test('namedExports: true, stringify: false', () => {
3966
const actual = transform(
4067
'{"a":1,\n"🫠": "",\n"const": false}',

packages/vite/src/node/plugins/json.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface JsonOptions {
2929
// Custom json filter for vite
3030
const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/
3131

32+
const jsonObjRE = /^\s*\{/
33+
3234
const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`
3335
const jsonLangRE = new RegExp(jsonLangs)
3436
export const isJSONRequest = (request: string): boolean =>
@@ -49,28 +51,26 @@ export function jsonPlugin(
4951

5052
try {
5153
if (options.stringify !== false) {
52-
if (options.namedExports) {
54+
if (options.namedExports && jsonObjRE.test(json)) {
5355
const parsed = JSON.parse(json)
54-
if (typeof parsed === 'object' && parsed != null) {
55-
const keys = Object.keys(parsed)
56-
57-
let code = ''
58-
let defaultObjectCode = '{\n'
59-
for (const key of keys) {
60-
if (key === makeLegalIdentifier(key)) {
61-
code += `export const ${key} = ${serializeValue(parsed[key])};\n`
62-
defaultObjectCode += ` ${key},\n`
63-
} else {
64-
defaultObjectCode += ` ${JSON.stringify(key)}: ${serializeValue(parsed[key])},\n`
65-
}
56+
const keys = Object.keys(parsed)
57+
58+
let code = ''
59+
let defaultObjectCode = '{\n'
60+
for (const key of keys) {
61+
if (key === makeLegalIdentifier(key)) {
62+
code += `export const ${key} = ${serializeValue(parsed[key])};\n`
63+
defaultObjectCode += ` ${key},\n`
64+
} else {
65+
defaultObjectCode += ` ${JSON.stringify(key)}: ${serializeValue(parsed[key])},\n`
6666
}
67-
defaultObjectCode += '}'
67+
}
68+
defaultObjectCode += '}'
6869

69-
code += `export default ${defaultObjectCode};\n`
70-
return {
71-
code,
72-
map: { mappings: '' },
73-
}
70+
code += `export default ${defaultObjectCode};\n`
71+
return {
72+
code,
73+
map: { mappings: '' },
7474
}
7575
}
7676

0 commit comments

Comments
 (0)