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 configs to have an 'overrides' array #7091
Conversation
This doesn't have any tests, but I'll try to add some tomorrow. |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6474/ |
@@ -419,7 +546,7 @@ function matchesPatterns( | |||
|
|||
const absolutePatterns = strings.map(pattern => { | |||
// Preserve the "!" prefix so that micromatch can use it for negation. | |||
const negate = pattern[0] === "!"; | |||
const negate = pattern[0] === "!" && !disableNegation; |
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 we should throw an error/report a warning if disableNegate && pattern[0] === "!"
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.
!disableNegation
is not super clear to me but I don't have a better idea.
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.
Good call, happy to throw instead.
@@ -216,6 +243,9 @@ export function validate(type: OptionsType, opts: {}): ValidatedOptions { | |||
if (type === "env" && key === "env") { | |||
throw new Error(`.${key} is not allowed inside another env block`); | |||
} | |||
if (type === "env" && key === "override") { | |||
throw new Error(`.${key} is not allowed inside an env block`); | |||
} |
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.
Should we also disable override
in another override
block?
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 would say yes.
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.
Good catch, definitely an oversight.
Awesome really looking forward to this one! Yeah tests and some docs like https://eslint.org/docs/user-guide/configuring#configuration-based-on-glob-patterns will be useful
Does this include the top level config without the |
It includes any config object, including top-level, and
Currently, Essentially I'm separating them as |
… to specific files.
…the filename is unknown.
54cf3b0
to
a76709c
Compare
I think this should be good now. |
Would be nice to be able to test this locally on our repo before merging (like all the other config changes) |
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.
good with this based on tests (tbh having a hard time looking through the code with all the caching, etc)
Allow every config object to specify a test/include/exclude field, just like you might do for Webpack. Each item allows an item, or array of items that can be a string, RegExp, or function.
Not sure we want to introduce all these different ways of doing it though? array/non-array, string, regex,fn, include/exclude/test
I went with it since it seemed like an established pattern in Webpack that people would be able to not have to think about a ton. The rest was taken from what we already did for ignore/only. Was there a trimmed down set of functionality you'd prefer? |
I guess it's hard to know all usecases, so not sure - like monorepo/not, app/library, webpack/node, etc. Nothing in particular, if we think that kind of parallel is good? Just wondering if it's confusing to have that kind of nested level of config but I guess doesn't mean people are going to write it that way. We probably want to provide some level of docs on basic/advanced configs with webpack soon |
Webpack is one of the few cases where people can have fine-grained control of transforms. I imagine this config will be more likely in use for people using the CLI or gulp or any number of other integrations. I also think it could be nice for Webpack since you can still centralize all your babel configs at least. |
Ok cool, sounds good. not really specific to the pr but: something that I was thinking of as a result of this is being able to get the files/transforms run in a debug mode for ourselves/others to better understand what babel is actually doing |
(options.test === undefined || | ||
configFieldIsApplicable(context, options.test, dirname)) && | ||
(options.include === undefined || | ||
configFieldIsApplicable(context, options.include, dirname)) && |
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.
What is the difference between test
and include
?
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's the same actually just matching webpack
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.
Old webpack docs are gone, but I believe it used to be that test
had to be a regex (e.g. /\.js$/
), while include
/exclude
had to be a string (e.g. srcDir
)? In any case, they have gained the connotations of "file type" and "directories".
throw new Error(`.${key} is not allowed inside an env block`); | ||
} | ||
if (type === "override" && key === "overrides") { | ||
throw new Error(`.${key} is not allowed inside an overrides block`); |
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.
Nit: You can write throw new Error(".overrides is not allowed inside an overrides block");
, since key
is guaranteed to be overrides
because of the line above.
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.
Yeah I just felt like it was more consistent this way.
Is it expected that the overriding options can contain an Validation of I'm not saying this behavior is necessarily wrong but it'd be good to know whether it's intentional. |
Sorry for being late at this, but super job @loganfsmyth! |
Reposting for visibility from my referencing comment above: With this const common = ["@babel/env"];
module.exports = {
overrides: [
{ test: /\.js$/, presets: common },
{ test: /\.jsx$/, presets: [...common, "@babel/react"] },
{ test: /\.ts$/, presets: [...common, "@babel/typescript"] },
{ test: /\.tsx$/, presets: [...common, "@babel/typescript", "@babel/react"] },
]
} Probably something to suggest in whatever typescript docs get added? What's the roadmap on docs anyway? |
This does two related things
Allow every config object to specify a
test/include/exclude
field, just like you might do for Webpack. Each item allows an item, or array of items that can be a string, RegExp, or function.Note, this means you could actually limit a
.babelrc
to only apply to some files inside of a directory, or make anenv
override only apply to certain files in that env, or something, which seemed like it was reasonable to allow and would be an unnecessary restriction to disallow.Allow an
overrides
array of sub-configs that apply on top of the base configs that will apply based on theirtest
/include
/exclude
values.As an example, this allows user to do
or for instance we'll use this in Babel's own codebase to do
to enable lazy modules for everything except
babel-register
, where it breaks things.