@@ -29,6 +29,7 @@ import {
29
29
dynamicImport ,
30
30
isExternalUrl ,
31
31
isObject ,
32
+ isTS ,
32
33
lookupFile ,
33
34
mergeAlias ,
34
35
mergeConfig ,
@@ -37,7 +38,12 @@ import {
37
38
} from './utils'
38
39
import { resolvePlugins } from './plugins'
39
40
import type { ESBuildOptions } from './plugins/esbuild'
40
- import { CLIENT_ENTRY , DEFAULT_ASSETS_RE , ENV_ENTRY } from './constants'
41
+ import {
42
+ CLIENT_ENTRY ,
43
+ DEFAULT_ASSETS_RE ,
44
+ DEFAULT_CONFIG_FILES ,
45
+ ENV_ENTRY
46
+ } from './constants'
41
47
import type { InternalResolveOptions , ResolveOptions } from './plugins/resolve'
42
48
import { resolvePlugin } from './plugins/resolve'
43
49
import type { LogLevel , Logger } from './logger'
@@ -825,56 +831,20 @@ export async function loadConfigFromFile(
825
831
const getTime = ( ) => `${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms`
826
832
827
833
let resolvedPath : string | undefined
828
- let isTS = false
829
- let isESM = false
830
834
let dependencies : string [ ] = [ ]
831
835
832
- // check package.json for type: "module" and set `isMjs` to true
833
- try {
834
- const pkg = lookupFile ( configRoot , [ 'package.json' ] )
835
- if ( pkg && JSON . parse ( pkg ) . type === 'module' ) {
836
- isESM = true
837
- }
838
- } catch ( e ) { }
839
-
840
836
if ( configFile ) {
841
837
// explicit config path is always resolved from cwd
842
838
resolvedPath = path . resolve ( configFile )
843
- isTS = configFile . endsWith ( '.ts' )
844
-
845
- if ( configFile . endsWith ( '.mjs' ) ) {
846
- isESM = true
847
- }
848
839
} else {
849
840
// implicit config file loaded from inline root (if present)
850
841
// otherwise from cwd
851
- const jsconfigFile = path . resolve ( configRoot , 'vite.config.js' )
852
- if ( fs . existsSync ( jsconfigFile ) ) {
853
- resolvedPath = jsconfigFile
854
- }
855
-
856
- if ( ! resolvedPath ) {
857
- const mjsconfigFile = path . resolve ( configRoot , 'vite.config.mjs' )
858
- if ( fs . existsSync ( mjsconfigFile ) ) {
859
- resolvedPath = mjsconfigFile
860
- isESM = true
861
- }
862
- }
842
+ for ( const filename of DEFAULT_CONFIG_FILES ) {
843
+ const filePath = path . resolve ( configRoot , filename )
844
+ if ( ! fs . existsSync ( filePath ) ) continue
863
845
864
- if ( ! resolvedPath ) {
865
- const tsconfigFile = path . resolve ( configRoot , 'vite.config.ts' )
866
- if ( fs . existsSync ( tsconfigFile ) ) {
867
- resolvedPath = tsconfigFile
868
- isTS = true
869
- }
870
- }
871
-
872
- if ( ! resolvedPath ) {
873
- const cjsConfigFile = path . resolve ( configRoot , 'vite.config.cjs' )
874
- if ( fs . existsSync ( cjsConfigFile ) ) {
875
- resolvedPath = cjsConfigFile
876
- isESM = false
877
- }
846
+ resolvedPath = filePath
847
+ break
878
848
}
879
849
}
880
850
@@ -883,22 +853,36 @@ export async function loadConfigFromFile(
883
853
return null
884
854
}
885
855
856
+ let isESM = false
857
+ if ( / \. m [ j t ] s $ / . test ( resolvedPath ) ) {
858
+ isESM = true
859
+ } else if ( / \. c [ j t ] s $ / . test ( resolvedPath ) ) {
860
+ isESM = false
861
+ } else {
862
+ // check package.json for type: "module" and set `isESM` to true
863
+ try {
864
+ const pkg = lookupFile ( configRoot , [ 'package.json' ] )
865
+ isESM = ! ! pkg && JSON . parse ( pkg ) . type === 'module'
866
+ } catch ( e ) { }
867
+ }
868
+
886
869
try {
887
870
let userConfig : UserConfigExport | undefined
888
871
889
872
if ( isESM ) {
890
873
const fileUrl = pathToFileURL ( resolvedPath )
891
874
const bundled = await bundleConfigFile ( resolvedPath , true )
892
875
dependencies = bundled . dependencies
893
- if ( isTS ) {
876
+
877
+ if ( isTS ( resolvedPath ) ) {
894
878
// before we can register loaders without requiring users to run node
895
879
// with --experimental-loader themselves, we have to do a hack here:
896
880
// bundle the config file w/ ts transforms first, write it to disk,
897
881
// load it with native Node ESM, then delete the file.
898
- fs . writeFileSync ( resolvedPath + '.js ' , bundled . code )
899
- userConfig = ( await dynamicImport ( `${ fileUrl } .js ?t=${ Date . now ( ) } ` ) )
882
+ fs . writeFileSync ( resolvedPath + '.mjs ' , bundled . code )
883
+ userConfig = ( await dynamicImport ( `${ fileUrl } .mjs ?t=${ Date . now ( ) } ` ) )
900
884
. default
901
- fs . unlinkSync ( resolvedPath + '.js ' )
885
+ fs . unlinkSync ( resolvedPath + '.mjs ' )
902
886
debug ( `TS + native esm config loaded in ${ getTime ( ) } ` , fileUrl )
903
887
} else {
904
888
// using Function to avoid this from being compiled away by TS/Rollup
@@ -972,7 +956,7 @@ async function bundleConfigFile(
972
956
{
973
957
name : 'inject-file-scope-variables' ,
974
958
setup ( build ) {
975
- build . onLoad ( { filter : / \. [ j t ] s $ / } , async ( args ) => {
959
+ build . onLoad ( { filter : / \. [ c m ] ? [ j t ] s $ / } , async ( args ) => {
976
960
const contents = await fs . promises . readFile ( args . path , 'utf8' )
977
961
const injectValues =
978
962
`const __dirname = ${ JSON . stringify ( path . dirname ( args . path ) ) } ;` +
@@ -982,7 +966,7 @@ async function bundleConfigFile(
982
966
) } ;`
983
967
984
968
return {
985
- loader : args . path . endsWith ( '.ts' ) ? 'ts' : 'js' ,
969
+ loader : isTS ( args . path ) ? 'ts' : 'js' ,
986
970
contents : injectValues + contents
987
971
}
988
972
} )
0 commit comments