简体   繁体   English

如何使用 create-react-app 配置创建和运行应用程序的开发版本

[英]How to create and run a development build of an application using create-react-app configuration

npm run build creates production build of the project. npm run build创建项目的生产版本。 How do I create development build?如何创建开发版本? I'm using gradle-node-plugin to build the artifact.我正在使用gradle-node-plugin来构建工件。 The project has standard create-react-app configuration.该项目具有标准的 create-react-app配置。

This is not really doable with just create-react-app , there is an open issue Here and it doesn't look like it will be added anytime soon.create-react-app这并不是真正可行的,这里有一个悬而未决的问题,看起来不会很快添加。

However, you can use a package called dotenv for that, following are the steps you should take:但是,您可以为此使用一个名为dotenv的 package,以下是您应该采取的步骤:

  • Install dotenv (make sure to add save dev ) npm install dotenv-cli --save-dev安装dotenv (确保添加save dev ) npm install dotenv-cli --save-dev

  • In package.json scripts section add new script: "build:dev": "dotenv -e.env.development react-scripts build",在 package.json 脚本部分添加新脚本: "build:dev": "dotenv -e.env.development react-scripts build",

  • And you can build for development with npm run build:dev您可以使用npm run build:dev


PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here ) you can add build:prod to package.json and disable the regular build command, see code: PS:如果你想避免错误地将开发构建部署到生产环境(如此所述),你可以将build:prod添加到 package.json 并禁用常规build命令,请参见代码:

"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",

Also note that process.env.NODE_ENV will still be production but it'll load your.env.development file另请注意, process.env.NODE_ENV仍将用于production ,但它会加载 your.env.development 文件

Thanks, @Moses.谢谢,@Moses。 This is an extension to the answer posted by Moses Schwartz.这是对 Moses Schwartz 发布的答案的扩展。 You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.您还可以通过在 bash shell 中导出环境(开发、测试、生产)的值来使构建动态选择环境文件。然后您不必为不同的环境使用不同的构建命令。

You can use this in your package.json您可以在您的 package.json 中使用它

"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",

So when your run npm start, it will pick the environment values from.env and when you run npm run build, it will pick the environment values from.env.{REACT_APP_ENVIRONMENT}因此,当您运行 npm 开始时,它将从 .env 中选择环境值,而当您运行 npm run build 时,它将从 .env 中选择环境值。{REACT_APP_ENVIRONMENT}

To define the REACT_APP_ENVIRONMENT, you can do:要定义 REACT_APP_ENVIRONMENT,您可以执行以下操作:

export REACT_APP_ENVIRONMENT="development" or 
export REACT_APP_ENVIRONMENT="test" or 
export REACT_APP_ENVIRONMENT="production"

Hope this helps.希望这可以帮助。 This will help in staging the react application for multiple environments.这将有助于为多个环境暂存反应应用程序。

Typing npm start in command line begins the development build:) It will run on the localhost in your browser在命令行中键入npm start开始开发构建:) 它将在浏览器的本地主机上运行

npm run build creates production build of the project. npm run build创建项目的生产构建。 How do I create development build?如何创建开发版本? I'm using gradle-node-plugin to build the artifact.我正在使用gradle-node-plugin来构建工件。 The project has standard create-react-app configuration.该项目具有标准的 create-react-app配置。

Thanks to @Tx_monster comment感谢@Tx_monster 评论

github.com/Nargonath/cra-build-watch github.com/Nargonath/cra-build-watch

A script for create-react-app that writes development builds to the disk将开发构建写入磁盘的 create-react-app 脚本

npm install -D cra-build-watch

package.json:
{
  "scripts": {
    "watch": "cra-build-watch"
  }
}

npm run watch

npm run build creates production build of the project. npm run build创建项目的生产构建。 How do I create development build?如何创建开发版本? I'm using gradle-node-plugin to build the artifact.我正在使用gradle-node-plugin来构建工件。 The project has standard create-react-app configuration.该项目具有标准的 create-react-app配置。

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

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