babel-plugin-preval icon indicating copy to clipboard operation
babel-plugin-preval copied to clipboard

ReferenceError: Can't find variable: preval (React Native app)

Open Twinbird24 opened this issue 2 years ago • 2 comments

  • babel-plugin-preval version: 5.1.0
  • node version: 16.12.0
  • npm (or yarn) version: 8.4.1

Relevant code or config

const gitHash = preval`
  import GitInfo from 'react-git-info/macro';
  gitInfo = GitInfo();
  module.exports = gitInfo.commit.shortHash
`

My babel.config.js file:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: ['babel-plugin-preval']
}

What you did: I'm trying to use preval to get the git hash and have it displayed in the app.

What happened:

ReferenceError: Can't find variable: preval

I'm not sure if there's any additional steps I need to take to have this work in React Native.

Twinbird24 avatar Mar 07 '22 15:03 Twinbird24

Did you find a fix?

nvmnghia avatar Jul 07 '22 03:07 nvmnghia

No, I used a different method to get the git hash which worked for me. I created a gitHash key in package.json which I update via an npm script: "update-git-hash": "npx json -I -f package.json -e 'this.gitHash=\"$(git rev-parse --short HEAD)\"'".

This way I can run that script in my Github action before building and distributing my app for QA, and have it display the correct info in my app—I reference it through import { gitHash } from '../package.json'. The default value I have for this gitHash key is "dev" because the specific hash isn't important during development, I only needed it for QA distributions.

Twinbird24 avatar Jul 07 '22 13:07 Twinbird24