【问题标题】:Angular-CLI - how to replace some string in files before distribution, based on environmentAngular-CLI - 如何在分发之前根据环境替换文件中的某些字符串
【发布时间】:2018-03-17 16:56:53
【问题描述】:

最初,我使用 Angular VS 2017 模板和 Angular 2.4.0 和 webpack

由于升级到 Angular 4 的一些问题,我决定切换到 Angular-CLI(angular 4) 和 net.core 2.0 应用程序。我的应用程序已设置并正在运行。

我正在使用 TFS 进行构建,并且根据环境,我的 Angular 应用程序中有几个不同的设置文件:

  • app.settings.debug.ts
  • app.settings.release.ts
  • app.settings.ts

我是根据环境导入这些文件来填写Auth Configuration:

import { Injectable } from '@angular/core';
import { AppSettings } from '../../app.settings';

@Injectable()
export class AuthConfiguration {

    // The Issuer Identifier for the OpenID Provider (which is typically obtained during Discovery) MUST exactly match the value of the iss (issuer) Claim.
    public iss = AppSettings.API_AUTH_URL;

    public server = AppSettings.API_AUTH_URL;

    public redirect_url = AppSettings.WEBAPP_URL;

    // This is required to get the signing keys so that the signiture of the Jwt can be validated.
    public jwks_url = AppSettings.API_AUTH_URL + '/.well-known/openid-configuration/jwks';

    // The Client MUST validate that the aud (audience) Claim contains its client_id value registered at the Issuer identified by the iss (issuer) Claim as an audience.
    // The ID Token MUST be rejected if the ID Token does not list the Client as a valid audience, or if it contains additional audiences not trusted by the Client.
    public client_id = 'angular2client';

    public response_type = 'id_token token';

    public scope = 'api openid profile';

    public post_logout_redirect_uri = AppSettings.WEBAPP_URL;
}

在旧项目中,我根据环境使用 webpack 替换了这部分:

import { AppSettings } from '../../app.settings';

在 webpack.config.js 中像这样:

 { test: /\.ts$/, include: /ClientApp/, use: [{ loader: 'string-replace-loader', query: { search: '/app.settings', replace: isLocalhost ? '/app.settings' : isDebug ? '/app.settings.debug' : '/app.settings.release' }, }, 'awesome-typescript-loader?silent=true', 'angular2-template-loader',] },

如何使用 angular-CLI 和 packages.json 实现这一点?

我希望实现与现在相同的行为,其中 TFS 构建我有一个 NPM 任务,使用 package.json 中的命令,例如,npm build debugnpm build release 分别用于每个环境。

【问题讨论】:

    标签: angular tfs asp.net-core angular-cli


    【解决方案1】:

    可以在(src/environments)中使用angular cli的环境文件

    // src/environments/environment.ts
    export const environment = {
        API_AUTH_URL: 'auth_url'
    }
    

    然后在你的打字稿中,导入变量并根据需要使用

    // service.ts
    import { environment } from './environments/environment';
    
    @Injectable()
    export class AuthConfiguration {
        public iss = environment.API_AUTH_URL;
    }
    

    要使用环境文件,您可以制作一个 npm 脚本

    "build-dev": "ng build --environment=dev" 
    

    注意:如果不指定环境,cli将使用{"apps": [{"environmentSource": "environments/environment.ts"}]}中的angular-cli.json中定义的默认环境

    cli 还允许您添加其他环境文件。例如,您可以添加 src/environments/environment.release.ts,然后在 angular-cli.json 中添加

    {
        "apps": [{
            ...,
            "environments": {
                ...,
                "release": "environments/environment.release.ts"
            }
        }]
    }
    

    现在您可以制作一个额外的 npm 脚本来使用您的自定义环境文件

    "build-release": "ng build --environment=release"
    

    这是一篇很好的文章,涵盖了上述内容。 http://tattoocoder.com/angular-cli-using-the-environment-option/

    【讨论】:

    • 如果我想加载发布文件,在这种情况下是 environment.release.ts 是否必须对 service.ts 文件进行任何更改?还是 IT 保持不变的进口?在这种情况下,不需要字符串替换
    • @AmelSalibasic 相同的导入。 cli/webpack 将处理要使用的内容,因此导入始终为 /environments/environment
    • 这东西完美运行,感谢您花时间一步一步回答。我想为其他来到这里的人提一下,对于环境值的实际服务使用ng serve --env=NAME_OF_ENVIRONMENT,对于构建到发行版文件夹使用ng build --env=NAME_OF_ENVIRONMENT
    • 我真的不喜欢每个环境的“编译”包不同的解决方案。它违反了一个关键的 DevOps 原则,即可部署的软件资产应该可以跨环境转移,而无需进行二进制修改。如果此配置是特定于环境的,则选择使用 HTTP 动态提供 JSON 并保持包的环境不可知。
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 2017-08-30
    • 1970-01-01
    • 2020-04-07
    • 2017-08-30
    • 2023-03-04
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多