Skip to content

Commit

Permalink
refactor(runtime): minor tweaks (#15904)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Feb 14, 2024
1 parent 75ddc0e commit 63a39c2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ app.use('*', async (req, res, next) => {
// ESM source code to be usable in Node.js! There is no bundling
// required, and provides efficient invalidation similar to HMR.
const { render } = await vite.ssrLoadModule('/src/entry-server.js')
// 3b. Since Vite 5.1, you can use createViteRuntime API instead.
// 3b. Since Vite 5.1, you can use the experimental createViteRuntime API
// instead.
// It fully supports HMR and works in a simillar way to ssrLoadModule
// More advanced use case would be creating a runtime in a separate
// thread or even a different machine using ViteRuntime class
Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/ssr/fetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ SOURCEMAPPING_URL += 'ppingURL'
const VITE_RUNTIME_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-runtime'
const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`

const OTHER_SOURCE_MAP_REGEXP = new RegExp(
`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`,
'gm',
)

function inlineSourceMap(
mod: ModuleNode,
result: TransformResult,
Expand All @@ -139,11 +144,8 @@ function inlineSourceMap(
return result

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

const sourceMap = Buffer.from(
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/ssr/runtime/moduleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
update(fsPath: string, mod: ModuleCache): this {
fsPath = this.normalize(fsPath)
if (!super.has(fsPath)) this.setByModuleId(fsPath, mod)
else Object.assign(super.get(fsPath) as ModuleCache, mod)
else Object.assign(super.get(fsPath)!, mod)
return this
}

Expand All @@ -50,7 +50,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
importers: new Set(),
})
}
return mod as ModuleCache
return mod
}

override get(fsPath: string): ModuleCache {
Expand Down
29 changes: 14 additions & 15 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,20 @@ export class ViteRuntime {
return this.processImport(mod.exports, fetchedModule, metadata)
}

const getStack = () =>
`stack:\n${[...callstack, moduleId]
.reverse()
.map((p) => ` - ${p}`)
.join('\n')}`

let debugTimer: any
if (this.debug)
debugTimer = setTimeout(
() =>
this.debug!(
`[vite-runtime] module ${moduleId} takes over 2s to load.\n${getStack()}`,
),
2000,
)
if (this.debug) {
debugTimer = setTimeout(() => {
const getStack = () =>
`stack:\n${[...callstack, moduleId]
.reverse()
.map((p) => ` - ${p}`)
.join('\n')}`

this.debug!(
`[vite-runtime] module ${moduleId} takes over 2s to load.\n${getStack()}`,
)
}, 2000)
}

try {
// cached module
Expand Down Expand Up @@ -266,7 +265,7 @@ export class ViteRuntime {
this.debug?.('[vite-runtime] fetching', id)
// fast return for established externalized patterns
const fetchedModule = id.startsWith('data:')
? ({ externalize: id, type: 'builtin' } as FetchResult)
? ({ externalize: id, type: 'builtin' } satisfies FetchResult)
: await this.options.fetchModule(id, importer)
// base moduleId on "file" and not on id
// if `import(variable)` is called it's possible that it doesn't have an extension for example
Expand Down
2 changes: 1 addition & 1 deletion playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export const ports = {
ssr: 9600,
'ssr-deps': 9601,
'ssr-html': 9602,
'ssr-hmr': 9609, // not imported but used in `ssr-hmr/vite.config.js`
'ssr-noexternal': 9603,
'ssr-pug': 9604,
'ssr-webworker': 9605,
'proxy-bypass': 9606, // not imported but used in `proxy-hmr/vite.config.js`
'proxy-bypass/non-existent-app': 9607, // not imported but used in `proxy-hmr/other-app/vite.config.js`
'ssr-hmr': 9609, // not imported but used in `hmr-ssr/__tests__/hmr.spec.ts`
'proxy-hmr': 9616, // not imported but used in `proxy-hmr/vite.config.js`
'proxy-hmr/other-app': 9617, // not imported but used in `proxy-hmr/other-app/vite.config.js`
'ssr-conditions': 9620,
Expand Down

0 comments on commit 63a39c2

Please sign in to comment.