简体   繁体   English

如何在创建反应应用程序中进行应用程序版本控制?

[英]How to do app versioning in create react app?

I need to display version of my react app in the footer in xyz format.我需要在页脚中以xyz格式显示我的 react 应用程序的版本

I need this version to increment every time I deploy the app by being provided a choice if I want to increment x or y or z .如果我想增加xyz ,我需要在每次部署应用程序时增加这个版本。

How do I achieve this?我如何实现这一目标? :) :)

To bump the version of your app you can use npm version .要提高应用程序的版本,您可以使用npm version

For example:例如:

npm version minor

You can use grunt-bump to handle your app versioning.您可以使用grunt-bump来处理您的应用程序版本控制。 As for displaying your app version, refer to Is there a way to get version from package.json in nodejs code?至于显示您的应用程序版本,请参阅有没有办法在 nodejs 代码中从 package.json 获取版本? . .

I don't think the answers from @Bertrand P or @VulfCompressor tell the complete picture.我认为@Bertrand P 或@VulfCompressor 的答案并不能说明全部情况。 I used genversion https://www.npmjs.com/package/genversion .我使用了 genversion https://www.npmjs.com/package/genversion The steps I did were:我做的步骤是:

  1. npm install genversion --save-dev
  2. Modify the build script in package.json to genversion --es6 src/autobuild_version.js && react-scripts build (I couldn't figure out a way to import the generated module from the lib directory as suggested in the genversion documentation so I had to put it in the src directory instead)package.json的构建脚本修改为genversion --es6 src/autobuild_version.js && react-scripts build (我想不出一种方法来从 genversion 文档中建议的lib目录导入生成的模块,所以我有将其放在src目录中)
  3. In the React app, import { version } from './autobuild_version' and use as appropriate在 React 应用程序中, import { version } from './autobuild_version'并根据需要使用
  4. Add src/autobuild_version.js to .gitignore (other source code control tools are available)src/autobuild_version.js添加到.gitignore (其他源代码控制工具可用)
  5. npm run build

Once you have a way to bump the version in package.json (eg npm version , as suggested by @bertrand-p), you can then assign the version to an environment variable.一旦你有办法在 package.json 中修改版本(例如npm version ,正如@bertrand-p 所建议的那样),你就可以将版本分配给一个环境变量。 For example, in .env you can set:例如,在.env您可以设置:

REACT_APP_VERSION=$npm_package_version

Then you can access the variable from within your app via process.env.REACT_APP_VERSION .然后您可以通过process.env.REACT_APP_VERSION从您的应用程序中访问该变量。

See also: https://github.com/facebook/create-react-app/issues/2466#issuecomment-357490359另见: https : //github.com/facebook/create-react-app/issues/2466#issuecomment-357490359

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM