Skip to content

Commit

Permalink
fix(json): don't json.stringify arrays (#18541)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Nov 1, 2024
1 parent d002e7d commit fa50b03
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
27 changes: 27 additions & 0 deletions packages/vite/src/node/__tests__/plugins/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ describe('transform', () => {
return (plugin.transform! as Function)(input, 'test.json').code
}

test("namedExports: true, stringify: 'auto' should not transformed an array input", () => {
const actualSmall = transform(
'[{"a":1,"b":2}]',
{ namedExports: true, stringify: 'auto' },
false,
)
expect(actualSmall).toMatchInlineSnapshot(`
"export default [
{
a: 1,
b: 2
}
];"
`)
})

test('namedExports: true, stringify: true should not transformed an array input', () => {
const actualSmall = transform(
'[{"a":1,"b":2}]',
{ namedExports: true, stringify: true },
false,
)
expect(actualSmall).toMatchInlineSnapshot(
`"export default JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
)
})

test('namedExports: true, stringify: false', () => {
const actual = transform(
'{"a":1,\n"🫠": "",\n"const": false}',
Expand Down
38 changes: 19 additions & 19 deletions packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface JsonOptions {
// Custom json filter for vite
const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/

const jsonObjRE = /^\s*\{/

const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`
const jsonLangRE = new RegExp(jsonLangs)
export const isJSONRequest = (request: string): boolean =>
Expand All @@ -49,28 +51,26 @@ export function jsonPlugin(

try {
if (options.stringify !== false) {
if (options.namedExports) {
if (options.namedExports && jsonObjRE.test(json)) {
const parsed = JSON.parse(json)
if (typeof parsed === 'object' && parsed != null) {
const keys = Object.keys(parsed)

let code = ''
let defaultObjectCode = '{\n'
for (const key of keys) {
if (key === makeLegalIdentifier(key)) {
code += `export const ${key} = ${serializeValue(parsed[key])};\n`
defaultObjectCode += ` ${key},\n`
} else {
defaultObjectCode += ` ${JSON.stringify(key)}: ${serializeValue(parsed[key])},\n`
}
const keys = Object.keys(parsed)

let code = ''
let defaultObjectCode = '{\n'
for (const key of keys) {
if (key === makeLegalIdentifier(key)) {
code += `export const ${key} = ${serializeValue(parsed[key])};\n`
defaultObjectCode += ` ${key},\n`
} else {
defaultObjectCode += ` ${JSON.stringify(key)}: ${serializeValue(parsed[key])},\n`
}
defaultObjectCode += '}'
}
defaultObjectCode += '}'

code += `export default ${defaultObjectCode};\n`
return {
code,
map: { mappings: '' },
}
code += `export default ${defaultObjectCode};\n`
return {
code,
map: { mappings: '' },
}
}

Expand Down

0 comments on commit fa50b03

Please sign in to comment.