"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Migrating from chokidar to

Migrating from chokidar to

Published on 2024-11-01
Browse:372

A big win recently in the e18e space is that chokidar published a new 4.x version!

This new version drops a whole bunch of dependencies and simplifies the internal logic greatly, leaning more on modern platform capabilities.

For those of you who don't know what chokidar is - it is a widely used file system watcher which provides an abstraction over the top of Node's own watch functions. More than likely, it is somewhere in your dependency tree already!

Dependency graph difference

In chokidar 3.x:

Migrating from chokidar  to

In chokidar 4.x:

Migrating from chokidar  to

Changes in 4.x

Most of the changes are internal and shouldn't affect how you use the library, but will improve performance a huge amount.

A few notable changes:

  • fsevents was dropped since Node itself handles cross-platform compatibility well these days
  • ES module support
  • Improved TypeScript types
  • Various performance improvements
  • Glob support removed

The one major change from this list that will affect users is the removal of globs.

Life without globs

In chokidar 3.x, it was possible to watch a glob. For example, we could watch src/*.ts and chokidar would expand the pattern internally to watch all TypeScript files inside src/.

In 4.x, this functionality has been removed since you can achieve the same with filters or an external glob library.

An example:

// chokidar v3
watch('src/*.ts');

// chokidar v4 (RegExp)
watch('src', {
  // any path whose end is not preceded by `.ts`
  ignored: /(?
    stats?.isFile() &&
    !path.endsWith('.ts')
});

// chokidar v4 (glob)
// NOTE: this will not watch newly added files. It
// will only watch the initial set of files
import {glob} from 'tinyglobby';
watch(await glob(['src/*.ts']));

In most cases, you can probably avoid the need for a glob library and use a filter function or RegExp instead (which will also be much faster in many cases).

Feedback

If you upgrade and have any feedback or find any bugs, we'd love to hear from you via issues.

You can also catch many of us in the e18e discord working hard on migrating popular packages from 3.x to 4.x.

Release Statement This article is reproduced at: https://dev.to/43081j/migrating-from-chokidar-3x-to-4x-5ab5?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3