Skip to content

Instantly share code, notes, and snippets.

@jacksteamdev
Created November 12, 2020 22:14
Show Gist options
  • Save jacksteamdev/bfe0485fedfd641623af551f37c50213 to your computer and use it in GitHub Desktop.
Save jacksteamdev/bfe0485fedfd641623af551f37c50213 to your computer and use it in GitHub Desktop.
Webpack Changed Files Logger Plugin
const fs = require("fs");
const filepath = "craco-changed-files.json";
/**
* This overwrites a log file of file changes that caused a rebuild.
*/
class ChangedFilesLogPlugin {
constructor() {
fs.writeFileSync(filepath, JSON.stringify([], null, 2), "utf8");
}
apply(compiler) {
compiler.hooks.watchRun.tap("WatchRun", (compiler) => {
if (compiler?.watchFileSystem?.watcher?.mtimes) {
const changedTimes = compiler.watchFileSystem.watcher.mtimes;
const changedFiles = Object.keys(changedTimes);
if (changedFiles.length) {
const logData = JSON.parse(fs.readFileSync(filepath, "utf8"));
logData.push(changedFiles);
fs.writeFileSync(filepath, JSON.stringify(logData, null, 2), "utf8");
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment