Skip to content

Commit

Permalink
fix: use correct require extension to load config (#9118)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jul 15, 2022
1 parent b7b3e65 commit ebf682e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,9 +962,11 @@ async function loadConfigFromBundledFile(
}
// for cjs, we can register a custom loader via `_require.extensions`
else {
const extension = path.extname(fileName)
const realFileName = fs.realpathSync(fileName)
const defaultLoader = _require.extensions['.js']
_require.extensions['.js'] = (module: NodeModule, filename: string) => {
const loaderExt = extension in _require.extensions ? extension : '.js'
const defaultLoader = _require.extensions[loaderExt]!
_require.extensions[loaderExt] = (module: NodeModule, filename: string) => {
if (filename === realFileName) {
;(module as NodeModuleWithCompile)._compile(bundledCode, filename)
} else {
Expand All @@ -974,7 +976,7 @@ async function loadConfigFromBundledFile(
// clear cache in case of server restart
delete _require.cache[_require.resolve(fileName)]
const raw = _require(fileName)
_require.extensions['.js'] = defaultLoader
_require.extensions[loaderExt] = defaultLoader
return raw.__esModule ? raw.default : raw
}
}
Expand Down

0 comments on commit ebf682e

Please sign in to comment.