@lynx-js/rspeedy
Version:
A webpack/rspack-based frontend toolchain for Lynx
38 lines (37 loc) • 1.36 kB
JavaScript
async function watchFiles(files, callback) {
const chokidar = await import("./1~chokidar.js");
const watcher = chokidar.default.watch(files, {
ignoreInitial: true,
ignorePermissionErrors: true
});
const cb = debounce((event, filePath)=>{
const startTime = Date.now();
watcher.close().then(()=>callback(filePath, startTime, event));
}, 300);
watcher.once('add', cb.bind(null, 'add'));
watcher.once('change', cb.bind(null, 'change'));
watcher.once('unlink', cb.bind(null, 'unlink'));
}
function debounce(func, wait) {
let timeoutId = null;
return (...args)=>{
if (null !== timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(()=>{
func(...args);
}, wait);
};
}
function getWatchedFiles(configPath, rspeedyConfig) {
const watchedFiles = [
configPath
];
if (Array.isArray(rspeedyConfig.dev?.watchFiles)) watchedFiles.push(...rspeedyConfig.dev.watchFiles.filter((item)=>'reload-server' === item.type).flatMap((item)=>item.paths));
else if (rspeedyConfig.dev?.watchFiles?.type === 'reload-server') {
const { paths } = rspeedyConfig.dev.watchFiles;
watchedFiles.push(...Array.isArray(paths) ? paths : [
paths
]);
}
return watchedFiles;
}
export { getWatchedFiles, watchFiles };