Skip to content

Commit b283663

Browse files
fix(vite): prevent race-condition when importing @vitejs/plugin-vue (#33307)
This PR primes the cache for `@vitejs/plugin-vue`, similar to how we already do for `esbuild`. When `vite.config.ts` is compiled into CJS, then doing `require('@vitejs/plugin-vue')` may error with a race condition if an `import` of the same module is in progress. Fixes #NXC-3289 --------- Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
1 parent 4ec3a99 commit b283663

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/vite/src/plugins/plugin.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ async function buildViteTargets(
203203
} catch {
204204
// do nothing
205205
}
206+
// Workaround for race condition with ESM-only Vite plugins (e.g. @vitejs/plugin-vue@6+)
207+
// If vite.config.ts is compiled as CJS, then when both require('@vitejs/plugin-vue') and import('@vitejs/plugin-vue')
208+
// are pending in the same process, Node will throw an error:
209+
// Error [ERR_INTERNAL_ASSERTION]: Cannot require() ES Module @vitejs/plugin-vue/dist/index.js because it is not yet fully loaded.
210+
// This may be caused by a race condition if the module is simultaneously dynamically import()-ed via Promise.all().
211+
try {
212+
const importVuePlugin = () =>
213+
new Function('return import("@vitejs/plugin-vue")')();
214+
await importVuePlugin();
215+
} catch {
216+
// Plugin not installed or not needed, ignore
217+
}
206218
const { resolveConfig } = await loadViteDynamicImport();
207219
const viteBuildConfig = await resolveConfig(
208220
{

0 commit comments

Comments
 (0)