I want to add commit hash into my react app. Normally, I added it with webpack but this project they are using vite.config.js. May I know how to inject commit hash into vite and how can I use it in react component.
Thanks.
const commitHash = require('child_process').execSync('git rev-parse --short HEAD').toString();
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD')
.toString();
export default defineConfig({
define: {
__COMMIT_HASH__: JSON.stringify(commitHash),
},
plugins: [react()],
});
Here's an alternative to the dynamic require
import * as child from "child_process";
const commitHash = child.execSync("git rev-parse --short HEAD").toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With