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)

Commits related to this issue

Most upvoted comments

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 objective of --copy-files is 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.

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-ignored doesn’t copy ignored js files only and still copy all others ignored files

I 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.js

I use the del-cli package and delete all test.js and stories.js files

Furthermore 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 js and json files. Test files are collocated by *.test.js convention.
Requirement: repo should have *.js and *.json files, but shouldnt have *.test.js.
Problem:

  • with --copy-files flag *.test.js are transpiled and copied even though babel config has ignore: [ '**/*.test.js' ].
  • without --copy-files flag *.json files are left out and package breaks.
☯ npm run build -s
🎉  Successfully compiled 1 file with Babel.
🎉  Successfully compiled 1 file with Babel.
dist
├── copy
│   ├── index.js
│   ├── index.test.js
│   └── l10n.json
└── notcopy
    └── index.js
dist-expected
├── index.js
└── l10n.json

here is a repo with reduced demo https://github.com/iamstarkov/babel-ignore-bug-demo/blob/master/README.md

The objective of --copy-files is 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-files because 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-files flag and have it do what seems like the intuitive thing and respect my ignore config… =(

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/cli basically exists to address the simplest of usecases. Once things get complicated, it might be worth exploring alternatives like gulp-babel.

In this case, perhaps it would make the most sense to ignore .json, use --copy-files and then delete the test files afterward?

as #10887 you could use --copy-files --no-copy-ignored

Just remove --copy-files and use fs-extra method of copy. ex:

const fs = require('fs-extra');
const filterFunc = (src, dest) => {
 // ignore file check
  if (check-xxx) {
    return false;
  }
  return true;
};
fs.copy(src, dist, { filter: filterFunc }, err => {
  if (err) return console.error (err);
  console.log ('Copy success!');
});

I stopped using --copy-files for the very reason that it copies my test files to the dist folder. Ended up going with a simple rsync in my build script instead.

babel src -d dist --source-maps --ignore "**/*.test.js" --ignore "**/__mocks__"
rsync -avz --exclude "*.js" --exclude "__tests__" --exclude "__snapshots__" --exclude "__mocks__" --exclude "node_modules" src/ dist/

Can confirm that --ignore has no effect on --copy-files in babel cli 7.2.x.

It’s really nice having --copy-files in 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-files respected the --ignore flag 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 ignore option as follows in babel.config.js:

module.exports = api => {
  // ...
  return {
    // ...
    ignore: ["src/**/*.spec.ts", "src/**/*.spec.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 --ignore option for CLI.

@thedavidprice tried all kind of bundlers/ways to make my monorepo work but ended up totally dropping Babel 🤷‍♂️ i now use

node --harmony --experimental-specifier-resolution=node

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.

Just tried Babel CLI with 7.0.0-beta.35 and I used --ignore after --copy-files and the files were not copied.

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 --ignore flag is passed to the command, versus inside the .babelrc.