Skip to content

fix(register): skip hook for ignored files#17928

Merged
nicolo-ribaudo merged 5 commits into
babel:mainfrom
JLHwung:skip-hook-for-ignored-files
Apr 27, 2026
Merged

fix(register): skip hook for ignored files#17928
nicolo-ribaudo merged 5 commits into
babel:mainfrom
JLHwung:skip-hook-for-ignored-files

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented Apr 7, 2026

Q                       A
Fixed Issues? Fixes #17903
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we skip the babel-register hook for ignored files in programmatic options. Previously the hook was still registered for ignored files and therefore an ignored .ts file may throw syntax error because the original JS loader, where the builtin TS type stripping happens, was skipped as pirates has no knowledge that Babel does not handle this ignored file.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: register labels Apr 7, 2026
@@ -109,8 +119,22 @@ async function transform(input: string, filename: string) {
return await store({ code, map });
}

async function isFileIgnored(filename: string) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper can potentially be reused by the babel-cli: See #17878

Do you think we should make it an API of @babel/core?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems small enough that it's fine to just copy&paste it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's leave it as-is.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Apr 7, 2026

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61377

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 7, 2026

Open in StackBlitz

commit: ab65f64

@liuxingbaoyu
Copy link
Copy Markdown
Member

https://babeljs.io/docs/babel-register#specifying-options
@babel/register supports ignore and only options, and supporting them may be sufficient.

@JLHwung
Copy link
Copy Markdown
Contributor Author

JLHwung commented Apr 8, 2026

https://babeljs.io/docs/babel-register#specifying-options @babel/register supports ignore and only options, and supporting them may be sufficient.

Thanks, as is said in the docs, @babel/register does not support ignore and only in config files, I will simplify the implementation.

@JLHwung
Copy link
Copy Markdown
Contributor Author

JLHwung commented Apr 8, 2026

https://babeljs.io/docs/babel-register#specifying-options @babel/register supports ignore and only options, and supporting them may be sufficient.

I looked into this issue a bit, if we want to completely remove the loadPartialConfig call, we will have to somehow duplicate the pattern matching logic of @babel/core to @babel/register, such as packages/babel-core/src/config/pattern-to-regex.ts and the matchesPatterns in packages/babel-core/src/config/config-chain.ts.

If performance is your concern, I think what we can do is to disable configFile and babelrc in the first loadPartialConfig call so the core only checks if the file is deemed ignored via programmatic options. This will reduce the overhead of looking for .babelrc or babel.config.

@liuxingbaoyu
Copy link
Copy Markdown
Member

It appears that we don't support patterns in the documentation, but only regular expressions and functions.

@JLHwung
Copy link
Copy Markdown
Contributor Author

JLHwung commented Apr 21, 2026

It appears that we don't support patterns in the documentation, but only regular expressions and functions.

I believe this is a doc issue. In somewhere we mentioned that it supports regex / glob, elsewhere regex / function. I have updated the register docs to reflect on the current implementation: babel/website#3190

@JLHwung JLHwung force-pushed the skip-hook-for-ignored-files branch from 6167bbd to fdc36b2 Compare April 24, 2026 21:05
browserslistConfigFile: false,
plugins: [],
presets: [],
targets: { browsers: undefined },
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, babel-core still has to resolve the browser targets, even though it does not determine whether an input file is ignored or not. In the future we should optimize it and resolve targets after the file ignoring logic.

Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I had this review in my draft and forgot to send it

@@ -109,8 +119,22 @@ async function transform(input: string, filename: string) {
return await store({ code, map });
}

async function isFileIgnored(filename: string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems small enough that it's fine to just copy&paste it.

Comment thread packages/babel-register/test/index.js Outdated
);
expect(output).toContain(path.join("ignored", "ignored-throw.ts"));
expect(output).toContain(
"SyntaxError: TypeScript enum is not supported in strip-only mode",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be thrown also by the non-ignored file if we accidentally ignore it. Could we instead try/catch the require that we expect to throw, and log something that then we check here?

@JLHwung JLHwung force-pushed the skip-hook-for-ignored-files branch from fdc36b2 to ab65f64 Compare April 27, 2026 14:08
@nicolo-ribaudo nicolo-ribaudo merged commit ed587c3 into babel:main Apr 27, 2026
56 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the skip-hook-for-ignored-files branch April 27, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: register PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@babel/register interfers with Node built-in type stripping for ignored files

4 participants