Skip to content

Commit

Permalink
chore: revert ssrModuleLoader changes from vitejs#3473
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jun 24, 2021
1 parent 4ff7a39 commit 88d44c0
Showing 1 changed file with 8 additions and 34 deletions.
42 changes: 8 additions & 34 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
ssrImportMetaKey,
ssrDynamicImportKey
} from './ssrTransform'
import { transformRequest, TransformResult } from '../server/transformRequest'
import { ModuleNode } from '../server/moduleGraph'
import { transformRequest } from '../server/transformRequest'

interface SSRContext {
global: NodeJS.Global
Expand Down Expand Up @@ -43,14 +42,6 @@ export async function ssrLoadModule(
// request to that module are simply waiting on the same promise.
const pending = pendingModules.get(url)
if (pending) {
const { moduleGraph } = server
const mod = await moduleGraph.ensureEntryFromUrl(url)
const transformResult = await transformModule(mod, server)
const deps = (transformResult.deps || []).map((d) => unwrapId(d))
const circularDep = urlStack.find((u) => deps.includes(u))
if (circularDep) {
return {}
}
return pending
}

Expand Down Expand Up @@ -78,16 +69,20 @@ async function instantiateModule(
return mod.ssrModule
}

const result = await transformModule(mod, server)
const result =
mod.ssrTransformResult ||
(await transformRequest(url, server, { ssr: true }))
if (!result) {
// TODO more info? is this even necessary?
throw new Error(`failed to load module for ssr: ${url}`)
}

urlStack = urlStack.concat(url)

const ssrModule = {
[Symbol.toStringTag]: 'Module'
}
Object.defineProperty(ssrModule, '__esModule', { value: true })
// Set immediately so the module is available in case of circular dependencies.
mod.ssrModule = ssrModule

// Tolerate circular imports by ensuring the module can be
// referenced before it's been instantiated.
Expand Down Expand Up @@ -213,24 +208,3 @@ function resolve(id: string, importer: string | null, root: string) {
resolveCache.set(key, resolved)
return resolved
}

const pendingTransforms = new Map<string, Promise<TransformResult | null>>()

async function transformModule(mod: ModuleNode, server: ViteDevServer) {
const url = mod.url
let transformResult = mod.ssrTransformResult
if (!transformResult) {
let transformPromise = pendingTransforms.get(url)
if (!transformPromise) {
transformPromise = transformRequest(url, server, { ssr: true })
pendingTransforms.set(url, transformPromise)
transformPromise.catch(() => {}).then(() => pendingTransforms.delete(url))
}
transformResult = await transformPromise
}
if (!transformResult) {
// TODO more info? is this even necessary?
throw new Error(`failed to load module for ssr: ${url}`)
}
return transformResult
}

0 comments on commit 88d44c0

Please sign in to comment.