babel: copy-files flag does not respect ignore block in .babelrc
Bug: If in your build script, you have --copy-files passed, files that are ignored in .babelrc files will be copied over, instead of being ignored.
Input Code
babel src --out-dir lib --copy-files
Babel Configuration (.babelrc, package.json, cli command)
{
"presets": [
"es2015",
"stage-1",
"react"
],
"ignore": [
"**__mocks__/*.js"
]
}
Expected Behavior
Babel should not copy files that are explicitly ignored in .babelrc configuration. In this case, babel should not transpile or copy Jest mocks.
Current Behavior
Babel copies files that are ignored are explicitly .babelrc.
> npm run build
> babel src --out-dir lib --copy-files
src/api/__mocks__/index.js -> lib/api/__mocks__/index.js
src/api/endpoints.js -> lib/api/endpoints.js
If you pass an --ignore flag (with the same blob) after the --copy-files flag, it will behave as expected.
> npm run build;
> babel src --out-dir lib --copy-files --ignore **__mocks__/*.js
src/api/endpoints.js -> lib/api/endpoints.js
(output does not include ignored directories)
Context
This issue comes up when you work with Jest manual mocks inside of your source directory. Jest warns that you have duplicate mocks directories.
| software | version(s) |
|---|---|
| Babel | 6.26.0 |
| node | 6.9.2 |
| npm | 5.4.0 |
| Operating System | OSX El Capitan |
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 48
- Comments: 31 (3 by maintainers)
Forgive me if I’m coming out of left field, but…
Isn’t the most obvious/common use case for --ignore to skip over test files? And isn’t the most obvious/common use case for --copy-files to copy non-code resources (css, etc.) along with transpiled code?
So, if you try to use those two most obvious use cases together, wouldn’t the most obvious thing be to not copy those untranspiled test files? Would there ever be a case in which someone would want untranspiled test files to be copied?
More generally, what would be the use case that this new behavior supports that couldn’t be achieved with the old one (simply by not listing in --ignore the files that user does still want to be copied)?
@loganfsmyth is there any chance you might reconsider?
Still having the same issue on babel-cli 7.4.4. Reading all the comments i agree that no one likes the current behavior and want to ignore completely the files listed on
--ignore.Any updates on this?
The thing is, babel-cli 6 does not work like that. So is either a bug, or somewhere along the line it was decided to change the behavior.
I just tested with babel-cli 6.18.0, there --copy-files respects what you had as --ignore
@sayjeyhi actually #10887 is weird
--no-copy-ignoreddoesn’t copy ignored js files only and still copy all others ignored filesI ended up by using another command to delete all test files after babel --copy-files
rimraf ./react/**/* && babel src/react -d dist --copy-files && del dist/**/*.test.js dist/**/*.stories.jsI use the
del-clipackage and delete all test.js and stories.js filesFurthermore i gitignored test.js and stories.js files in the dist dir
I encountered the same problem and has a legit use case for this.
Reality: repository consists of
jsandjsonfiles. Test files are collocated by*.test.jsconvention.Requirement: repo should have
*.jsand*.jsonfiles, but shouldnt have*.test.js.Problem:
--copy-filesflag*.test.jsare transpiled and copied even though babel config hasignore: [ '**/*.test.js' ].--copy-filesflag*.jsonfiles are left out and package breaks.here is a repo with reduced demo https://github.com/iamstarkov/babel-ignore-bug-demo/blob/master/README.md
The objective of
--copy-filesis to say “please copy the files that Babel doesn’t process”, so this is working as expected. If you only want it to copy the files Babel processes, just leave off--copy-files. If you need a bit of both, seems like you’d want to piece that together with Gulp.Yup, that seems like a bug.
It seems like the intuitive, expected behavior for most users is that
--ignore(or an ignore config in babelrc) be respected by--copy-filesbecause it’s a great feature and the simplest way to solve a very common problem. Further, it sounds like the behavior changed to this less desirable behavior between v6 and v7 and if that’s true, it sounds like this must be a regression.There are workarounds but they all require additional babel plugins, npm scripts, and/or dependencies to manage something that, it sounds like, used to just work…
Currently, I also have a need to simply copy over CSS files at build time without also copying the test files… It’s super unfortunate that I can’t just add the
--copy-filesflag and have it do what seems like the intuitive thing and respect myignoreconfig… =(Please consider making this work like people expect it to work? I could even imagine a separate flag that does respect the ignore, so you can get either behavior. But this does feel like something that shouldn’t require an additional script or dependency to do and Gulp feels like an overly complicated solution to simple need… Thanks!
Any updates on this?
@babel/clibasically exists to address the simplest of usecases. Once things get complicated, it might be worth exploring alternatives likegulp-babel.In this case, perhaps it would make the most sense to ignore
.json, use--copy-filesand then delete the test files afterward?as #10887 you could use
--copy-files --no-copy-ignoredJust remove
--copy-filesand usefs-extramethod ofcopy. ex:I stopped using
--copy-filesfor the very reason that it copies my test files to thedistfolder. Ended up going with a simplersyncin my build script instead.Can confirm that
--ignorehas no effect on--copy-filesin babel cli 7.2.x.It’s really nice having
--copy-filesin the same command that compilation occurs in, especially when running it in watch mode for use in a monorepo environment.If I go with something like rsync I’ll have to setup a separate watch and copy command. If
--copy-filesrespected the--ignoreflag then none of that is needed.Looks like the change happened here https://github.com/babel/babel/issues/5404
@Sceat I’m experiencing the same, frustrating to diagnose, behavior. Do you know if anyone has opened an issue for this? To my surprise, I couldn’t find one. Assuming I’m just not looking hard enough.
Just in case anybody else has a similar issue, I was running into this in a TypeScript project. Had to set my
ignoreoption as follows inbabel.config.js:Was hoping to blow them all away with
**/*.spec.*but that didn’t work, nor did**/*.spec.(ts|js)or either of the entries in the example above by themselves, only in that particular combination does it work as I expected. I also had to combine them when using the--ignoreoption for CLI.@thedavidprice tried all kind of bundlers/ways to make my monorepo work but ended up totally dropping Babel 🤷♂️ i now use
to run my projects with still advanced ecma features but without having to move any files around (except through dockerfiles)
If my reading is correct, the current behavior is a regression. I think babel-cli should be able to handle relatively simple combinations of include/exclude/copy as requested here, without resorting to gulp. After several years struggling with gulp, I am trying to remove it, which brings me here. Relying on another cmd such as rsync solves one problem, but does not work well with watch.
I don’t think I’ve ever come across an order-dependency like this in CLI unless it was for parameters to flags. This would be weird.
@loganfsmyth That makes sense, I was curious if that was the expected. However, it’s strange that it works differently when the
--ignoreflag is passed to the command, versus inside the .babelrc.