Allow plugins/presets to indicate external dependencies#14065
Allow plugins/presets to indicate external dependencies#14065nicolo-ribaudo merged 13 commits intobabel:mainfrom
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/50985/ |
fefc840 to
2929ca9
Compare
|
Just a heads up: with this design, since I maintain a babel plugin that checks .env files, I don't think this will help since I can't tell it to invalidate constantly. Adding external dependencies is a feature that really helps prevent excessive cache clearing |
|
You can do this: function plugin(api) {
const dotEnvFilename = `${process.cwd()}/.env`;
const env = api.cache.using(() => fs.readFileSync(dotEnvFilename, "utf8"));
api.addExternalDependency(dotEnvFilename);
return {
visitor: {}...
}
}The |
2929ca9 to
3165f26
Compare
| error += | ||
| `Plugins/presets should configure their cache to be invalidated when the external ` + | ||
| `dependencies change, for example using \`api.cache.invalidate(() => ` + | ||
| `+statSync(filepath).mtime)\` or \`api.cache.never()\`\n` + |
There was a problem hiding this comment.
| `+statSync(filepath).mtime)\` or \`api.cache.never()\`\n` + | |
| `statSync(filepath).mtimeMs)\` or \`api.cache.never()\`\n` + |
| return { | ||
| options: opts, | ||
| passes: passes, | ||
| externalDependencies: new ReadonlySet(externalDependencies), |
There was a problem hiding this comment.
I think it suffices to use TS ReadOnlyArray for externalDependencies, since eventually they would be added to a Set.
sosukesuzuki
left a comment
There was a problem hiding this comment.
I commented some nits!
There is something weird going on with stdin/out streams.
9d1a684 to
e92d85e
Compare
This PR is an alternative to #11741, addressing the concerns I explained in my review:
babel.transformcall. The caller is then free to merge/overwrite the external dependencies between different calls as it wish.babel.transform-local,api.addExternalDependencyonly needs to take the dependency path as a parameter.Additionally, I made a few other design choices:
@babel/coredoes not resolve external dependencies: they can be any string you want. This makes it possible, for example, to also store URLs or pointers to a virtual fs (as long as the caller knows how to handle them).api.addExternalDependency, it must also explicitly configure the cache (for example, withcache.invalidateorcache.using). This is because when you have external dependencies you rely on a side communication channel that Babel does not control, so you have to manually invalidate the cache when it changes.@vedantroy thanks for your awesome work in #11741! I made these changes by myself because I noticed you asked us to do so and I changed almost all the code you initially wrote, but your original design (especially regarding
@babel/cli) helped me a ton figuring out what is the best way to integrate this feature in Babel 😄