Skip to content

Commit

Permalink
fix: backport make resolveConfig() concurrent safe (#9224) (#9229)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Jul 19, 2022
1 parent 0d13630 commit 7f01a00
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,20 +925,22 @@ 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) {
// before we can register loaders without requiring users to run node
// 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
Expand Down

0 comments on commit 7f01a00

Please sign in to comment.