babel: `--no-copy-ignored` only ignores `.js` files and does not enforce config "ignore:" settings

Bug Report

Current Behavior When using babel cli with --copy-files --no-copy-ignored, files and directories configured in my babel.config.js ARE copied into my build directory (with the exception of specified file types ending in .js). To be clear:

  • there are no included files with names including test.js
  • there are included files with names including test.ts
  • there are included directories named __tests__
  • there are included directories named __mocks__

Input Code

“package.json” build script: babel src -d dist --delete-dir-on-start --copy-files --no-copy-ignored

Expected behavior/code When running yarn build, I expect babel to build my files and output to the “dist/” directory. The build will include non-js files because I am using the flag --copy-files, for example, template files ending in .template. The build will not include directories and files from the babel.config.js “ignore” list because I am using --no-copy-ignored:

  • directory __tests__
  • directory __mocks__
  • files with name including test.js
  • files with name including test.ts

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
  ...
  ignore:
     [
          /\.test\.(js|ts)/,
          '**/__tests__',
          '**/__mocks__',
     ]
}

Environment

  System:
    OS: macOS 10.15.4
  Binaries:
    Node: 12.16.0 - ~/.nvm/versions/node/v12.16.0/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.0/bin/npm
  • Monorepo: Lerna
  • How you are using Babel: cli

Possible Solution I have tried using --ignore, which also does NOT work. I suspect the --no-copy-ignored flag has a bug.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 19
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Is there a utility function in the CLI that checks if a file matches the ignore parameter?

@babel/core uses shouldIgnore to check whether a file should be ignored given ignore config items: https://github.com/babel/babel/blob/65911144e9eee760052e20fed9780bd78922f942/packages/babel-core/src/config/config-chain.js#L466

If a file is ignored, @babel/core will return null, which is used by @babel/cli later to mark it as FILE_TYPE.IGNORED:

https://github.com/babel/babel/blob/65911144e9eee760052e20fed9780bd78922f942/packages/babel-cli/src/babel/dir.js#L61

However I doubt whether there is any to fix here: --no-copy-ignored works only if the file itself is considered compilable by @babel/cli and thus it has been supplied to @babel/core.

@thedavidprice In your cases, you can tell @babel/cli that it should compile .ts files (so the ignore config also applied)

babel src --extensions ".ts" -d dist --delete-dir-on-start --copy-files --no-copy-ignored

@vxcamiloxv .json and .snap are not considered as compilable and thus it will be picked up by --copy-files. You can use extensions.

I agree with @loganfsmyth expressed before in https://github.com/babel/babel/issues/6226#issuecomment-328660064, that @babel/cli is meant to be used in simple projects. Consider migrate to Gulp or Rollup if you are bundling a middle-large scale apps.

--ignore and --only are not working at all,babel-cli keeps copying .git dir

https://github.com/babel/babel/blob/65911144e9eee760052e20fed9780bd78922f942/packages/babel-cli/src/babel/dir.js#L103-L106

This condition is the cause of this issue. If the --copy-files is flagged and the file is not compilable such as .json, the condition skips the check for whether the file is ignored.

Could I work on this?

same issue, directories or for example .json, .snap are not been ignored

I have this problem too. I use babel to build several packages that have node_modules in them. When I use no-copy-ignored, babel tries to copy node_modules (although I have added it to the ignore list).

I use pnpm which install packages via symlinks. However, tries to follow these symlinks and copy them which takes a lot of time.

For me, non-following the symlinks during copying is enough, but it would be nice if this behavior is fixed.

I have added a script to remove node_modules to speed up build.nuclide: https://github.com/atom-ide-community/atom-ide-base/blob/f5cae309f3fff8fe57ef0e897002722ce12ddeb5/package.json#L57

I also had to add png and snap so they did not get copied. That’s not how it should be:

--extensions '.js,.ts,.tsx,.png,.snap'

I really hope @barronwei could have a look into that issue, could you? 🙏

same issue:

"build:es2015": "NODE_ENV=production babel ./src --out-dir ./build --copy-files --no-copy-ignored",

babel.config looks like:

ignore: [
      'node_modules',
      'build',
      'src/index.js',
      '**/__test__',
      '**/__tests__',
      '**/__mocks__',
      '**/__fixtures__',
      '**/__snapshots__',
      '**/devMode',
    ],

output includes __test__ and __tests__ and __snapshots__ dirs, but correctly excludes devMode and src/index.js.