From 7f01a00a73cd3962fc468273156943a7fe0bc7f9 Mon Sep 17 00:00:00 2001 From: Rom Date: Tue, 19 Jul 2022 21:54:50 +0200 Subject: [PATCH] fix: backport make `resolveConfig()` concurrent safe (#9224) (#9229) --- packages/vite/src/node/config.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1df307d6c017f4..fa7fd5e19772f9 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -925,7 +925,6 @@ export async function loadConfigFromFile( let userConfig: UserConfigExport | undefined if (isESM) { - const fileUrl = require('url').pathToFileURL(resolvedPath) const bundled = await bundleConfigFile(resolvedPath, true) dependencies = bundled.dependencies if (isTS) { @@ -933,12 +932,15 @@ export async function loadConfigFromFile( // with --experimental-loader themselves, we have to do a hack here: // bundle the config file w/ ts transforms first, write it to disk, // load it with native Node ESM, then delete the file. - fs.writeFileSync(resolvedPath + '.js', bundled.code) - userConfig = (await dynamicImport(`${fileUrl}.js?t=${Date.now()}`)) - .default - fs.unlinkSync(resolvedPath + '.js') + const fileBase = `${resolvedPath}.timestamp-${Date.now()}` + const fileNameTmp = `${fileBase}.js` + const fileUrl = `${require('url').pathToFileURL(fileBase)}.js` + fs.writeFileSync(fileNameTmp, bundled.code) + userConfig = (await dynamicImport(fileUrl)).default + fs.unlinkSync(fileNameTmp) debug(`TS + native esm config loaded in ${getTime()}`, fileUrl) } else { + const fileUrl = require('url').pathToFileURL(resolvedPath) // using Function to avoid this from being compiled away by TS/Rollup // append a query so that we force reload fresh config in case of // server restart