Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 24, 2022
1 parent 9faa456 commit c1af66d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
36 changes: 16 additions & 20 deletions packages/vite/src/node/config.ts
Expand Up @@ -830,54 +830,50 @@ 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

if (isESM) {
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:
Expand Down
14 changes: 7 additions & 7 deletions packages/vite/src/node/constants.ts
Expand Up @@ -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$/

Expand Down

0 comments on commit c1af66d

Please sign in to comment.