Skip to content

[Babel 8] Refactor register cache#17229

Merged
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:register-cache
Jan 30, 2026
Merged

[Babel 8] Refactor register cache#17229
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:register-cache

Conversation

@liuxingbaoyu
Copy link
Member

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

This also fixes a hidden bug in Babel 8: process.on("exit", cb) would not be called.

@babel-bot
Copy link
Collaborator

babel-bot commented Apr 8, 2025

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

});

let createdAt = this.db.get("created_at");
// If the cache is older than 30 days, clear it
Copy link
Member

Choose a reason for hiding this comment

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

If I compile something every day for 31st day, on the 31st day will it delate all the cache or the data from the 1t day?

Copy link
Member Author

Choose a reason for hiding this comment

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

It will remove all caches.

"clone-deep": "^4.0.1",
"find-cache-dir": "condition:BABEL_8_BREAKING ? ^3.3.1 : ^2.0.0",
"find-cache-dir": "condition:BABEL_8_BREAKING ? ^5.0.0 : ^2.0.0 (esm:default)",
"lmdb": "condition:BABEL_8_BREAKING ? ^3.2.6 : (esm:open)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make LMDB an optional dependency? The node-lmdb depends on node-gyp-build, which will kick it when the prebuilt binary bindings does not match the user arch. It is likely to happen when we still support a node version that is not supported by the prebuilt.

This opens all sort of install issues because of the tools/libs required and users may not be familiar with them.

See also parcel-bundler/parcel#8152 for an example.

If we make it an optional dependency, users can still use Babel register regardless of node-gyp-errors, though the cache handling is not as efficient as it would be.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that this is an annoying problem.
But I worry that making it an optional dependency will make almost no one use it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I investigated and the pre-built packages released by lmdb support node-api, so the version shouldn't be a problem.
But they do not provide some pre-built packages compared to rollup, such as the lack of Windows x86 and arm.

@liuxingbaoyu
Copy link
Member Author

I changed it to cacache.
Since it uses a lot of small files, the performance may not be comparable to lmdb.
I added a batch cache logic, hoping the performance will be better.
It would be nice if lmdb could support more platforms, and jest would also benefit.

@JLHwung
Copy link
Contributor

JLHwung commented Apr 10, 2025

I changed it to cacache.

Since it uses a lot of small files, the performance may not be comparable to lmdb.

I added a batch cache logic, hoping the performance will be better.

It would be nice if lmdb could support more platforms, and jest would also benefit.

Thought: if the batch cacache is performant, we can use it for the Babel-loader as well.

@liuxingbaoyu
Copy link
Member Author

@JLHwung
What do you think about importing webpack internal files directly or do we explicitly depend on a specific version of webpack to use its cache.
https://github.com/webpack/webpack/blob/eda2e284ac7020fb8e8aa5a6c1fd1547a0ad1b08/lib/cache/PackFileCacheStrategy.js
This is incredibly awesome and seems easy to use.

@JLHwung
Copy link
Contributor

JLHwung commented Apr 12, 2025

@liuxingbaoyu The webpack cache PR was held back because of #16915 (comment), but we don't have an opportunity to debug why the webpack cache is slower than the current babel-loader file cache.

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release pkg: register labels Apr 14, 2025
@liuxingbaoyu liuxingbaoyu force-pushed the register-cache branch 2 times, most recently from 7838d13 to fa98620 Compare January 27, 2026 11:55
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 27, 2026

Open in StackBlitz

commit: 4501a33


exports.init = import("@babel/core").then(initialize);
exports.init = (async function () {
await import("@babel/core").then(initialize).catch(err => {
Copy link
Member

Choose a reason for hiding this comment

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

Can we require(esm) now?

Copy link
Member Author

Choose a reason for hiding this comment

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

I forgot we're using light-jest-runner, let me try!

Copy link
Member Author

Choose a reason for hiding this comment

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

I suddenly realized that we can use mjs directly in the worker, which greatly simplifies the code.

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
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.

This looks good to me, but are we still blocked on the babel-loader bug?

@liuxingbaoyu
Copy link
Member Author

Yes, I am investigating.

@nicolo-ribaudo nicolo-ribaudo added this to the v7.29.0 milestone Jan 29, 2026
@nicolo-ribaudo nicolo-ribaudo merged commit f7ca0dd into babel:main Jan 30, 2026
54 of 55 checks passed
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Mar 7, 2026
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 PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

babel-register doesn't save modules to cache properly babel-register cache grows infinitely and breaks v8

4 participants