#1466·chokidar

Windows: 监视文件的更改事件在许多兄弟文件被写入时发生错误

作者: okorion创建于 2026年6月18日更新于 2026年6月25日

import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import process from 'node:process'; import { createRequire } from 'node:module'; import chokidar from 'chokidar';

const require = createRequire(import.meta.url); const chokidarEntry = require.resolve('chokidar'); const chokidarPkgPath = path.join(path.dirname(chokidarEntry), 'package.json'); const chokidarPkg = JSON.parse(fs.readFileSync(chokidarPkgPath, 'utf8'));

const args = new Map( process.argv.slice(2).map((arg) => { const [key, value = 'true'] = arg.replace(/^--/, '').split('='); return [key, value]; }), );

const fileCount = Number(args.get('files') ?? 300); const waitMs = Number(args.get('wait') ?? 2500); const workspace = path.resolve(args.get('workspace') ?? './tmp-watched'); const watchedDir = path.join(workspace, 'watched'); const rewriteOnChange = args.get('rewrite-on-change') !== 'false';

fs.rmSync(workspace, { recursive: true, force: true }); fs.mkdirSync(watchedDir, { recursive: true });

const files = []; const previousStats = new Map();

for (let i = 0; i < fileCount; i += 1) { const file = path.join(watchedDir, file${i}.scss); fs.writeFileSync(file, .class-${i} { color: red; }\n); const stat = fs.statSync(file); files.push(file); previousStats.set(file, { mtimeMs: stat.mtimeMs, ctimeMs: stat.ctimeMs, atimeMs: stat.atimeMs, size: stat.size, }); }

let rawScssEvents = 0; let rawGeneratedEvents = 0; let highLevelAddEvents = 0; let highLevelChangeEvents = 0; let generatedWritesFromChange = 0; const samples = [];

function eventPathKind(value) { const normalized = String(value ?? '').replaceAll('\', '/'); if (normalized.endsWith('.scss.d.ts')) return 'generated'; if (normalized.endsWith('.scss')) return 'scss'; return 'other'; }

内容来源: paulmillr/chokidar