Galooshi / import-js

A tool to simplify importing JS modules
MIT License
524 stars 70 forks source link

Configuration to also look for .importjs.cjs and .importjs.mjs #589

Closed mikabytes closed 7 months ago

mikabytes commented 7 months ago

When working on a project using ES modules ("type": "module"), and using a .importjs.js file, then there's an error

Unable to parse configuration file. Reason:                                                                                                                   
Error [ERR_REQUIRE_ESM]: require() of ES Module /.importjs.js from /lib/node_modules/import-js/build/FileUtils.js not supported.
.importjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename .importjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.readJsFile (/import-js/build/FileUtils.js:35:12)

However, following the suggestion to rename the file into .importjs.cjs will fail because ImportJS does not look for that file.

That's what this PR fixes.

lencioni commented 7 months ago

Thanks! Will you please add a test?

mikabytes commented 7 months ago

Sure, I've added tests now. However, there is currently one failing test on master branch for the Importer module. I haven't touched that, but it's also failing on this branch now.

The codeclimate bot regarded the loadUserConfig code as too complex so I split it into three functions. Then tests got unhappy with me not using this in one of them, so I had to move it out of the class. Ideally, I would have liked them to remain close together.

Codeclimate still doesn't like the code even though it's simpler now than it was before. I'm out of time, so if someone wants to find a way to appease it then please go ahead.

The tests disliked using a for-of loop for iterating through the array, so I replaced that with something from the 80s... All these issues combined make it less fun for me to contribute.

lencioni commented 7 months ago

This is now published in v5.1.0. Thanks again for the contribution!

mikabytes commented 7 months ago

Great, thanks @lencioni !

Hmm, I might be interested in being a maintainer for this project. I've been using it for many years and it's one of those projects I would love to help keep alive.

Shall we get into a chat and discuss the expectations?

lencioni commented 7 months ago

I'm happy to get into a chat and discuss if you like, but first I'll just outline it here.

I haven't used this project in a while, so I'm no longer an ideal maintainer here and I think it would be great to have someone like you, who is currently benefiting from this project to help maintain it since it is in your best interest.

Mostly, I expect that you will treat people with respect in your interactions here (which I think you've already demonstrated), and help improve this project in ways that you think are good. If you want to make any really big changes or want feedback on an idea, run it past me and @trotzig first in an issue.

mikabytes commented 7 months ago

I think that's all reasonable and well-aligned with how I like to do things. If you see me do anything annoying, just let me know and we'll sort it out.

I have some motivation to make the DX a bit better but nothing too invasive as of now. I'll probably have a look at some stale issues/PRs and see if there's something I can do there. Just generally get the project "freshness" up a bit.

Thank you for trusting me

trotzig commented 7 months ago

Thank you so much for the work you're doing here! I'm still a user but I have to admit I haven't looked at this project in many many years (although I use it every day). I'm happy to review and provide feedback on changes when I have time. As @lencioni said, we know that you take good care of code and people. 🙂

mikabytes commented 7 months ago

Happy to hear that @trotzig :-)

I started having a look around and I'm creating new issues as I go along. I'll be looking forward to hearing your feedback.