Including git commit hash and date in webpack build

前端 未结 3 891
半阙折子戏
半阙折子戏 2021-02-01 01:04

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?

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 01:38

    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(
                
    {VERSION} {COMMITHASH} {BRANCH}
    ) } }

    In your Template (Angular):

    {{ VERSION }} 
    {{ COMMITHASH }}
    {{ BRANCH }}
    

提交回复
热议问题