简体   繁体   English

Angular + Azure DevOps(TFS):替换 environment.ts 文件中的令牌,与环境无关的角度构建

[英]Angular + Azure DevOps(TFS): replace tokens in environment.ts file, environment agnostic angular build

Let's say we have a tokenised environment.ts file like:假设我们有一个标记化的environment.ts文件,例如:

export const environment = {
  production: #{production}#
}

where production type must be boolean .其中production类型必须是boolean

Now, we want to ng build so we get an environment agnostic build, which we can deploy to any environment.现在,我们想要ng build所以我们得到一个环境无关的构建,我们可以将其部署到任何环境。 When deploying to a specific environment we just replace tokens in main.js or main.*.js (that's where the environment.ts file is bundled).当部署到特定环境时,我们只需替换main.jsmain.*.js中的标记(这是environment.ts文件的捆绑位置)。

The problem is, that ng build fails as the above environment.ts has compile tyme errors.问题是, ng build失败,因为上述environment.ts有编译 tyme 错误。

Is there any way how to achieve that?有什么方法可以实现吗? How to produce an environment agnostic angular app build which can be deployed to any environment with any configuration?如何生成可以部署到具有任何配置的任何环境的环境无关的角度应用程序构建?

PS: this task is used for replacing tokens PS:这个任务是用来替换token的

This is what I have come up so far:这是我到目前为止提出的:

export const environment = {
   production: !!'#{production}#',
}

When VSTS variable production is not empty then !!'#{production}#' is resolved to true , otherwise to false .当 VSTS 变量production不为空时, !!'#{production}#'解析为true ,否则解析为false

Because VSTS does ignore variables with empty string ( "" ) value.因为 VSTS 确实会忽略具有空字符串 ( "" ) 值的变量。 It is important to set the production variable for falsy value as:将 falsy 值的production变量设置为:

在此处输入图片说明

according to task settings:根据任务设置:

在此处输入图片说明

Easiest solution to make your file still compile while using replace token task would be:在使用替换令牌任务时使您的文件仍然编译的最简单的解决方案是:

export const environment = {
  production: '#{production}#' === 'true'
}

In your pipeline set variable production to value false when not in production (or any value to avoid the task warning you of a undefined variable) and true when in production.在您的管道中,在不生产时将变量production设置为false (或任何值以避免任务警告您未定义的变量),并在生产中设置为true

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

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