Skip to content

Commit ca5dbef

Browse files
authored
fix(vite-node): have a separate cache for web/ssr transforms (#4221)
1 parent 5996c8c commit ca5dbef

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

packages/vite-node/src/server.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,33 @@ import { withInlineSourcemap } from './source-map'
1111

1212
export * from './externalize'
1313

14+
interface FetchCache {
15+
duration?: number
16+
timestamp: number
17+
result: FetchResult
18+
}
19+
1420
const debugRequest = createDebug('vite-node:server:request')
1521

1622
export class ViteNodeServer {
17-
private fetchPromiseMap = new Map<string, Promise<FetchResult>>()
18-
private transformPromiseMap = new Map<string, Promise<TransformResult | null | undefined>>()
23+
private fetchPromiseMap = {
24+
ssr: new Map<string, Promise<FetchResult>>(),
25+
web: new Map<string, Promise<FetchResult>>(),
26+
}
27+
28+
private transformPromiseMap = {
29+
ssr: new Map<string, Promise<TransformResult | null | undefined>>(),
30+
web: new Map<string, Promise<TransformResult | null | undefined>>(),
31+
}
1932

2033
private existingOptimizedDeps = new Set<string>()
2134

22-
fetchCache = new Map<string, {
23-
duration?: number
24-
timestamp: number
25-
result: FetchResult
26-
}>()
35+
fetchCaches = {
36+
ssr: new Map<string, FetchCache>(),
37+
web: new Map<string, FetchCache>(),
38+
}
39+
40+
fetchCache = new Map<string, FetchCache>()
2741

2842
externalizeCache = new Map<string, Promise<string | false>>()
2943

@@ -33,8 +47,6 @@ export class ViteNodeServer {
3347
public server: ViteDevServer,
3448
public options: ViteNodeServerOptions = {},
3549
) {
36-
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
37-
// @ts-ignore ssr is not typed in Vite 2, but defined in Vite 3, so we can't use expect-error
3850
const ssrOptions = server.config.ssr
3951

4052
options.deps ??= {}
@@ -120,33 +132,37 @@ export class ViteNodeServer {
120132
}
121133

122134
async fetchModule(id: string, transformMode?: 'web' | 'ssr'): Promise<FetchResult> {
123-
id = normalizeModuleId(id)
135+
const moduleId = normalizeModuleId(id)
136+
const mode = transformMode || this.getTransformMode(id)
137+
const promiseMap = this.fetchPromiseMap[mode]
124138
// reuse transform for concurrent requests
125-
if (!this.fetchPromiseMap.has(id)) {
126-
this.fetchPromiseMap.set(id,
127-
this._fetchModule(id, transformMode)
139+
if (!promiseMap.has(moduleId)) {
140+
promiseMap.set(moduleId,
141+
this._fetchModule(moduleId, mode)
128142
.then((r) => {
129143
return this.options.sourcemap !== true ? { ...r, map: undefined } : r
130144
})
131145
.finally(() => {
132-
this.fetchPromiseMap.delete(id)
146+
promiseMap.delete(moduleId)
133147
}),
134148
)
135149
}
136-
return this.fetchPromiseMap.get(id)!
150+
return promiseMap.get(moduleId)!
137151
}
138152

139-
async transformRequest(id: string, filepath = id) {
153+
async transformRequest(id: string, filepath = id, transformMode?: 'web' | 'ssr') {
154+
const mode = transformMode || this.getTransformMode(id)
155+
const promiseMap = this.transformPromiseMap[mode]
140156
// reuse transform for concurrent requests
141-
if (!this.transformPromiseMap.has(id)) {
142-
this.transformPromiseMap.set(id,
143-
this._transformRequest(id, filepath)
157+
if (!promiseMap.has(id)) {
158+
promiseMap.set(id,
159+
this._transformRequest(id, filepath, mode)
144160
.finally(() => {
145-
this.transformPromiseMap.delete(id)
161+
promiseMap.delete(id)
146162
}),
147163
)
148164
}
149-
return this.transformPromiseMap.get(id)!
165+
return promiseMap.get(id)!
150166
}
151167

152168
async transformModule(id: string, transformMode?: 'web' | 'ssr') {
@@ -175,7 +191,7 @@ export class ViteNodeServer {
175191
return 'web'
176192
}
177193

178-
private async _fetchModule(id: string, transformMode?: 'web' | 'ssr'): Promise<FetchResult> {
194+
private async _fetchModule(id: string, transformMode: 'web' | 'ssr'): Promise<FetchResult> {
179195
let result: FetchResult
180196

181197
const cacheDir = this.options.deps?.cacheDir
@@ -194,7 +210,7 @@ export class ViteNodeServer {
194210

195211
const module = this.server.moduleGraph.getModuleById(id)
196212
const timestamp = module ? module.lastHMRTimestamp : null
197-
const cache = this.fetchCache.get(filePath)
213+
const cache = this.fetchCaches[transformMode].get(filePath)
198214
if (timestamp && cache && cache.timestamp >= timestamp)
199215
return cache.result
200216

@@ -212,11 +228,14 @@ export class ViteNodeServer {
212228
result = { code: r?.code, map: r?.map as any }
213229
}
214230

215-
this.fetchCache.set(filePath, {
231+
const cacheEntry = {
216232
duration,
217233
timestamp: time,
218234
result,
219-
})
235+
}
236+
237+
this.fetchCaches[transformMode].set(filePath, cacheEntry)
238+
this.fetchCache.set(filePath, cacheEntry)
220239

221240
return result
222241
}
@@ -229,7 +248,7 @@ export class ViteNodeServer {
229248
})
230249
}
231250

232-
private async _transformRequest(id: string, filepath: string, customTransformMode?: 'web' | 'ssr') {
251+
private async _transformRequest(id: string, filepath: string, transformMode: 'web' | 'ssr') {
233252
debugRequest(id)
234253

235254
let result: TransformResult | null = null
@@ -240,8 +259,6 @@ export class ViteNodeServer {
240259
return result
241260
}
242261

243-
const transformMode = customTransformMode ?? this.getTransformMode(id)
244-
245262
if (transformMode === 'web') {
246263
// for components like Vue, we want to use the client side
247264
// plugins but then convert the code to be consumed by the server

0 commit comments

Comments
 (0)