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

Use module.exports export to detect CJS config #2461

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

davidjgoss
Copy link
Contributor

@davidjgoss davidjgoss commented Dec 29, 2024

🤔 What's changed?

Node.js 23 introduced a module.exports export on the wrapper namespace for CommonJS modules when imported. The motivation for this seems to have mostly been around the new support for using require() to load ES modules in some circumstances. But a side effect is that it provides a heuristic as to whether what you imported is from CommonJS or ESM.

This is useful for us in cases where we find a configuration file with a .js suffix and want to know whether it's CommonJS or ESM since we have to process it a little differently. Currently we use read-pkg-up to check if the containing package has type=module but this is brittle, fussy and adds an extra dependency.

This PR changes the config-loading code to check the for the module.exports instead, simplifying the code and jettisoning a dependency. Currently it only works on Node.js 23.x - as you can see from failing builds for other versions - but in future this could become viable. We'd then only be using require() where explicitly requested for loading support code, which is where we want to be.

🏷️ What kind of change is this?

  • 🏦 Refactoring/debt/DX (improvement to code design, tooling, etc. without changing behaviour)

📋 Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

@davidjgoss davidjgoss changed the title Config import rework Use module.exports export to detect CJS config Dec 29, 2024
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.

1 participant