【问题标题】:how to ignore files within subdirectories using chokidar如何使用 chokidar 忽略子目录中的文件
【发布时间】:2019-07-07 19:35:03
【问题描述】:

我有这个目录结构

├── components
│   ├── quarks
│   │   └── index.js
│   │   └── ...
│   ├── bosons
│   │   └── index.js
│   │   └── GridLayout.vue
│   │   └── ...
│   ├── atoms
│   │   └── ButtonStyle.vue
│   │   └── InputStyle.vue
│   │   └── index.js
│   │   └── ...
│   ├── .......
└─────

我想忽略每个文件夹中的index.js,但我没有得到它,我已经尝试了多种方式

const path = require('path')
const chokidar = require('chokidar')
const ROOT_PATH = path.resolve('components')

const watcher = chokidar.watch(ROOT_PATH, {
  ignored: ROOT_PATH + '/*/index.js', //does not work
  ignoreInitial: true
})

已经尝试过: './components/**/index.js', './components/*/index.js', 'components/*/index.js', 'components/**/index.js', 'ROOT_PATH + '/**/index.js'

有人知道如何让它工作吗?

【问题讨论】:

  • @Emma,这两个选项都不起作用,但感谢您的尝试

标签: node.js regex glob chokidar


【解决方案1】:

chokidar documentation 指定ignored 参数为anymatch-compatiable,因此可以通过多种方式完成。

这里是正则表达式解决方案...

任何index.js 文件,即使在根文件夹中:

{
    ignored: /(^|[\/\\])index\.js$/,
    // ...
}

子文件夹中只有index.js 文件:

{
    ignored: /[\/\\]index\.js$/,
    // ...
}

还请注意,在您的示例中,您使用 signoreInitial 这不是一个选项,也许您的意思是 ignoreInitial


也可以使用回调:

{
    ignored: (path) => { return path.endsWith('\\index.js') || path.endsWith('/index.js'); },
    // ...
}

【讨论】:

  • 我在我的问题中写了ignoreInitialwrong,但我已经修复了它。你建议的所有选项都不适合我。
  • chokidar 继续观察index.js
  • @YungSilva 听起来您可能遇到了错误,请参阅issue #773。我添加了使用回调函数的替代方法,请参阅更新的答案。
  • 我尝试了你的替代方案,但它仍然对我不起作用,chokidar 继续观察 index.js :(
  • @YungSilva 尝试使用回调方法,但是,添加一些 console.log 语句 - 看看你得到了什么作为 path 值。
【解决方案2】:

Chokidar 似乎有问题,在 MacOS 上忽略文件有缺陷,这就是我的印象。

所以在运行我的操作之前,我会检查文件是否与我想忽略的文件相同。

chokidar
  .watch('components', { ignoreInitial: true })
  .on('all', (event, filename) => {
    filename !== 'index.js'
    // action here
  })

【讨论】:

    【解决方案3】:

    在 Mac 上对我有用的是使用 **:

    ignored: ['**/node_modules'],
    

    因此,如果其他选项由于错误而不起作用,请选择这个:

     ignored: ['**/index.js'],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2011-02-02
      • 2016-04-05
      • 2017-10-18
      • 2020-03-22
      相关资源
      最近更新 更多