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

Support ESM configs and explicit .cjs and .mjs config extensions #746

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

ecraig12345
Copy link
Member

@ecraig12345 ecraig12345 commented Mar 14, 2024

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 with ts-node 10 (which added .cts and .mts support)
  • .mts is allowed, but will ONLY work if with the new just-esm/just-scripts-esm binaries (next section)
    • This restriction also applies to .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 or ts-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 of ts-node. (It unfortunately doesn't appear to be possible to enable tsx or ts-node-esm with an after-the-fact register() call like with traditional ts-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:

  • It has a .mjs or .mts extension
  • The package.json has "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 the module/moduleResolution "Node16" setting to prevent the import() from being transpiled to require().

Updates to ts-node setup

Also made some updates to the ts-node register() logic, for the previous TS loading approach

  • Check the local typescript version and if it's >= 4.7, set ts-node's module/moduleResolution to "Node16" (to prevent dynamic import() from being transpiled to require())
  • Skip ts-node register() if already running in ts-node or tsx (from the new binaries)

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