mirror of
https://github.com/citizenfx/cfx-server-data.git
synced 2025-12-12 06:14:09 +01:00
builders: webpack: cache input file state and only rebuild when needing to
This commit is contained in:
@@ -1,5 +1,48 @@
|
||||
const weebpack = require('webpack');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
function getStat(path) {
|
||||
try {
|
||||
const stat = fs.statSync(path);
|
||||
|
||||
return stat ? {
|
||||
mtime: stat.mtimeMs,
|
||||
size: stat.size,
|
||||
inode: stat.ino,
|
||||
} : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class SaveStatePlugin {
|
||||
constructor(inp) {
|
||||
this.cache = [];
|
||||
this.cachePath = inp.cachePath;
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.hooks.afterCompile.tap('SaveStatePlugin', (compilation) => {
|
||||
for (const file of compilation.fileDependencies) {
|
||||
this.cache.push({
|
||||
name: file,
|
||||
stats: getStat(file)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
compiler.hooks.done.tap('SaveStatePlugin', (stats) => {
|
||||
if (stats.hasErrors()) {
|
||||
return;
|
||||
}
|
||||
|
||||
fs.writeFile(this.cachePath, JSON.stringify(this.cache), () => {
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (inp, callback) => {
|
||||
const config = require(inp.configPath);
|
||||
@@ -9,6 +52,12 @@ module.exports = (inp, callback) => {
|
||||
if (config.output && config.output.path) {
|
||||
config.output.path = path.resolve(inp.resourcePath, config.output.path);
|
||||
}
|
||||
|
||||
if (!config.plugins) {
|
||||
config.plugins = [];
|
||||
}
|
||||
|
||||
config.plugins.push(new SaveStatePlugin(inp));
|
||||
|
||||
weebpack(config, (err, stats) => {
|
||||
if (err) {
|
||||
|
||||
Reference in New Issue
Block a user