Skip to content

Commit a79a1e2

Browse files
authored
feat: improved cold start using deps scanner (#8869)
1 parent 3388fe4 commit a79a1e2

File tree

7 files changed

+86
-34
lines changed

7 files changed

+86
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const bad = 1
2+
throw new Error('it is an expected error')

playground/ssr-vue/__tests__/serve.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
import path from 'node:path'
55
import kill from 'kill-port'
6+
import type { ViteDevServer } from 'vite'
67
import { hmrPorts, isBuild, ports, rootDir } from '~utils'
78

89
export const port = ports['ssr-vue']
910

11+
export let viteServer: ViteDevServer
12+
1013
export async function serve(): Promise<{ close(): Promise<void> }> {
1114
if (isBuild) {
1215
// build first
@@ -44,6 +47,7 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
4447
isBuild,
4548
hmrPorts['ssr-vue']
4649
)
50+
viteServer = vite
4751

4852
return new Promise((resolve, reject) => {
4953
try {

playground/ssr-vue/__tests__/ssr-vue.spec.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
23
import fetch from 'node-fetch'
34
import { port } from './serve'
45
import {
@@ -7,7 +8,8 @@ import {
78
getColor,
89
isBuild,
910
page,
10-
untilUpdated
11+
untilUpdated,
12+
viteServer
1113
} from '~utils'
1214

1315
const url = `http://localhost:${port}/test/`
@@ -202,3 +204,33 @@ test.runIf(isBuild)('dynamic css file should be preloaded', async () => {
202204
expect(homeHtml).toMatch(file)
203205
}
204206
})
207+
208+
test.runIf(!isBuild)(
209+
'always throw error when evaluating an wrong SSR module',
210+
async () => {
211+
const __filename = fileURLToPath(import.meta.url)
212+
const badjs = resolve(__filename, '../fixtures/ssrModuleLoader-bad.js')
213+
const THROW_MESSAGE = 'it is an expected error'
214+
215+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
216+
const expectedErrors = []
217+
for (const _ of [0, 1]) {
218+
try {
219+
console.log(viteServer)
220+
await viteServer.ssrLoadModule(badjs, { fixStacktrace: true })
221+
} catch (e) {
222+
expectedErrors.push(e)
223+
}
224+
}
225+
expect(expectedErrors).toHaveLength(2)
226+
expect(expectedErrors[0]).toBe(expectedErrors[1])
227+
expectedErrors.forEach((error) => {
228+
expect(error?.message).toContain(THROW_MESSAGE)
229+
})
230+
expect(spy).toBeCalledTimes(1)
231+
const [firstParameter] = spy.mock.calls[0]
232+
expect(firstParameter).toContain('Error when evaluating SSR module')
233+
expect(firstParameter).toContain(THROW_MESSAGE)
234+
spy.mockClear()
235+
}
236+
)

playground/vitestSetup.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export const viteBinPath = path.posix.join(
3535

3636
let server: ViteDevServer | http.Server
3737

38+
/**
39+
* Vite Dev Server when testing serve
40+
*/
41+
export let viteServer: ViteDevServer
3842
/**
3943
* Root of the Vite fixture
4044
*/
@@ -146,6 +150,7 @@ beforeAll(async (s) => {
146150
}
147151
if (serve) {
148152
server = await serve()
153+
viteServer = mod.viteServer
149154
return
150155
}
151156
} else {
@@ -212,7 +217,7 @@ export async function startDefaultServe(): Promise<void> {
212217
process.env.VITE_INLINE = 'inline-serve'
213218
const testConfig = mergeConfig(options, config || {})
214219
viteConfig = testConfig
215-
server = await (await createServer(testConfig)).listen()
220+
viteServer = server = await (await createServer(testConfig)).listen()
216221
// use resolved port/base from server
217222
const devBase = server.config.base
218223
viteTestUrl = `http://localhost:${server.config.server.port}${

playground/vue-sourcemap/__tests__/__snapshots__/serve.spec.ts.snap playground/vue-sourcemap/__tests__/__snapshots__/vue-sourcemap.spec.ts.snap

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1
22

3-
exports[`serve:vue-sourcemap > css 1`] = `
3+
exports[`serve:vue-sourcemap > css > serve-css 1`] = `
44
{
55
"mappings": ";AAQA;EACE,UAAU;AACZ",
66
"sources": [
@@ -47,7 +47,7 @@ exports[`serve:vue-sourcemap > css 1`] = `
4747
}
4848
`;
4949

50-
exports[`serve:vue-sourcemap > css module 1`] = `
50+
exports[`serve:vue-sourcemap > css module > serve-css-module 1`] = `
5151
{
5252
"mappings": ";AAcA;EACE,UAAU;AACZ",
5353
"sources": [
@@ -94,7 +94,7 @@ exports[`serve:vue-sourcemap > css module 1`] = `
9494
}
9595
`;
9696

97-
exports[`serve:vue-sourcemap > css scoped 1`] = `
97+
exports[`serve:vue-sourcemap > css scoped > serve-css-scoped 1`] = `
9898
{
9999
"mappings": ";AAoBA;EACE,UAAU;AACZ",
100100
"sources": [
@@ -141,7 +141,7 @@ exports[`serve:vue-sourcemap > css scoped 1`] = `
141141
}
142142
`;
143143

144-
exports[`serve:vue-sourcemap > js 1`] = `
144+
exports[`serve:vue-sourcemap > js > serve-js 1`] = `
145145
{
146146
"mappings": "AAKA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;wBARlB,oBAAiB,WAAd,MAAU",
147147
"sources": [
@@ -165,7 +165,7 @@ console.log('setup')
165165
}
166166
`;
167167

168-
exports[`serve:vue-sourcemap > less with additionalData 1`] = `
168+
exports[`serve:vue-sourcemap > less with additionalData > serve-less-with-additionalData 1`] = `
169169
{
170170
"mappings": "AAKA;EACE",
171171
"sources": [
@@ -187,7 +187,7 @@ exports[`serve:vue-sourcemap > less with additionalData 1`] = `
187187
}
188188
`;
189189
190-
exports[`serve:vue-sourcemap > no script 1`] = `
190+
exports[`serve:vue-sourcemap > no script > serve-no-script 1`] = `
191191
{
192192
"mappings": ";;;wBACE,oBAAwB,WAArB,aAAiB",
193193
"sourceRoot": "",
@@ -204,7 +204,7 @@ exports[`serve:vue-sourcemap > no script 1`] = `
204204
}
205205
`;
206206
207-
exports[`serve:vue-sourcemap > no template 1`] = `
207+
exports[`serve:vue-sourcemap > no template > serve-no-template 1`] = `
208208
{
209209
"mappings": "AACA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;AAGP;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;;;;;;",
210210
"sources": [
@@ -224,7 +224,7 @@ console.log('setup')
224224
}
225225
`;
226226
227-
exports[`serve:vue-sourcemap > sass 1`] = `
227+
exports[`serve:vue-sourcemap > sass > serve-sass 1`] = `
228228
{
229229
"mappings": "AAKA;EACE",
230230
"sources": [
@@ -245,7 +245,7 @@ exports[`serve:vue-sourcemap > sass 1`] = `
245245
}
246246
`;
247247
248-
exports[`serve:vue-sourcemap > sass with import 1`] = `
248+
exports[`serve:vue-sourcemap > sass with import > serve-sass-with-import 1`] = `
249249
{
250250
"mappings": "AAAA;EACE;;ACOF;EACE",
251251
"sources": [
@@ -273,7 +273,7 @@ exports[`serve:vue-sourcemap > sass with import 1`] = `
273273
}
274274
`;
275275
276-
exports[`serve:vue-sourcemap > src imported 1`] = `
276+
exports[`serve:vue-sourcemap > src imported > serve-src-imported 1`] = `
277277
{
278278
"mappings": "AAAA;EACE,UAAU;AACZ",
279279
"sources": [
@@ -289,7 +289,7 @@ exports[`serve:vue-sourcemap > src imported 1`] = `
289289
}
290290
`;
291291
292-
exports[`serve:vue-sourcemap > src imported sass 1`] = `
292+
exports[`serve:vue-sourcemap > src imported sass > serve-src-imported-sass 1`] = `
293293
{
294294
"mappings": "AAAA;EACE;;ACCF;EACE",
295295
"sources": [
@@ -310,7 +310,7 @@ exports[`serve:vue-sourcemap > src imported sass 1`] = `
310310
}
311311
`;
312312
313-
exports[`serve:vue-sourcemap > ts 1`] = `
313+
exports[`serve:vue-sourcemap > ts > serve-ts 1`] = `
314314
{
315315
"mappings": ";AAKA,QAAQ,IAAI,WAAW;;;;;AAIvB,YAAQ,IAAI,UAAU;;;;;;;;uBARpB,oBAAiB,WAAd,MAAU",
316316
"sources": [

playground/vue-sourcemap/__tests__/build.spec.ts

-7
This file was deleted.

playground/vue-sourcemap/__tests__/serve.spec.ts playground/vue-sourcemap/__tests__/vue-sourcemap.spec.ts

+29-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { URL } from 'node:url'
22
import {
33
extractSourcemap,
44
formatSourcemapForSnapshot,
5+
isBuild,
56
isServe,
6-
page
7+
page,
8+
serverLogs
79
} from '~utils'
810

911
describe.runIf(isServe)('serve:vue-sourcemap', () => {
@@ -22,62 +24,70 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
2224
const res = await page.request.get(new URL('./Js.vue', page.url()).href)
2325
const js = await res.text()
2426
const map = extractSourcemap(js)
25-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
27+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-js')
2628
})
2729

2830
test('ts', async () => {
2931
const res = await page.request.get(new URL('./Ts.vue', page.url()).href)
3032
const js = await res.text()
3133
const map = extractSourcemap(js)
32-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
34+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-ts')
3335
})
3436

3537
test('css', async () => {
3638
const css = await getStyleTagContentIncluding('.css ')
3739
const map = extractSourcemap(css)
38-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
40+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-css')
3941
})
4042

4143
test('css module', async () => {
4244
const css = await getStyleTagContentIncluding('._css-module_')
4345
const map = extractSourcemap(css)
44-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
46+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-css-module')
4547
})
4648

4749
test('css scoped', async () => {
4850
const css = await getStyleTagContentIncluding('.css-scoped[data-v-')
4951
const map = extractSourcemap(css)
50-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
52+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-css-scoped')
5153
})
5254

5355
test('sass', async () => {
5456
const css = await getStyleTagContentIncluding('.sass ')
5557
const map = extractSourcemap(css)
56-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
58+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-sass')
5759
})
5860

5961
test('sass with import', async () => {
6062
const css = await getStyleTagContentIncluding('.sass-with-import ')
6163
const map = extractSourcemap(css)
62-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
64+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot(
65+
'serve-sass-with-import'
66+
)
6367
})
6468

6569
test('less with additionalData', async () => {
6670
const css = await getStyleTagContentIncluding('.less ')
6771
const map = extractSourcemap(css)
68-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
72+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot(
73+
'serve-less-with-additionalData'
74+
)
6975
})
7076

7177
test('src imported', async () => {
7278
const css = await getStyleTagContentIncluding('.src-import[data-v-')
7379
const map = extractSourcemap(css)
74-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
80+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot(
81+
'serve-src-imported'
82+
)
7583
})
7684

7785
test('src imported sass', async () => {
7886
const css = await getStyleTagContentIncluding('.src-import-sass[data-v-')
7987
const map = extractSourcemap(css)
80-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
88+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot(
89+
'serve-src-imported-sass'
90+
)
8191
})
8292

8393
test('no script', async () => {
@@ -86,7 +96,7 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
8696
)
8797
const js = await res.text()
8898
const map = extractSourcemap(js)
89-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
99+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-no-script')
90100
})
91101

92102
test('no template', async () => {
@@ -95,6 +105,12 @@ describe.runIf(isServe)('serve:vue-sourcemap', () => {
95105
)
96106
const js = await res.text()
97107
const map = extractSourcemap(js)
98-
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot()
108+
expect(formatSourcemapForSnapshot(map)).toMatchSnapshot('serve-no-template')
109+
})
110+
})
111+
112+
test.runIf(isBuild)('should not output sourcemap warning (#4939)', () => {
113+
serverLogs.forEach((log) => {
114+
expect(log).not.toMatch('Sourcemap is likely to be incorrect')
99115
})
100116
})

0 commit comments

Comments
 (0)