/
server.ts
151 lines (125 loc) · 4.49 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { join } from 'pathe'
import type { TransformResult, ViteDevServer } from 'vite'
import createDebug from 'debug'
import type { FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
import { shouldExternalize } from './externalize'
import { toArray, toFilePath, withInlineSourcemap } from './utils'
export * from './externalize'
const debugRequest = createDebug('vite-node:server:request')
// store the original reference to avoid it been mocked
const RealDate = Date
export class ViteNodeServer {
private fetchPromiseMap = new Map<string, Promise<FetchResult>>()
private transformPromiseMap = new Map<string, Promise<TransformResult | null | undefined>>()
fetchCache = new Map<string, {
timestamp: number
result: FetchResult
}>()
constructor(
public server: ViteDevServer,
public options: ViteNodeServerOptions = {},
) {
// @ts-expect-error ssr is not typed
const ssrOptions = server.config.ssr
if (ssrOptions) {
options.deps ??= {}
// we don't externalize ssr, because it has different semantics in Vite
// if (ssrOptions.external) {
// options.deps.external ??= []
// options.deps.external.push(...ssrOptions.external)
// }
if (ssrOptions.noExternal === true) {
options.deps.inline ??= true
}
else if (options.deps.inline !== true) {
options.deps.inline ??= []
options.deps.inline.push(...toArray(ssrOptions.noExternal))
}
}
}
shouldExternalize(id: string) {
return shouldExternalize(id, this.options.deps)
}
async resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null> {
if (importer && !importer.startsWith(this.server.config.root))
importer = join(this.server.config.root, importer)
return this.server.pluginContainer.resolveId(id, importer, { ssr: true })
}
async fetchModule(id: string): Promise<FetchResult> {
// reuse transform for concurrent requests
if (!this.fetchPromiseMap.has(id)) {
this.fetchPromiseMap.set(id,
this._fetchModule(id)
.then((r) => {
return this.options.sourcemap !== true ? { ...r, map: undefined } : r
})
.finally(() => {
this.fetchPromiseMap.delete(id)
}),
)
}
return this.fetchPromiseMap.get(id)!
}
async transformRequest(id: string) {
// reuse transform for concurrent requests
if (!this.transformPromiseMap.has(id)) {
this.transformPromiseMap.set(id,
this._transformRequest(id)
.finally(() => {
this.transformPromiseMap.delete(id)
}),
)
}
return this.transformPromiseMap.get(id)!
}
getTransformMode(id: string) {
const withoutQuery = id.split('?')[0]
if (this.options.transformMode?.web?.some(r => withoutQuery.match(r)))
return 'web'
if (this.options.transformMode?.ssr?.some(r => withoutQuery.match(r)))
return 'ssr'
if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
return 'ssr'
return 'web'
}
private async _fetchModule(id: string): Promise<FetchResult> {
let result: FetchResult
const filePath = toFilePath(id, this.server.config.root)
const module = this.server.moduleGraph.getModuleById(id)
const timestamp = module?.lastHMRTimestamp || RealDate.now()
const cache = this.fetchCache.get(filePath)
if (timestamp && cache && cache.timestamp >= timestamp)
return cache.result
const externalize = await this.shouldExternalize(filePath)
if (externalize) {
result = { externalize }
}
else {
const r = await this._transformRequest(id)
result = { code: r?.code, map: r?.map as unknown as RawSourceMap }
}
this.fetchCache.set(filePath, {
timestamp,
result,
})
return result
}
private async _transformRequest(id: string) {
debugRequest(id)
let result: TransformResult | null = null
if (this.getTransformMode(id) === 'web') {
// for components like Vue, we want to use the client side
// plugins but then convert the code to be consumed by the server
result = await this.server.transformRequest(id)
if (result)
result = await this.server.ssrTransform(result.code, result.map, id)
}
else {
result = await this.server.transformRequest(id, { ssr: true })
}
const sourcemap = this.options.sourcemap ?? 'inline'
if (sourcemap === 'inline' && result && !id.includes('node_modules'))
withInlineSourcemap(result)
return result
}
}