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: tree shake stringified JSON imports #19189

Merged
merged 4 commits into from
Jan 15, 2025
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
63 changes: 63 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,69 @@ describe('build', () => {
assertOutputHashContentChange(result[0], result[1])
})

test.for([
[true, true],
[true, false],
[false, true],
[false, false],
['auto', true],
['auto', false],
] as const)(
'large json object files should have tree-shaking (json.stringify: %s, json.namedExports: %s)',
async ([stringify, namedExports]) => {
const esBundle = (await build({
mode: 'development',
root: resolve(__dirname, 'packages/build-project'),
logLevel: 'silent',
json: { stringify, namedExports },
build: {
minify: false,
modulePreload: { polyfill: false },
write: false,
},
plugins: [
{
name: 'test',
resolveId(id) {
if (
id === 'entry.js' ||
id === 'object.json' ||
id === 'array.json'
) {
return '\0' + id
}
},
load(id) {
if (id === '\0entry.js') {
return `
import object from 'object.json';
import array from 'array.json';
console.log();
`
}
if (id === '\0object.json') {
return `
{"value": {"${stringify}_${namedExports}":"JSON_OBJ${'_'.repeat(10_000)}"}}
`
}
if (id === '\0array.json') {
return `
["${stringify}_${namedExports}","JSON_ARR${'_'.repeat(10_000)}"]
`
}
},
},
],
})) as RollupOutput

const foo = esBundle.output.find(
(chunk) => chunk.type === 'chunk' && chunk.isEntry,
) as OutputChunk
expect(foo.code).not.contains('JSON_ARR')
expect(foo.code).not.contains('JSON_OBJ')
},
)

test('external modules should not be hoisted in library build', async () => {
const [esBundle] = (await build({
logLevel: 'silent',
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/__tests__/plugins/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('transform', () => {
false,
)
expect(actualSmall).toMatchInlineSnapshot(
`"export default JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
`"export default /* #__PURE__ */ JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
)
})

Expand Down Expand Up @@ -122,7 +122,7 @@ describe('transform', () => {
false,
)
expect(actualDev).toMatchInlineSnapshot(
`"export default JSON.parse("{\\"a\\":1,\\n\\"🫠\\": \\"\\",\\n\\"const\\": false}")"`,
`"export default /* #__PURE__ */ JSON.parse("{\\"a\\":1,\\n\\"🫠\\": \\"\\",\\n\\"const\\": false}")"`,
)

const actualBuild = transform(
Expand All @@ -131,7 +131,7 @@ describe('transform', () => {
true,
)
expect(actualBuild).toMatchInlineSnapshot(
`"export default JSON.parse("{\\"a\\":1,\\"🫠\\":\\"\\",\\"const\\":false}")"`,
`"export default /* #__PURE__ */ JSON.parse("{\\"a\\":1,\\"🫠\\":\\"\\",\\"const\\":false}")"`,
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function jsonPlugin(
}

return {
code: `export default JSON.parse(${JSON.stringify(json)})`,
code: `export default /* #__PURE__ */ JSON.parse(${JSON.stringify(json)})`,
map: { mappings: '' },
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ function serializeValue(value: unknown): string {
value != null &&
valueAsString.length > 10 * 1000
) {
return `JSON.parse(${JSON.stringify(valueAsString)})`
return `/* #__PURE__ */ JSON.parse(${JSON.stringify(valueAsString)})`
}
return valueAsString
}
Expand Down
Loading