Skip to content

Commit f2aed62

Browse files
authored
fix: tree shake stringified JSON imports (#19189)
1 parent 0802662 commit f2aed62

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

packages/vite/src/node/__tests__/build.spec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,69 @@ describe('build', () => {
134134
assertOutputHashContentChange(result[0], result[1])
135135
})
136136

137+
test.for([
138+
[true, true],
139+
[true, false],
140+
[false, true],
141+
[false, false],
142+
['auto', true],
143+
['auto', false],
144+
] as const)(
145+
'large json object files should have tree-shaking (json.stringify: %s, json.namedExports: %s)',
146+
async ([stringify, namedExports]) => {
147+
const esBundle = (await build({
148+
mode: 'development',
149+
root: resolve(__dirname, 'packages/build-project'),
150+
logLevel: 'silent',
151+
json: { stringify, namedExports },
152+
build: {
153+
minify: false,
154+
modulePreload: { polyfill: false },
155+
write: false,
156+
},
157+
plugins: [
158+
{
159+
name: 'test',
160+
resolveId(id) {
161+
if (
162+
id === 'entry.js' ||
163+
id === 'object.json' ||
164+
id === 'array.json'
165+
) {
166+
return '\0' + id
167+
}
168+
},
169+
load(id) {
170+
if (id === '\0entry.js') {
171+
return `
172+
import object from 'object.json';
173+
import array from 'array.json';
174+
console.log();
175+
`
176+
}
177+
if (id === '\0object.json') {
178+
return `
179+
{"value": {"${stringify}_${namedExports}":"JSON_OBJ${'_'.repeat(10_000)}"}}
180+
`
181+
}
182+
if (id === '\0array.json') {
183+
return `
184+
["${stringify}_${namedExports}","JSON_ARR${'_'.repeat(10_000)}"]
185+
`
186+
}
187+
},
188+
},
189+
],
190+
})) as RollupOutput
191+
192+
const foo = esBundle.output.find(
193+
(chunk) => chunk.type === 'chunk' && chunk.isEntry,
194+
) as OutputChunk
195+
expect(foo.code).not.contains('JSON_ARR')
196+
expect(foo.code).not.contains('JSON_OBJ')
197+
},
198+
)
199+
137200
test('external modules should not be hoisted in library build', async () => {
138201
const [esBundle] = (await build({
139202
logLevel: 'silent',

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('transform', () => {
6262
false,
6363
)
6464
expect(actualSmall).toMatchInlineSnapshot(
65-
`"export default JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
65+
`"export default /* #__PURE__ */ JSON.parse("[{\\"a\\":1,\\"b\\":2}]")"`,
6666
)
6767
})
6868

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

128128
const actualBuild = transform(
@@ -131,7 +131,7 @@ describe('transform', () => {
131131
true,
132132
)
133133
expect(actualBuild).toMatchInlineSnapshot(
134-
`"export default JSON.parse("{\\"a\\":1,\\"🫠\\":\\"\\",\\"const\\":false}")"`,
134+
`"export default /* #__PURE__ */ JSON.parse("{\\"a\\":1,\\"🫠\\":\\"\\",\\"const\\":false}")"`,
135135
)
136136
})
137137

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function jsonPlugin(
8787
}
8888

8989
return {
90-
code: `export default JSON.parse(${JSON.stringify(json)})`,
90+
code: `export default /* #__PURE__ */ JSON.parse(${JSON.stringify(json)})`,
9191
map: { mappings: '' },
9292
}
9393
}
@@ -120,7 +120,7 @@ function serializeValue(value: unknown): string {
120120
value != null &&
121121
valueAsString.length > 10 * 1000
122122
) {
123-
return `JSON.parse(${JSON.stringify(valueAsString)})`
123+
return `/* #__PURE__ */ JSON.parse(${JSON.stringify(valueAsString)})`
124124
}
125125
return valueAsString
126126
}

0 commit comments

Comments
 (0)