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

Fix loading of built-in plugins when using an ESM or TypeScript config with the Standalone CLI #12506

Merged
merged 2 commits into from
Dec 1, 2023

Conversation

thecrypticace
Copy link
Contributor

In the case of a config that's not directly loadable by NodeJS — for example because it is TypeScript, ESM, or uses unsupported syntax, we invoke JITI to load the config file.

Normally there are no problems caused by this. However, in the standalone CLI we provide our plugins already built-in. This becomes a problem because JITI uses a version of require that we were not patching to load the built in plugins.

Making this work is a bit more complicated than it seems at first glance and relies on an internal implementation detail of JITI. However, it's probably one that's not subject to change too much (… famous last words — I know)

Basically we now:

  • Patch require and require.resolve that's available natively to every module loaded by Node
  • Patch createRequire to return patched require + resolve functions that work the same way
  • Overload the JITI instance used so we can provide additional options that effectively instruct it to always use the "native" require to load the module rather than trying to find it on the filesystem

Fixes #12374

@thecrypticace
Copy link
Contributor Author

@RobinMalfait any objections to my useCustomJiti thingy?

I can't override loadConfig directly I'm pretty sure because it's compiled as a non-configurable property of the module's exports.

@thecrypticace thecrypticace changed the base branch from 3.3 to master December 1, 2023 16:36
@thecrypticace thecrypticace merged commit 6dfcdd1 into master Dec 1, 2023
10 checks passed
@thecrypticace thecrypticace deleted the fix/standalone-cli-jiti-modules branch December 1, 2023 16:44
thecrypticace added a commit that referenced this pull request Dec 1, 2023
@privatenumber
Copy link

privatenumber commented May 1, 2024

If you're interested, tsx just released a tsx.require() and register/unregister functions if you want to load TypeScript/ESM files: https://tsx.is/node#only-commonjs-enhancement

Since tsx adoption is quite high, leveraging it if it's already available could be an option.

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.

Standalone CLI build fails with first-party plugin in config
2 participants