简体   繁体   English

如何使用create-react-app创建新环境?

[英]How to create new environments with create-react-app?

I am using react-scripts v2 (beta) and I have read the documentation here . 我正在使用react-scripts v2 (测试版),并且已经在此处阅读了文档。

We need to create many environments. 我们需要创建许多环境。

  • We want to store env file under folder config/env . 我们要将env文件存储在config/env文件夹下。
  • We might use javascript file in config/env/staging.js because .env seems to be only for root directory. 我们可能在config/env/staging.js .env使用javascript文件,因为.env似乎仅用于根目录。
  • We have real environment : 我们有真实的环境:
    • default 默认
    • development 发展
    • staging 分期
    • preproduction 试生产
    • production 生产
  • We expect the default environment to be a default config under version control in config/env/default.js , it must be the default configuration used when doing npm start 我们希望default环境是config/env/default.js版本控制下的默认配置,它必须是在执行npm start时使用的默认配置。
  • We expect the user to be able to override with a file with no version control. 我们希望用户能够使用没有版本控制的文件覆盖。 (something like config/env/default.local.js (类似于config/env/default.local.js

Basically that can be reduced to two issues : 基本上可以简化为两个问题:

  1. Is it possible to relocate env location folder? 是否可以重定位环境位置文件夹?
  2. How can we create and select a new environment on npm start/build ? 我们如何在npm start/build上创建和选择新环境?

without ejecting. 没有弹出。

Just copy the environment file to .env before starting / building. 只需在开始/构建之前将环境文件复制到.env You can even put .env in .gitignore that way 您甚至可以将.env放在.gitignore

"start": "cp $ENV .env && react-scripts start"

Then run it: 然后运行它:

ENV=config/staging/.env npm start

There are lots of ways of doing what you want without ejecting, since it's all preprocessing (before your app starts / builds). 有很多方法可以完成您想做的事情而无需弹出,因为它们都是预处理(在您的应用启动/构建之前)。

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

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