Search code examples
ember.jsember-clibroccolijs

Ember-CLI: include app file in Brocfile.js


I have a config setting I'd like to include in Brocfile.js, so that I don't set it in the file directly. For example, in config/custom.js I would have something like:

export default {
  path: 'http://abc.blah.com/'
};

In my Brocfile, I'd like to do something like:

if (process.env.EMBER_ENV === 'development') {
  app.options.inlineContent = {
    assetPrefix: {
      content: customConfig.path
    }
  };
}

How do I import/include custom.js in my Brocfile, and use its path property? I tried importing, but get:

import config from 'config/custom.js';
^^^^^^
Unexpected reserved word

UPDATE:

Based on the answer below, here's what my files ended up looking like:

// config/custom.js

module.exports = {
  assetPrependPath: 'http://abc.blah.com/'
};


// Brocfile.js

var customConfig = require('./config/custom');

...

if (process.env.EMBER_ENV === 'development') {
  app.options.inlineContent = {
    assetPrefix: {
      content:  customConfig.assetPrependPath
    }
  };
}

Solution

  • You are getting the error because Brocfile.js is not parsed by the ES6 parser. This is because the Brocfile is not in your app's tree (the app directory). Therefore, you cannot use syntax like import in the Brocfile.

    Your Brocfile is used for building the application. Environment specific variables like those you're setting inside if (process.env.EMBER_ENV === 'development') should go in Ember CLI's config/environment.js file. Any properties in the environment.js's ENV.APP object are passed to your application instance and addons. Thus, depending on your end goal, you may be able to take that approach and have no need to import into the Brocfile.

    If you really want to import something into your Brocfile you will need to use a module loader like require.js:

    var Config = require('path/to/config.js');`
    

    ... And then in you path/to/config.js file:

    module.exports = {
      // Options here
    }
    

    Asset Revision

    Looking at your use case you might want to look at a Broccoli addon like broccoli-asset-rev to do the heavy lifting for you.