-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
web-ext-config ES Module Support #2604
Comments
I had a bit of time to look at this again this afternoon. Currently the config import functionality is provided by Although there is a fork with ESM support ( diff --git a/.babelrc b/.babelrc
index 9d545e5..54e0e73 100644
--- a/.babelrc
+++ b/.babelrc
@@ -14,7 +14,8 @@
"include": [
"WEBEXT_BUILD_ENV"
]
- }]
+ }],
+ ["@babel/plugin-syntax-import-assertions"]
],
"env": {
"test": {
diff --git a/package-lock.json b/package-lock.json
index bd1d245..c328720 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -49,6 +49,7 @@
"@babel/cli": "7.20.7",
"@babel/core": "7.20.12",
"@babel/eslint-parser": "7.19.1",
+ "@babel/plugin-syntax-import-assertions": "7.20.0",
"@babel/preset-env": "7.20.2",
"@babel/preset-flow": "7.18.6",
"@babel/register": "7.18.9",
diff --git a/package.json b/package.json
index 9e6ce55..8bbcfab 100644
--- a/package.json
+++ b/package.json
@@ -95,6 +95,7 @@
"@babel/cli": "7.20.7",
"@babel/core": "7.20.12",
"@babel/eslint-parser": "7.19.1",
+ "@babel/plugin-syntax-import-assertions": "7.20.0",
"@babel/preset-env": "7.20.2",
"@babel/preset-flow": "7.18.6",
"@babel/register": "7.18.9",
diff --git a/src/config.js b/src/config.js
index 1de18a2..e2bbbc9 100644
--- a/src/config.js
+++ b/src/config.js
@@ -2,7 +2,6 @@
import os from 'os';
import path from 'path';
-import importFresh from 'import-fresh';
import camelCase from 'camelcase';
import decamelize from 'decamelize';
@@ -127,7 +126,7 @@ export function applyConfigToArgv({
return newArgv;
}
-export function loadJSConfigFile(filePath: string): Object {
+export async function loadJSConfigFile(filePath: string): Object {
const resolvedFilePath = path.resolve(filePath);
log.debug(
`Loading JS config file: "${filePath}" ` +
@@ -135,7 +134,9 @@ export function loadJSConfigFile(filePath: string): Object {
);
let configObject;
try {
- configObject = importFresh(resolvedFilePath);
+ configObject = resolvedFilePath.includes('json') ?
+ (await import(`${resolvedFilePath}?cacheBust=${new Date().getTime()}`, { assert: { type: 'json' } })).default :
+ (await import(`${resolvedFilePath}?cacheBust=${new Date().getTime()}`)).default;
} catch (error) {
log.debug('Handling error:', error);
throw new UsageError(
diff --git a/src/program.js b/src/program.js
index 89f2f21..6da4793 100644
--- a/src/program.js
+++ b/src/program.js
@@ -336,8 +336,8 @@ export class Program {
);
}
- configFiles.forEach((configFileName) => {
- const configObject = loadJSConfigFile(configFileName);
+ configFiles.forEach(async (configFileName) => {
+ const configObject = await loadJSConfigFile(configFileName);
adjustedArgv = applyConfigToArgv({
argv: adjustedArgv,
argvFromCLI: argv, This (partially) works for ES Modules but Common JS continues to throw an exception Another simpler solution (the example below fully breaks backwards compatibility, but it could be adapted to search for both diff --git a/src/config.js b/src/config.js
index 1de18a2..d632014 100644
--- a/src/config.js
+++ b/src/config.js
@@ -163,7 +163,7 @@ type DiscoverConfigFilesParams = {
export async function discoverConfigFiles({
getHomeDir = os.homedir,
}: DiscoverConfigFilesParams = {}): Promise<Array<string>> {
- const magicConfigName = 'web-ext-config.js';
+ const magicConfigName = 'web-ext-config.cjs';
// Config files will be loaded in this order.
const possibleConfigs = [ This doesn't unbreak the current exception raised when parsing a Common JS Does anybody have any thoughts on the best way to move this forward? The
But currently the only way to continue to use |
Is this a feature request or a bug?
Feature
What is the current behavior?
If you've migrated your browser extension to no longer use CommonJS modules then Node will no longer parse the
web-ext-config.js
file without additional changes.This is the correct behavior as per the
web-ext
docs:and
https://extensionworkshop.com/documentation/develop/getting-started-with-web-ext/#setting-option-defaults-in-a-configuration-file
Example
In
package.json
you have set:and you then run any
web-ext
command you get an error from Node.What is the expected or desired behavior?
Quickest win would be to add
./web-ext-config.cjs
to the pool of default files checked.Less quick would be to allow ES Modules
export {}
inweb-ext-config.{,m}js
.The workaround for now is to add
--config web-ext-config.cjs
as a suffix toweb-ext
commands.It seems setting
config
in thewebExt
block inpackage.json
doesn't work:Version information (for bug reports)
The text was updated successfully, but these errors were encountered: