Search code examples
node.jsreactjsgitcreate-react-app

Add git information to create-react-app


In development, I want to be able to see the build information (git commit hash, author, last commit message, etc) from the web. I have tried:

  • use child_process to execute a git command line, and read the result (Does not work because browser environment)
  • generate a buildInfo.txt file during npm build and read from the file (Does not work because fs is also unavailable in browser environment)
  • use external libraries such as "git-rev"

The only thing left to do seems to be doing npm run eject and applying https://www.npmjs.com/package/git-revision-webpack-plugin , but I really don't want to eject out of create-react-app. Anyone got any ideas?


Solution

  • So, turns out there is no way to achieve this without ejecting, so the workaround I used is:

    1) in package.json, define a script "git-info": "git log -1 --oneline > src/static/gitInfo.txt"

    2) add npm run git-info for both start and build

    3) In the config js file (or whenever you need the git info), i have

    const data = require('static/gitInfo.txt')
    fetch(data).then(result => {
        return result.text()
    })