Skip to content

Commit 63a39c2

Browse files
authored
refactor(runtime): minor tweaks (#15904)
1 parent 75ddc0e commit 63a39c2

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

docs/guide/ssr.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ app.use('*', async (req, res, next) => {
129129
// ESM source code to be usable in Node.js! There is no bundling
130130
// required, and provides efficient invalidation similar to HMR.
131131
const { render } = await vite.ssrLoadModule('/src/entry-server.js')
132-
// 3b. Since Vite 5.1, you can use createViteRuntime API instead.
132+
// 3b. Since Vite 5.1, you can use the experimental createViteRuntime API
133+
// instead.
133134
// It fully supports HMR and works in a simillar way to ssrLoadModule
134135
// More advanced use case would be creating a runtime in a separate
135136
// thread or even a different machine using ViteRuntime class

packages/vite/src/node/ssr/fetchModule.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ SOURCEMAPPING_URL += 'ppingURL'
123123
const VITE_RUNTIME_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime'
124124
const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
125125

126+
const OTHER_SOURCE_MAP_REGEXP = new RegExp(
127+
`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`,
128+
'gm',
129+
)
130+
126131
function inlineSourceMap(
127132
mod: ModuleNode,
128133
result: TransformResult,
@@ -139,11 +144,8 @@ function inlineSourceMap(
139144
return result
140145

141146
// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
142-
const OTHER_SOURCE_MAP_REGEXP = new RegExp(
143-
`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`,
144-
'gm',
145-
)
146-
while (OTHER_SOURCE_MAP_REGEXP.test(code))
147+
OTHER_SOURCE_MAP_REGEXP.lastIndex = 0
148+
if (OTHER_SOURCE_MAP_REGEXP.test(code))
147149
code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')
148150

149151
const sourceMap = Buffer.from(

packages/vite/src/node/ssr/runtime/moduleCache.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
2828
update(fsPath: string, mod: ModuleCache): this {
2929
fsPath = this.normalize(fsPath)
3030
if (!super.has(fsPath)) this.setByModuleId(fsPath, mod)
31-
else Object.assign(super.get(fsPath) as ModuleCache, mod)
31+
else Object.assign(super.get(fsPath)!, mod)
3232
return this
3333
}
3434

@@ -50,7 +50,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
5050
importers: new Set(),
5151
})
5252
}
53-
return mod as ModuleCache
53+
return mod
5454
}
5555

5656
override get(fsPath: string): ModuleCache {

packages/vite/src/node/ssr/runtime/runtime.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,20 @@ export class ViteRuntime {
218218
return this.processImport(mod.exports, fetchedModule, metadata)
219219
}
220220

221-
const getStack = () =>
222-
`stack:\n${[...callstack, moduleId]
223-
.reverse()
224-
.map((p) => ` - ${p}`)
225-
.join('\n')}`
226-
227221
let debugTimer: any
228-
if (this.debug)
229-
debugTimer = setTimeout(
230-
() =>
231-
this.debug!(
232-
`[vite-runtime] module ${moduleId} takes over 2s to load.\n${getStack()}`,
233-
),
234-
2000,
235-
)
222+
if (this.debug) {
223+
debugTimer = setTimeout(() => {
224+
const getStack = () =>
225+
`stack:\n${[...callstack, moduleId]
226+
.reverse()
227+
.map((p) => ` - ${p}`)
228+
.join('\n')}`
229+
230+
this.debug!(
231+
`[vite-runtime] module ${moduleId} takes over 2s to load.\n${getStack()}`,
232+
)
233+
}, 2000)
234+
}
236235

237236
try {
238237
// cached module
@@ -266,7 +265,7 @@ export class ViteRuntime {
266265
this.debug?.('[vite-runtime] fetching', id)
267266
// fast return for established externalized patterns
268267
const fetchedModule = id.startsWith('data:')
269-
? ({ externalize: id, type: 'builtin' } as FetchResult)
268+
? ({ externalize: id, type: 'builtin' } satisfies FetchResult)
270269
: await this.options.fetchModule(id, importer)
271270
// base moduleId on "file" and not on id
272271
// if `import(variable)` is called it's possible that it doesn't have an extension for example

playground/test-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ export const ports = {
3232
ssr: 9600,
3333
'ssr-deps': 9601,
3434
'ssr-html': 9602,
35-
'ssr-hmr': 9609, // not imported but used in `ssr-hmr/vite.config.js`
3635
'ssr-noexternal': 9603,
3736
'ssr-pug': 9604,
3837
'ssr-webworker': 9605,
3938
'proxy-bypass': 9606, // not imported but used in `proxy-hmr/vite.config.js`
4039
'proxy-bypass/non-existent-app': 9607, // not imported but used in `proxy-hmr/other-app/vite.config.js`
40+
'ssr-hmr': 9609, // not imported but used in `hmr-ssr/__tests__/hmr.spec.ts`
4141
'proxy-hmr': 9616, // not imported but used in `proxy-hmr/vite.config.js`
4242
'proxy-hmr/other-app': 9617, // not imported but used in `proxy-hmr/other-app/vite.config.js`
4343
'ssr-conditions': 9620,

0 commit comments

Comments
 (0)