简体   繁体   English

这是直接以角度更改构建文件的良好做法吗?

[英]Is this good practice to change build files directly in angular?

I have some scenario to modify build files after deployment. 我有一些方案可以在部署后修改构建文件。 Is this good practice to change manually? 这是手动更改的好习惯吗?

As per the discussion in the comments, what you want to do is to have different configurations for different environments, there are lots of ways to accomplish this. 根据评论中的讨论,您要做的是针对不同的环境使用不同的配置,有很多方法可以完成此操作。 A built in angular cli way is to use different build configurations . 一种内置的cli方式是使用不同的构建配置 Build configurations have a file replacements option that replaces a file you specify with different variants of it. 构建配置具有文件替换选项,该选项将您指定的文件替换为其不同的变体。

In the default scaffold project you can see this pattern with the environments/environment.ts file. 在默认的脚手架项目中,您可以在environments/environment.ts文件中看到此模式。 You will see a production variant of it with the name environment.prod.ts To have a different api url for production all you have to do is to add the production api url to that file and the development api url to environment.ts For staging, you will have to create a build configuration with a file replacement called environment.staging.ts 您将看到名称为environment.prod.ts的生产变体。要为生产使用不同的api URL,您要做的就是将生产api url添加到该文件,将开发api url添加到environment.ts 。 ,您将必须创建一个带有文件替换的构建配置,该文件名为environment.staging.ts

The configuration in angular.json would look like this, angular.json的配置如下所示:

"configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": ".../environments/environment.ts",
                  "with": ".../environments/environment.prod.ts"
                }
               ]
              ...
              "staging": {
               "fileReplacements": [
                 {
                   "replace": ".../environments/environment.ts",
                   "with": ".../environments/environment.staging.ts"
                  }
                ],
  }

Make sure you always import the api base url from environment.ts and not any of the variants. 确保始终从environment.ts而不是任何变体导入api基本url。 ng build --configuration staging will do the replacement for you. ng build --configuration staging将替您完成替换。

Not really, I would make build again and then deploy it. 并非如此,我会再次进行构建,然后进行部署。 In your case if you do not have time to do all of this and you are sure your changes won't break it then go ahead. 对于您来说,如果您没有时间做所有这些事情,并且您确定所做的更改不会破坏它,那么请继续。

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

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