Skip to content

Commit

Permalink
fix: race condition creation module in graph in transformRequest (#13085
Browse files Browse the repository at this point in the history
)
  • Loading branch information
patak-dev committed Jun 8, 2023
1 parent d458ccd commit 43cbbcf
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { performance } from 'node:perf_hooks'
import getEtag from 'etag'
import convertSourceMap from 'convert-source-map'
import type { SourceDescription, SourceMap } from 'rollup'
import type { PartialResolvedId, SourceDescription, SourceMap } from 'rollup'
import colors from 'picocolors'
import type { ModuleNode, ViteDevServer } from '..'
import {
Expand Down Expand Up @@ -141,13 +141,22 @@ async function doTransform(
return cached
}

const resolved = module
? undefined
: (await pluginContainer.resolveId(url, undefined, { ssr })) ?? undefined

// resolve
const id =
module?.id ??
(await pluginContainer.resolveId(url, undefined, { ssr }))?.id ??
url
const id = module?.id ?? resolved?.id ?? url

const result = loadAndTransform(id, url, server, options, timestamp)
const result = loadAndTransform(
id,
url,
server,
options,
timestamp,
module,
resolved,
)

getDepsOptimizer(config, ssr)?.delayDepsOptimizerUntil(id, () => result)

Expand All @@ -160,6 +169,8 @@ async function loadAndTransform(
server: ViteDevServer,
options: TransformOptions,
timestamp: number,
mod?: ModuleNode,
resolved?: PartialResolvedId,
) {
const { config, pluginContainer, moduleGraph, watcher } = server
const { root, logger } = config
Expand Down Expand Up @@ -243,7 +254,7 @@ async function loadAndTransform(
throw err
}
// ensure module in graph after successful load
const mod = await moduleGraph.ensureEntryFromUrl(url, ssr)
mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved)
ensureWatchedFile(watcher, mod.file, root)

// transform
Expand Down

0 comments on commit 43cbbcf

Please sign in to comment.