diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index f013ee2fcb6a19..d40f7ea019be7d 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -830,47 +830,41 @@ export async function loadConfigFromFile( const getTime = () => `${(performance.now() - start).toFixed(2)}ms` let resolvedPath: string | undefined - let isTS = false - let isESM: boolean | 'auto' = 'auto' let dependencies: string[] = [] if (configFile) { // explicit config path is always resolved from cwd resolvedPath = path.resolve(configFile) - isTS = /\.[cm]?ts$/.test(configFile) - - if (/\.m[jt]s$/.test(configFile)) { - isESM = true - } else if (/\.c[jt]s$/.test(configFile)) { - isESM = false - } } else { // implicit config file loaded from inline root (if present) // otherwise from cwd - for (const file of DEFAULT_CONFIG_FILES) { - const filePath = path.resolve(configRoot, file.filename) + for (const filename of DEFAULT_CONFIG_FILES) { + const filePath = path.resolve(configRoot, filename) if (!fs.existsSync(filePath)) continue resolvedPath = filePath - isESM = file.isESM - isTS = file.isTS break } } - // check package.json for type: "module" and set `isMjs` to true - if (isESM === 'auto') { + if (!resolvedPath) { + debug('no config file found.') + return null + } + + let isESM = false + if (/\.m[jt]s$/.test(resolvedPath)) { + isESM = true + } else if (/\.c[jt]s$/.test(resolvedPath)) { + isESM = false + } else { + // check package.json for type: "module" and set `isMjs` to true try { const pkg = lookupFile(configRoot, ['package.json']) isESM = !!pkg && JSON.parse(pkg).type === 'module' } catch (e) {} } - if (!resolvedPath) { - debug('no config file found.') - return null - } - try { let userConfig: UserConfigExport | undefined @@ -878,6 +872,8 @@ export async function loadConfigFromFile( const fileUrl = pathToFileURL(resolvedPath) const bundled = await bundleConfigFile(resolvedPath, true) dependencies = bundled.dependencies + + const isTS = /\.[cm]?ts$/.test(resolvedPath) if (isTS) { // before we can register loaders without requiring users to run node // with --experimental-loader themselves, we have to do a hack here: diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index 38b627386fe1e4..d85130aba14499 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -21,13 +21,13 @@ export const DEFAULT_EXTENSIONS = [ ] export const DEFAULT_CONFIG_FILES = [ - { filename: 'vite.config.js', isESM: 'auto', isTS: false }, - { filename: 'vite.config.mjs', isESM: true, isTS: false }, - { filename: 'vite.config.ts', isESM: 'auto', isTS: true }, - { filename: 'vite.config.cjs', isESM: false, isTS: false }, - { filename: 'vite.config.mts', isESM: true, isTS: true }, - { filename: 'vite.config.cts', isESM: false, isTS: true } -] as const + 'vite.config.js', + 'vite.config.mjs', + 'vite.config.ts', + 'vite.config.cjs', + 'vite.config.mts', + 'vite.config.cts' +] export const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/