@@ -116,20 +116,16 @@ Promise.resolve()
116116// file specified by the user. It checks for config
117117// files in the following order:
118118// - no extension (JSON)
119+ // - cjs extension (JavaScript)
119120// - js extension (JavaScript)
120121// - json extension (JSON)
121122function loadConfig ( configPath ) {
122123 return new Promise ( ( resolve , reject ) => {
123124 configPath = resolveConfigPath ( configPath ) ;
124125 let config ;
125126 try {
126- config = loadLocalConfigUnmodified ( configPath ) ;
127- if ( ! config ) {
128- config = loadLocalConfigWithJs ( configPath ) ;
129- }
130- if ( ! config ) {
131- config = loadLocalConfigWithJson ( configPath ) ;
132- }
127+ config = loadLocalConfigUnmodified ( configPath ) || loadConfigModule ( configPath ) ;
128+
133129 if ( options . config && ! config ) {
134130 return reject ( new Error ( `The config file "${ configPath } " could not be loaded` ) ) ;
135131 }
@@ -154,8 +150,9 @@ function resolveConfigPath(configPath) {
154150 if ( ! path . isAbsolute ( configPath ) ) {
155151 configPath = path . join ( process . cwd ( ) , configPath ) ;
156152 }
157- if ( / \. j s ( o n ) ? $ / . test ( configPath ) ) {
158- configPath = configPath . replace ( / \. j s ( o n ) ? $ / , '' ) ;
153+ const extensionPattern = / \. ( c j s ) ? ( j s ) ? ( j s o n ) ? $ / ;
154+ if ( extensionPattern . test ( configPath ) ) {
155+ configPath = configPath . replace ( extensionPattern , '' ) ;
159156 }
160157 return configPath ;
161158}
@@ -172,21 +169,15 @@ function loadLocalConfigUnmodified(configPath) {
172169 }
173170}
174171
175- // Load the config file but adding a .js extension
176- function loadLocalConfigWithJs ( configPath ) {
177- try {
178- return require ( `${ configPath } .js` ) ;
179- } catch ( error ) {
180- if ( error . code !== 'MODULE_NOT_FOUND' ) {
181- throw error ;
182- }
183- }
172+ function loadConfigModule ( pathWithoutExtension ) {
173+ return loadConfigModuleWithExtension ( pathWithoutExtension , 'cjs' ) ||
174+ loadConfigModuleWithExtension ( pathWithoutExtension , 'js' ) ||
175+ loadConfigModuleWithExtension ( pathWithoutExtension , 'json' ) ;
184176}
185177
186- // Load the config file but adding a .json extension
187- function loadLocalConfigWithJson ( configPath ) {
178+ function loadConfigModuleWithExtension ( pathWithoutExtension , extension ) {
188179 try {
189- return require ( `${ configPath } .json ` ) ;
180+ return require ( `${ pathWithoutExtension } . ${ extension } ` ) ;
190181 } catch ( error ) {
191182 if ( error . code !== 'MODULE_NOT_FOUND' ) {
192183 throw error ;
0 commit comments