简体   繁体   English

如何从 ember-cli-build.js 访问环境参数

[英]How to access the environment parameter from ember-cli-build.js

When you do:当你这样做时:

ember build --environment="production"

The environment parameter is made available in config/environment.js : environment参数在config/environment.js可用:

module.exports = function(environment) {
  ...
};

I also need to access the environment from within ember-cli-build.js :我还需要从ember-cli-build.js访问环境:

let STATIC_URL = "TODO";  // This depends on the deploy "environment" parameter

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    fingerprint: {
      enabled: true,
      prepend: STATIC_URL,
    },
  });

  return app.toTree();
};

How can I access the environment parameter from ember-cli-build.js ?如何从ember-cli-build.js访问环境参数?

In our Brocfile.js (I guess yours is called ember-cli-build.js?) we are doing something like this:在我们的 Brocfile.js(我猜你的叫做 ember-cli-build.js?)我们正在做这样的事情:

var EmberApp = require('ember-cli/broccoli/ember-app');
var environment = process.env.EMBER_ENV || 'development';
var config = require('./config/environment')(environment);

var app = new EmberApp(/* configuration for the app... */ );
module.exports = app.toTree();

The line where we're assigning to the environment variable is how you get which environment you're in. We use the EMBER_ENV command line variable but you can use something different.我们分配给环境变量的行是你如何获得你所在的环境。我们使用 EMBER_ENV 命令行变量,但你可以使用不同的东西。 Basically in all our code we run ember like this:基本上在我们所有的代码中,我们都像这样运行 ember:

EMBER_ENV=production ember-cli start
EMBER_ENV=test ember-cli test

# the next lines use the same 'development' environment
EMBER_ENV=development ember-cli start
ember-cli start

I'm using Ember 2.5.我正在使用 Ember 2.5。 To access the environment parameter from ember-cli-build.js, use process.env.EMBER_ENV .要从 ember-cli-build.js 访问环境参数,请使用process.env.EMBER_ENV Here's my ember-cli-build.js:这是我的 ember-cli-build.js:

let EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  let app = getApp(defaults, process.env.EMBER_ENV);

  // Use `app.import` to add additional libraries to the generated
  // output files.

  return app.toTree();
};

function getApp(defaults, environment) {
  switch(environment) {

    case "production":
      return new EmberApp(defaults, {
        fingerprint: {
          enabled: true
        },
      });

    default:
      return new EmberApp(defaults, {
        fingerprint: {
          enabled: false
        },
      });     

  }
}

EDIT :编辑
I just recognized that you need environment already in ember-cli-build.js not only in app.js, so this answer might not work.我刚刚认识到您不仅在 app.js 中还需要 ember-cli-build.js 中已经存在的环境,所以这个答案可能不起作用。 I'll leave it posted anyway, may be it'll help!无论如何我都会把它贴出来,也许它会有所帮助!

my configuration is a bit different, but the including of enviroment is the same:我的配置有点不同,但包括环境是一样的:

// app.js - I stripped some unrelated stuff
import Ember from 'ember';
import Resolver from 'ember/resolver';

import ENV from 'my-appname/config/environment';

var App;


App = Ember.Application.extend({
  fingerprint: {
     enabled: true,
     prepend: ENV.STATIC_URL,
  },
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  Resolver: Resolver,
});

export default App;

Now you can change STATIC_URL in enviroment.js depending on the enviroment you get passed to:现在,您可以根据传递给的环境更改 environment.js 中的 STATIC_URL:

// config/enviroment.js
module.exports = function(environment) {
    var ENV;
    if(environment==='production') {
        ENV.STATIC_URL='foo';
    }
    return ENV;
}

Note that config/environment lives under your dasherized appname.请注意, config/environment 位于您的dasherized appname 下。

From within ember-cli-build.js you can call EmberApp.env() , like so:ember-cli-build.js您可以调用EmberApp.env() ,如下所示:

let STATIC_URL = EmberApp.env() === 'development' ? "TODO" : "READY";

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    fingerprint: {
      enabled: true,
      prepend: STATIC_URL,
    },
  });

  return app.toTree();
};

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

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