简体   繁体   中英

ember-cli-build.js | set broccoli's params depending on environmnet

I tried to search in google this but without the correct answer if I need to do.

It's a simple question I have my ember-cli-build.js

  /*jshint node:true*/
  /* global require, module */
  var EmberApp = require('ember-cli/lib/broccoli/ember-app');

  module.exports = function(defaults) {
    var app = new EmberApp(defaults, {
      storeConfigInMeta: false,
      minifyJS: {
        enabled: false
      },
      minifyCSS: {
        enabled: false
      }
    });

    return app.toTree();
  };

And I want change the value of minifyJS or minifyCSS depending on whether I am running ember in production or dev - how can achieve that?

This answer applies to Ember 2.xx and was written as of 2.15.

In your ember-cli-build.js you can do EmberApp.env() to get the environment. You could also do process.env.EMBER_ENV but the first option is in the Ember CLI Docs , so I'd go with that personally.

Example:

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

  module.exports = function(defaults) {
    var enableMin = (EmberApp.env() === 'production');
    var app = new EmberApp(defaults, {
      storeConfigInMeta: false,
      minifyJS: {
        enabled: enableMin
      },
      minifyCSS: {
        enabled: enableMin
      }
    });


    return app.toTree();
  };

According to EmberCLI docs: Fingerprinting and CDN URLs also app.env property should be available and the following code should be fine, too:

module.exports = function (defaults) {

  const app = new EmberApp(defaults, {});

  if (app.env === 'development') {
      // do something specific for dev
  } else if (app.env === 'production') {
      // do something specific for prod
   }
};

Note: app.env holds "test" when building for testing purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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