Transition to ESM #2059
davidjgoss
announced in
Announcements
Replies: 1 comment
-
Looks good 👍 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
The Node.js ecosystem is gradually moving from CommonJS (
require
,module.exports
etc) to ESM (import
,export
etc).CommonJS code can be consumed by ESM code, but ESM code cannot be consumed by CommonJS code (because ESM is async and CommonJS is sync). This means that once a package switches to ESM, your project must be ESM to use it. As more open source projects switch, life will get harder for the remaining projects as they'll be unable to upgrade dependencies or add new dependencies that have switched. Switching to ESM also unlocks nice things like top-level await.
As of 8.0.0, we added opt-in support for user code written in ES Modules, and made Cucumber a hybrid package - meaning it's delivered in CommonJS format but has an ESM entry point. This involved a few compromises and leaves us in a transitional state.
Plan
Here's the (draft, for now) plan of how we eventually make the full switch to ESM:
Phase 0 - where we are now
--require
option loads code withrequire()
(only works for CommonJS)--require-module
option--import
option loads code withawait import()
(works for both ESM and CommonJS)--loader
option (there's no mechanism to register loaders in-process)require()
require()
, with a fallback to retry withawait import()
require('@cucumber/cucumber/lib/models/test_case_hook_definition')
) still work in a limited wayPhase 1: establish ESM as the best and default way
(Can we done gradually - doesn't have to be in a single release.)
--require
and--import
options still work as beforeawait import()
await import()
--require
and--require-module
are deprecated, users are encouraged to switch to useimport()
and report any difficultiesPhase 2: all-in on ESM
--require
and--require-module
are gone, code is only loaded withawait import()
Stray thoughts
The time between phases 1 and 2 could be short or long; we can stay in a hybrid form for a while and take cues from the wider ecosystem. One milestone will be when ESM loaders become properly stable.
Cucumber could be quicker to do this than other test runners, because the footprint of what's affected by
require
vsimport
is relatively small, unlike some to other tools that have e.g. module-level mocking built aroundrequire
.On the other hand, we are to some extent limited by third-party frameworks that wrap Cucumber - if these move slowly towards ESM, that will give us some headaches.
Beta Was this translation helpful? Give feedback.
All reactions