Created
February 17, 2025 08:37
-
-
Save genki/b3ecdc7f038124056deebd22c6e54b64 to your computer and use it in GitHub Desktop.
A vite-plugin for bringing mtime/atime of files in ./public to ./dist
This file contains 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
import type {Plugin} from 'vite'; | |
import path from 'path'; | |
import fs from 'fs'; | |
const publicDir = path.resolve(__dirname, '../../public'); | |
const distDir = path.resolve(__dirname, '../../dist'); | |
const copyMtime = (srcDir:string, dstDir:string):void => { | |
console.log('keep-mtime: copying stat', srcDir, '->', dstDir); | |
for (const file of fs.readdirSync(srcDir)) { | |
const src = path.resolve(srcDir, file); | |
const dst = path.resolve(dstDir, file); | |
if (fs.statSync(src).isDirectory()) { | |
copyMtime(src, dst); | |
continue; | |
} | |
// copy mtime and atime | |
const stat = fs.statSync(src); | |
if (fs.existsSync(dst)) { | |
fs.utimesSync(dst, stat.atime, stat.mtime); | |
} else { | |
console.warn("Expected file not found:", dst); | |
} | |
} | |
} | |
export function keepMtime(): Plugin { | |
return { | |
name: 'keep-mtime', | |
enforce: 'post', | |
applyToEnvironment: env => { | |
return env.name === "client"; | |
}, | |
generateBundle: { | |
order: "post", | |
handler() { | |
copyMtime(publicDir, distDir); | |
} | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment