Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including git commit hash and date in webpack build

I'm using react/es6/webpack. I want to show the date of the build and git hash somewhere in my app. What's the best approach?

like image 951
Matt Avatar asked Jul 15 '16 15:07

Matt


2 Answers

You can use webpack's DefinePlugin:

// get git info from command line let commitHash = require('child_process')   .execSync('git rev-parse --short HEAD')   .toString()   .trim();  ... plugins: [     new webpack.DefinePlugin({       __COMMIT_HASH__: JSON.stringify(commitHash)     })   ] ... 

Then you can use it in your app with __COMMIT_HASH__

like image 112
azium Avatar answered Oct 02 '22 14:10

azium


Another way of doing this is ( in ANGULAR + REACT ) :

Just install this package git-revision-webpack-plugin

Simple webpack plugin that generates VERSION and COMMITHASH files during build based on a local git repository.


Sample Code :

Inside your webpack.config.js (or any dev - prod file)

const GitRevisionPlugin = require('git-revision-webpack-plugin'); const gitRevisionPlugin = new GitRevisionPlugin();  plugins: [     new DefinePlugin({       'VERSION': JSON.stringify(gitRevisionPlugin.version()),       'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),       'BRANCH': JSON.stringify(gitRevisionPlugin.branch()),     }),   ] 

In your Component (React):

export class Home extends Component{     ....      render() {         return(             <div>                 {VERSION}                  {COMMITHASH}                 {BRANCH}             </div>         )     } } 

In your Template (Angular):

{{ VERSION }}  {{ COMMITHASH }} {{ BRANCH }} 
like image 25
Vivek Doshi Avatar answered Oct 02 '22 14:10

Vivek Doshi