Skip to content

Commit 1fe1d0a

Browse files
committed
update tests
1 parent 99dc8fd commit 1fe1d0a

File tree

18 files changed

+64
-236
lines changed

18 files changed

+64
-236
lines changed

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

Lines changed: 63 additions & 0 deletions
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/plugins/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

playground/json-stringify/__tests__/csr/json-csr.spec.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

playground/json-stringify/__tests__/minify.spec.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

playground/json-stringify/dev.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

playground/json-stringify/hmr.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

playground/json-stringify/index.html

Lines changed: 0 additions & 75 deletions
This file was deleted.

playground/json-stringify/json-bom/has-bom.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

playground/json-stringify/json-module/index.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

playground/json-stringify/json-module/package.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)