简体   繁体   English

angular2-starter配置文件(带有环境(测试,生产)变量,也称为.env或.conf)

[英]angular2-starter config file (with environment (testing, production) variables aka .env or .conf)

In many frameworks like for example PHP Laravel there are files with local configuration (different from dev, test, production environments). 在许多框架中,例如PHP Laravel,都有带有本地配置的文件(不同于开发,测试,生产环境)。 How to provide such configuration file which will contain all local environment variables values (like key for Google Analytics, snetry.io etc.) for angular-starter project? 如何为angular-starter项目提供这样的配置文件,其中将包含所有本地环境变量值(例如Google Analytics(分析)的键,snetry.io等)?

I propose following solution which works also with AoT: 我提出以下解决方案也适用于AoT:

In repository with our angular-starter project in main directory (where is package.json, docker and other top folder files) we create file .env.example.js with following body: 在主目录中包含angular-starter项目的存储库中(其中是package.json,docker和其他顶级文件夹文件),我们创建带有以下主体的文件.env.example.js

// copy this file to './.env.js' and modify variables in EnvData

var Env = {
    // If some of below variable is NULL then that varabile feature is off
    google_analaytics : 'XX-YYYYYYYY-Z',
    sentry_clientKey_publicDSN: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@sentry.io/yyyyyy',
    // ... here you can put more variables
};

module.exports = Env;

Then we copy this file to .env.js and include that last one to .gitignore file. 然后,我们将此文件复制到.env.js并将最后一个文件包含到.gitignore文件中。 And then we commit files .env.example.js and .gitignore into our git repository. 然后,我们将文件.env.example.js.gitignore提交到git存储库中。

We use .js file instead of .ts (typescript) because on generating index.html file webpack works on js files. 我们使用.js文件而不是.ts(打字稿),因为生成index.html文件时,webpack可以在js文件上使用。 Ok so let's show how use this variables in webpack config files. 好的,让我们展示如何在webpack配置文件中使用此变量。 Let's go to ./config/webpack.common.js and find below code and add line: 让我们转到./config/webpack.common.js并找到以下代码并添加以下行:

  new HtmlWebpackPlugin({
    template: 'src/index.html',
    title: METADATA.title,
    chunksSortMode: 'dependency',
    metadata: METADATA,
    inject: 'head',
    env: require('../.env')    // <-- new line
  }),

Then in ./src/index.html in google analytics section you can change to: 然后在Google Analytics(分析)部分的./src/index.html中,您可以更改为:

  <% if (htmlWebpackPlugin.options.env.google_analaytics) { %>
  <!-- Google Analytics: -->
  <script>
    (function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=r;A[l]=A[l]||function(){
    (A[l].q=A[l].q||[]).push(arguments)},A[l].l=1*new Date();a=n.createElement(g),
    r=n.getElementsByTagName(g)[0];a.async=1;a.src=u;r.parentNode.insertBefore(a,r)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', '<%= htmlWebpackPlugin.options.env.google_analaytics %>', 'auto');
    ga('send', 'pageview');
  </script>
  <% } %>

And inside you typescript module/component/class (.ts file) you can use variable value (we import js into ts) in such way (below example is for setup sentry): 在打字稿模块/组件/类(.ts文件)中,您可以通过以下方式使用变量值(我们将js导入ts)(以下示例用于设置哨兵):

import * as Raven from 'raven-js'; // http://sentry.io
import * as Env  from '../../.env.js';
...
let sentryDSN = Env['sentry_clientKey_publicDSN']
if(sentryDSN)
{
    Raven.config(sentryDSN).install();
    ...
}
...

Thats all :) 就这样 :)

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

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