@@ -27,7 +27,6 @@ import {
27
27
dynamicImport ,
28
28
isExternalUrl ,
29
29
isObject ,
30
- isTS ,
31
30
lookupFile ,
32
31
mergeAlias ,
33
32
mergeConfig ,
@@ -816,7 +815,6 @@ export async function loadConfigFromFile(
816
815
const getTime = ( ) => `${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms`
817
816
818
817
let resolvedPath : string | undefined
819
- let dependencies : string [ ] = [ ]
820
818
821
819
if ( configFile ) {
822
820
// explicit config path is always resolved from cwd
@@ -852,42 +850,13 @@ export async function loadConfigFromFile(
852
850
}
853
851
854
852
try {
855
- let userConfig : UserConfigExport | undefined
856
-
857
- if ( isESM ) {
858
- const fileUrl = pathToFileURL ( resolvedPath )
859
- const bundled = await bundleConfigFile ( resolvedPath , true )
860
- dependencies = bundled . dependencies
861
-
862
- if ( isTS ( resolvedPath ) ) {
863
- // before we can register loaders without requiring users to run node
864
- // with --experimental-loader themselves, we have to do a hack here:
865
- // bundle the config file w/ ts transforms first, write it to disk,
866
- // load it with native Node ESM, then delete the file.
867
- fs . writeFileSync ( resolvedPath + '.mjs' , bundled . code )
868
- try {
869
- userConfig = ( await dynamicImport ( `${ fileUrl } .mjs?t=${ Date . now ( ) } ` ) )
870
- . default
871
- } finally {
872
- fs . unlinkSync ( resolvedPath + '.mjs' )
873
- }
874
- debug ( `TS + native esm config loaded in ${ getTime ( ) } ` , fileUrl )
875
- } else {
876
- // using Function to avoid this from being compiled away by TS/Rollup
877
- // append a query so that we force reload fresh config in case of
878
- // server restart
879
- userConfig = ( await dynamicImport ( `${ fileUrl } ?t=${ Date . now ( ) } ` ) ) . default
880
- debug ( `native esm config loaded in ${ getTime ( ) } ` , fileUrl )
881
- }
882
- }
883
-
884
- if ( ! userConfig ) {
885
- // Bundle config file and transpile it to cjs using esbuild.
886
- const bundled = await bundleConfigFile ( resolvedPath )
887
- dependencies = bundled . dependencies
888
- userConfig = await loadConfigFromBundledFile ( resolvedPath , bundled . code )
889
- debug ( `bundled config file loaded in ${ getTime ( ) } ` )
890
- }
853
+ const bundled = await bundleConfigFile ( resolvedPath , isESM )
854
+ const userConfig = await loadConfigFromBundledFile (
855
+ resolvedPath ,
856
+ bundled . code ,
857
+ isESM
858
+ )
859
+ debug ( `bundled config file loaded in ${ getTime ( ) } ` )
891
860
892
861
const config = await ( typeof userConfig === 'function'
893
862
? userConfig ( configEnv )
@@ -898,7 +867,7 @@ export async function loadConfigFromFile(
898
867
return {
899
868
path : normalizePath ( resolvedPath ) ,
900
869
config,
901
- dependencies
870
+ dependencies : bundled . dependencies
902
871
}
903
872
} catch ( e ) {
904
873
createLogger ( logLevel ) . error (
@@ -911,7 +880,7 @@ export async function loadConfigFromFile(
911
880
912
881
async function bundleConfigFile (
913
882
fileName : string ,
914
- isESM = false
883
+ isESM : boolean
915
884
) : Promise < { code : string ; dependencies : string [ ] } > {
916
885
const importMetaUrlVarName = '__vite_injected_original_import_meta_url'
917
886
const result = await build ( {
@@ -954,7 +923,7 @@ async function bundleConfigFile(
954
923
) } ;`
955
924
956
925
return {
957
- loader : isTS ( args . path ) ? 'ts' : 'js' ,
926
+ loader : args . path . endsWith ( 'ts' ) ? 'ts' : 'js' ,
958
927
contents : injectValues + contents
959
928
}
960
929
} )
@@ -976,22 +945,38 @@ interface NodeModuleWithCompile extends NodeModule {
976
945
const _require = createRequire ( import . meta. url )
977
946
async function loadConfigFromBundledFile (
978
947
fileName : string ,
979
- bundledCode : string
980
- ) : Promise < UserConfig > {
981
- const realFileName = fs . realpathSync ( fileName )
982
- const defaultLoader = _require . extensions [ '.js' ]
983
- _require . extensions [ '.js' ] = ( module : NodeModule , filename : string ) => {
984
- if ( filename === realFileName ) {
985
- ; ( module as NodeModuleWithCompile ) . _compile ( bundledCode , filename )
986
- } else {
987
- defaultLoader ( module , filename )
948
+ bundledCode : string ,
949
+ isESM : boolean
950
+ ) : Promise < UserConfigExport > {
951
+ // for esm, before we can register loaders without requiring users to run node
952
+ // with --experimental-loader themselves, we have to do a hack here:
953
+ // write it to disk, load it with native Node ESM, then delete the file.
954
+ if ( isESM ) {
955
+ const fileUrl = pathToFileURL ( fileName )
956
+ fs . writeFileSync ( fileName + '.mjs' , bundledCode )
957
+ try {
958
+ return ( await dynamicImport ( `${ fileUrl } .mjs?t=${ Date . now ( ) } ` ) ) . default
959
+ } finally {
960
+ fs . unlinkSync ( fileName + '.mjs' )
961
+ }
962
+ }
963
+ // for cjs, we can register a custom loader via `_require.extensions`
964
+ else {
965
+ const realFileName = fs . realpathSync ( fileName )
966
+ const defaultLoader = _require . extensions [ '.js' ]
967
+ _require . extensions [ '.js' ] = ( module : NodeModule , filename : string ) => {
968
+ if ( filename === realFileName ) {
969
+ ; ( module as NodeModuleWithCompile ) . _compile ( bundledCode , filename )
970
+ } else {
971
+ defaultLoader ( module , filename )
972
+ }
988
973
}
974
+ // clear cache in case of server restart
975
+ delete _require . cache [ _require . resolve ( fileName ) ]
976
+ const raw = _require ( fileName )
977
+ _require . extensions [ '.js' ] = defaultLoader
978
+ return raw . __esModule ? raw . default : raw
989
979
}
990
- // clear cache in case of server restart
991
- delete _require . cache [ _require . resolve ( fileName ) ]
992
- const raw = _require ( fileName )
993
- _require . extensions [ '.js' ] = defaultLoader
994
- return raw . __esModule ? raw . default : raw
995
980
}
996
981
997
982
export function getDepOptimizationConfig (
0 commit comments