Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(runtime): minor tweaks #15904

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC OTHER_SOURCE_MAP_REGEXP.test(code) will only be true once, so this while can simply be if.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be several times by mistake :P


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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of super.get(fsPath) is ModuleCache | undefined.

return this
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as ModuleCache is not needed because super.get(modulePath)! is already ModuleCache type.

}

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')}`
Comment on lines +224 to +228
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this getStack function inside to avoid creating this function when this.debug is false.


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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as is not needed. Instead of satisfies, { externalize: id, type: 'builtin' as const } can be used as well. But I chose satisfies because I thought that would represent the intent more accurately.

: 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`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path was not correct.

'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