Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 30, 2023
1 parent 7e229cd commit f6239d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isPrimitive,
normalizeModuleId,
slash,
unwrapId,
} from './utils'
import {
ssrDynamicImportKey,
Expand Down Expand Up @@ -56,7 +57,7 @@ export class ViteRuntime {
public async executeId(rawId: string): Promise<any> {
const id =
rawId[0] === '.' ? posix.resolve(this.options.root, rawId) : rawId
const fetchedModule = await this.options.fetchModule(id)
const fetchedModule = await this.options.fetchModule(unwrapId(id))
return await this.cachedRequest(id, fetchedModule, [])
}

Expand All @@ -78,12 +79,6 @@ export class ViteRuntime {

if (importee) importers.add(importee)

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

// check circular dependency
if (
callstack.includes(moduleId) ||
Expand All @@ -92,6 +87,12 @@ export class ViteRuntime {
if (mod.exports) return mod.exports
}

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

let debugTimer: any
if (this.debug)
debugTimer = setTimeout(
Expand All @@ -103,8 +104,9 @@ export class ViteRuntime {
)

try {
// TODO: confirm the theory that we don't need to wait for dynamic import (it's already cached by Node.js)
// cached module
if (mod.promise) return await mod.promise
if (!fetchedModule.externalize && mod.promise) return await mod.promise

const promise = this.directRequest(id, fetchedModule, callstack, metadata)
Object.assign(mod, { promise, evaluated: false })
Expand All @@ -131,7 +133,10 @@ export class ViteRuntime {
const mod = this.moduleCache.getByModuleId(moduleId)

const request = async (dep: string, metadata?: SSRImportMetadata) => {
const fetchedModule = await this.options.fetchModule(dep, moduleId)
const fetchedModule = await this.options.fetchModule(
unwrapId(dep),
moduleId,
)
const depModuleId = normalizeModuleId(fetchedModule.file || dep)
const depMod = this.moduleCache.getByModuleId(depModuleId)
depMod.importers!.add(moduleId)
Expand All @@ -143,6 +148,7 @@ export class ViteRuntime {
const dynamicRequest = async (dep: string) => {
// it's possible to provide an object with toString() method inside import()
dep = String(dep)
// an early return for data: urls
if (dep.startsWith('data:')) {
return this.cachedRequest(
dep,
Expand All @@ -160,7 +166,7 @@ export class ViteRuntime {
if (id in requestStubs) return requestStubs[id]

if (externalize) {
this.debug?.(externalize)
this.debug?.('[vite-runtime] externalizing', externalize)
const exports = await this.runner.runExternalModule(externalize, metadata)
mod.exports = exports
return exports
Expand Down Expand Up @@ -219,11 +225,7 @@ export class ViteRuntime {
[ssrImportMetaKey]: meta,
}

this.debug?.(href)

// remove shebang
if (transformed[0] === '#')
transformed = transformed.replace(/^#!.*/, (s) => ' '.repeat(s.length))
this.debug?.('[vite-runtime] executing', href)

await this.runner.runViteModule(context, transformed, metadata)

Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/ssr/ssrFetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export async function ssrFetchModule(
result = inlineSourceMap(mod, result)
}

// remove shebang
if (result.code[0] === '#')
result.code = result.code.replace(/^#!.*/, (s) => ' '.repeat(s.length))

return { code: result.code, file: mod.file }
}

Expand Down

0 comments on commit f6239d9

Please sign in to comment.