Skip to content

Commit

Permalink
fix(react-rsbuild): avoid race condition in async (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework authored May 17, 2024
1 parent fc71882 commit f188a57
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions packages/react-rsbuild/src/loaders/react-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,23 @@ const defaultHandlers = Object.values(docgenHandlers).map((handler) => handler)
const defaultResolver = new docgenResolver.FindExportedDefinitionsResolver()
const handlers = [...defaultHandlers, actualNameHandler]

let tsconfigPathsInitialized = false
let tsconfigPathsInitializeStatus:
| 'uninitialized'
| 'initializing'
| 'initialized' = 'uninitialized'

let resolveTsconfigPathsInitialingPromise: (
value: void | PromiseLike<void>,
) => void
let tsconfigPathsInitialingPromise = new Promise<void>((resolve) => {
resolveTsconfigPathsInitialingPromise = resolve
})

let finishInitialization = () => {
resolveTsconfigPathsInitialingPromise()
tsconfigPathsInitializeStatus = 'initialized'
}

let matchPath: TsconfigPaths.MatchPath | undefined

export default async function reactDocgenLoader(
Expand All @@ -89,7 +105,8 @@ export default async function reactDocgenLoader(
const options = this.getOptions() || {}
const { debug = false } = options

if (!tsconfigPathsInitialized) {
if (tsconfigPathsInitializeStatus === 'uninitialized') {
tsconfigPathsInitializeStatus = 'initializing'
const tsconfigPath = await findUp('tsconfig.json', { cwd: process.cwd() })
const tsconfig = TsconfigPaths.loadConfig(tsconfigPath)

Expand All @@ -102,7 +119,11 @@ export default async function reactDocgenLoader(
)
}

tsconfigPathsInitialized = true
finishInitialization()
}

if (tsconfigPathsInitializeStatus === 'initializing') {
await tsconfigPathsInitialingPromise
}

try {
Expand Down

0 comments on commit f188a57

Please sign in to comment.