Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(json): don't json.stringify arrays #18541

Merged
merged 7 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
];"
`)
})

sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
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