Access ENV at build time (as opposed to runtime)

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.jsember-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?

1 Like

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

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;

}

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

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;
};
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:

That seems to work, thanks!

Also check out @fivetanley’s GitHub - fivetanley/ember-cli-dotenv: Dotenv for your ember app through ember-cli

The public api is simply EmberApp.env()

2 Likes

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.

1 Like

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

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 EmberAddon access to `env` by jasonmit · Pull Request #5321 · ember-cli/ember-cli · GitHub