-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Allow plugins/presets to indicate external dependencies #14065
Allow plugins/presets to indicate external dependencies #14065
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`+statSync(filepath).mtime)\` or \`api.cache.never()\`\n` + | |
`statSync(filepath).mtimeMs)\` or \`api.cache.never()\`\n` + |
@@ -206,6 +221,7 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>( | |||
return { | |||
options: opts, | |||
passes: passes, | |||
externalDependencies: new ReadonlySet(externalDependencies), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it suffices to use TS ReadOnlyArray
for externalDependencies
, since eventually they would be added to a Set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.transform
call. The caller is then free to merge/overwrite the external dependencies between different calls as it wish.babel.transform
-local,api.addExternalDependency
only needs to take the dependency path as a parameter.Additionally, I made a few other design choices:
@babel/core
does 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.invalidate
orcache.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 😄