@@ -12,29 +12,6 @@ import { UsageError, WebExtError } from './errors.js';
1212
1313const log = createLogger ( import . meta. url ) ;
1414
15- // NOTE: this error message is used in an interpolated string (while the other two help
16- // messages are being logged as is).
17- export const WARN_LEGACY_JS_EXT = [
18- 'should be renamed to ".cjs" or ".mjs" file extension to ensure its format is not ambiguous.' ,
19- 'Config files with the ".js" file extension are deprecated and will not be loaded anymore' ,
20- 'in a future web-ext major version.' ,
21- ] . join ( ' ' ) ;
22-
23- export const HELP_ERR_MODULE_FROM_ESM = [
24- 'This config file belongs to a package.json file with "type" set to "module".' ,
25- 'Change the file extension to ".cjs" or rewrite it as an ES module and use the ".mjs" file extension.' ,
26- ] . join ( ' ' ) ;
27-
28- export const HELP_ERR_IMPORTEXPORT_CJS = [
29- 'This config file is defined as an ES module, but it belongs to either a project directory' ,
30- 'with a package.json file with "type" set to "commonjs" or one without any package.json file.' ,
31- 'Change the file extension to ".mjs" to fix the config loading error.' ,
32- ] . join ( ' ' ) ;
33-
34- const ERR_IMPORT_FROM_CJS = 'Cannot use import statement outside a module' ;
35- const ERR_EXPORT_FROM_CJS = "Unexpected token 'export'" ;
36- const ERR_MODULE_FROM_ESM = 'module is not defined in ES module scope' ;
37-
3815export function applyConfigToArgv ( {
3916 argv,
4017 argvFromCLI,
@@ -145,12 +122,18 @@ export async function loadJSConfigFile(filePath) {
145122 `Loading JS config file: "${ filePath } " ` +
146123 `(resolved to "${ resolvedFilePath } ")` ,
147124 ) ;
125+
148126 if ( filePath . endsWith ( '.js' ) ) {
149- log . warn ( `WARNING: config file ${ filePath } ${ WARN_LEGACY_JS_EXT } ` ) ;
127+ throw new UsageError (
128+ ` Invalid config file "${ resolvedFilePath } ": the file extension should be` +
129+ '".cjs" or ".mjs". More information at: https://mzl.la/web-ext-config-file' ,
130+ ) ;
150131 }
132+
151133 let configObject ;
152134 try {
153135 const nonce = `${ Date . now ( ) } -${ Math . random ( ) } ` ;
136+
154137 let configModule ;
155138 if ( resolvedFilePath . endsWith ( 'package.json' ) ) {
156139 configModule = parseJSON (
@@ -165,35 +148,36 @@ export async function loadJSConfigFile(filePath) {
165148 // ES modules may expose both a default and named exports and so
166149 // we merge the named exports on top of what may have been set in
167150 // the default export.
151+ if ( filePath . endsWith ( '.cjs' ) ) {
152+ // Remove the additional 'module.exports' named export that Node.js >=
153+ // 24 is returning from the dynamic import call (in addition to being
154+ // also set on the default property as in Node.js < 24).
155+ delete esmConfigMod [ 'module.exports' ] ;
156+ }
168157 configObject = { ...configDefault , ...esmConfigMod } ;
169158 } else {
170159 configObject = { ...configModule } ;
171160 }
172161 } catch ( error ) {
173- log . debug ( 'Handling error:' , error ) ;
174- let errorMessage = error . message ;
175- if ( error . message . startsWith ( ERR_MODULE_FROM_ESM ) ) {
176- errorMessage = HELP_ERR_MODULE_FROM_ESM ;
177- } else if (
178- [ ERR_IMPORT_FROM_CJS , ERR_EXPORT_FROM_CJS ] . includes ( error . message )
179- ) {
180- errorMessage = HELP_ERR_IMPORTEXPORT_CJS ;
181- }
182- throw new UsageError (
183- `Cannot read config file: ${ resolvedFilePath } \n` +
184- `Error: ${ errorMessage } ` ,
162+ const configFileError = new UsageError (
163+ `Cannot read config file "${ resolvedFilePath } ":\n${ error } ` ,
185164 ) ;
165+ configFileError . cause = error ;
166+ throw configFileError ;
186167 }
168+
187169 if ( filePath . endsWith ( 'package.json' ) ) {
188170 log . debug ( 'Looking for webExt key inside package.json file' ) ;
189171 configObject = configObject . webExt || { } ;
190172 }
173+
191174 if ( Object . keys ( configObject ) . length === 0 ) {
192175 log . debug (
193176 `Config file ${ resolvedFilePath } did not define any options. ` +
194177 'Did you set module.exports = {...}?' ,
195178 ) ;
196179 }
180+
197181 return configObject ;
198182}
199183
0 commit comments