Created
November 12, 2020 22:14
-
-
Save jacksteamdev/bfe0485fedfd641623af551f37c50213 to your computer and use it in GitHub Desktop.
Webpack Changed Files Logger Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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