Support ESM configs and explicit .cjs and .mjs config extensions #746
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Update the config loading approach and introduce new binaries to support ESM configs. This addresses part of #572 and #686 as much as is possible without a potential breaking change.
Allow more extensions for config files
.cjs
and.mjs
configs will "just work".cts
configs will work withts-node
10 (which added.cts
and.mts
support).mts
is allowed, but will ONLY work if with the newjust-esm
/just-scripts-esm
binaries (next section).ts
configs in a module context (type: "module"
in package.json)Add new binaries to support TS ESM config files (+
tsx
support)Add two new binaries which spawn a new process to run the Just CLI via
tsx
orts-node-esm
(depending on what's installed in the repo):just-scripts-esm
(just-scripts
package)just-esm
(just-task
package)You must use one of these binaries if you want to use a
.ts
or.mts
config file in a module context (type: "module"
in package.json).They should also work in a CJS context, like if you just want to use
tsx
instead ofts-node
. (It unfortunately doesn't appear to be possible to enabletsx
orts-node-esm
with an after-the-factregister()
call like with traditionalts-node
.)Load ESM configs with
import()
Update the config loading logic to use
import()
for ESM configs. A config is considered ESM if one of the following is true:.mjs
or.mts
extension"type": "module"
and the config has a.js
or.ts
extension (but see notes below)This required updating the repo version of
typescript
to 4.7 so that I could use themodule
/moduleResolution
"Node16"
setting to prevent theimport()
from being transpiled torequire()
.Updates to
ts-node
setupAlso made some updates to the
ts-node
register()
logic, for the previous TS loading approachtypescript
version and if it's >= 4.7, setts-node
'smodule
/moduleResolution
to"Node16"
(to prevent dynamicimport()
from being transpiled torequire()
)ts-node
register()
if already running ints-node
ortsx
(from the new binaries)