@@ -22,18 +22,40 @@ class WebpackCLI {
2222 constructor ( ) { }
2323
2424 async resolveConfig ( args ) {
25- const loadConfig = ( configPath ) => {
25+ const loadConfig = async ( configPath ) => {
2626 const ext = extname ( configPath ) ;
2727 const interpreted = Object . keys ( jsVariants ) . find ( ( variant ) => variant === ext ) ;
2828
2929 if ( interpreted ) {
3030 rechoir . prepare ( extensions , configPath ) ;
3131 }
3232
33- let options ;
33+ const { pathToFileURL } = require ( 'url' ) ;
34+
35+ let importESM ;
3436
3537 try {
36- options = require ( configPath ) ;
38+ importESM = new Function ( 'id' , 'return import(id);' ) ;
39+ } catch ( e ) {
40+ importESM = null ;
41+ }
42+
43+ let options ;
44+ try {
45+ try {
46+ options = require ( configPath ) ;
47+ } catch ( error ) {
48+ if ( pathToFileURL && importESM && error . code === 'ERR_REQUIRE_ESM' ) {
49+ const urlForConfig = pathToFileURL ( configPath ) ;
50+
51+ options = await importESM ( urlForConfig ) ;
52+ options = options . default ;
53+
54+ return { options, path : configPath } ;
55+ }
56+
57+ throw error ;
58+ }
3759 } catch ( error ) {
3860 logger . error ( `Failed to load '${ configPath } '` ) ;
3961 logger . error ( error ) ;
@@ -91,7 +113,7 @@ class WebpackCLI {
91113 process . exit ( 2 ) ;
92114 }
93115
94- const loadedConfig = loadConfig ( configPath ) ;
116+ const loadedConfig = await loadConfig ( configPath ) ;
95117
96118 return evaluateConfig ( loadedConfig , args ) ;
97119 } ) ,
@@ -136,7 +158,7 @@ class WebpackCLI {
136158 }
137159
138160 if ( foundDefaultConfigFile ) {
139- const loadedConfig = loadConfig ( foundDefaultConfigFile . path ) ;
161+ const loadedConfig = await loadConfig ( foundDefaultConfigFile . path ) ;
140162 const evaluatedConfig = await evaluateConfig ( loadedConfig , args ) ;
141163
142164 config . options = evaluatedConfig . options ;
0 commit comments