Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add pure annotation to json import when stringified
Since Vite 6, a json import over 10kB is stringified by default.

Current implementation would add a JSON.parse() call that would not be tree shaken
  • Loading branch information
lPadier committed Jan 12, 2025
commit 54d26e7b669e8d7e8a6f12d23be9dceaed2b00dc
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
2 changes: 1 addition & 1 deletion 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
Loading