blueFast
blueFast

Reputation: 44351

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:

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

I also need to access the environment from within 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?

Upvotes: 7

Views: 3249

Answers (4)

sandstrom
sandstrom

Reputation: 15082

From within ember-cli-build.js you can call EmberApp.env(), like so:

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();
};

Upvotes: 0

Nick Charney Kaye
Nick Charney Kaye

Reputation: 4340

I'm using Ember 2.5. To access the environment parameter from ember-cli-build.js, use process.env.EMBER_ENV. Here's my 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
        },
      });     

  }
}

Upvotes: 5

user9903
user9903

Reputation:

In our Brocfile.js (I guess yours is called ember-cli-build.js?) we are doing something like this:

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. Basically in all our code we run ember like this:

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

Upvotes: 7

Jeff
Jeff

Reputation: 6953

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. 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:

// 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.

Upvotes: 0

Related Questions