Skip to content
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

fix: handle ESM-style config file #303

Merged
merged 1 commit into from
Dec 19, 2024

Conversation

quantizor
Copy link
Contributor

@quantizor quantizor commented Dec 19, 2024

Fixes happo/happo-e2e#50

Note that this PR doesn't change how the file itself is loaded to completely settle ESM compat, but it at least makes the CLI work properly when used in conjunction with NODE_OPTIONS='--experimental-require-module'

@quantizor
Copy link
Contributor Author

This is the quick & dirty patch I made via yarn patch to fix this + get rid of the need for the NODE_OPTIONS override:

--- a/build/loadUserConfig.js
+++ b/build/loadUserConfig.js
@@ -14,10 +14,12 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 async function load(pathToConfigFile) {
   try {
-    let userConfig = (0, _requireRelative.default)(pathToConfigFile, process.cwd());
+    let userConfig = await import(process.cwd() + pathToConfigFile.slice(1));
     // await if the config is a function, async or not
     if (typeof userConfig === 'function') {
       userConfig = await userConfig();
+    } else if (userConfig?.default) {
+      userConfig = userConfig.default;
     }
     return {
       ...defaultConfig,

Probably need to pull in path.resolve for the import to fix it properly.

Copy link
Contributor

@lencioni lencioni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the fix!

@lencioni
Copy link
Contributor

@quantizor would you be interested in working on a PR to handle the rest of the ESM compat?

@lencioni lencioni merged commit 14c60fe into happo:main Dec 19, 2024
4 checks passed
@quantizor
Copy link
Contributor Author

@quantizor would you be interested in working on a PR to handle the rest of the ESM compat?

Last few work days before winter vacation, this is my contribution for now 😊

@lencioni
Copy link
Contributor

Sounds great, thank you again! I'll get this published now then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM issue loading config
2 participants