Access ENV at build time (as opposed to runtime)


#1

I am running:

    » ember --version
    version: 1.13.13
    node: 0.12.2
    npm: 2.14.10
    os: linux x64

My application needs to be aware of the STATIC_URL where it is being deployed (which depends on the deploy environment “development” / “production”) at two different stages:

  • when building the application, in order to use the right fingerprint prepend
  • on the client application, in order to prefix static assets with the right url

First I thought about defining a STATIC_URL variable on config/environment.js (where I have access to the environment parameter), and then do in ember-cli-build.js:

    // TODO: This does not work
    import config from './config/environment';

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

But I am unable to import the config environment:

import config from './config/environment';
^^^^^^
Unexpected reserved word

Probably what I am trying to do does not make much sense (passing values upwards from config/environment.js -> ember-cli-build.js). So instead of that, what I need is:

  1. Access the build environment parameter (“development” / “production”) from ember-cli-build.js. This should be already available, since the ember-cli guide specifies that fingerprinting is by default only available in production: app.env === 'production'. But I need to access app.env before app is even defined. How to do that?
  2. Configure a STATIC_URL
  3. Pass the STATIC_URL parameter down to config/environment.js (to avoid reconfiguring it again for the client application)

How can I do 1 and 3?


#3

Thanks, but I don’t fully get it: app depends on config (which is defined later!), and config depends on app. How can this work at all?

And indeed:

» ember build --environment="production"

Cannot read property 'STATIC_URL' of undefined

#5

That does not crash, but I am unable to access STATIC_URL as defined on config/environment.js:

module.exports = function(environment) {

  ...

  if (environment === 'production') {
    var STATIC_URL = 'http://cdn.xxx.xxx/';
  }

  ENV.STATIC_URL = STATIC_URL;

}

#7

It should be there, since I define it. How can I log to terminal from ember-cli-build.js?


#9

config.STATIC_URL is indeed empty, but I define it:

/* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'frontend-django-ember-showcase',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    }
  };

  // TODO: we need an empty STATIC_URL when running ember serve
  var STATIC_URL = '';
  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
    var STATIC_URL = 'static/ember-app/';
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.baseURL = '/';
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {
    var STATIC_URL = 'static/ember-app/';
  }

  ENV.STATIC_URL = STATIC_URL;

  return ENV;
};

#10
var config = defaults.project.config(process.env.EMBER_ENV || 'development');
console.log(config.STATIC_URL);

This should do it, I don’t know of a public API way of doing this since you need it before the app is initialized. But this will get you what you need.

Cleaning up my previous posts so I don’t confuse future travelers :slight_smile:


#11

That seems to work, thanks!


#12

Also check out @fivetanley’s https://github.com/fivetanley/ember-cli-dotenv


#13

The public api is simply EmberApp.env()


#14

Yeah, my previous responses, which I deleted, included attempts to use EmberApp.env() but it would occasionally return undefined and in one instance throw on EmberApp.env is not a function.


#15

Really? We are using it in Ember Twiddle without any issues.


#16

I now know what happened, I was calling this from an addon so EmberApp is ember-cli/lib/broccoli/ember-addon not ember-cli/lib/broccoli/ember-app

Sent a PR to fix this https://github.com/ember-cli/ember-cli/pull/5321