0

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
    }
  };
}
yuяi
  • 2,617
  • 1
  • 23
  • 46

1 Answers1

3

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.

Duncan Walker
  • 2,182
  • 19
  • 28
  • Unfortunately, doing a **require** for a file inside the app directory throws a **Cannot find module** error. And using the **broccoli-asset-rev** add-on doesn't help the issue at hand. I'm using a similar component already, and need to be able to insert a variable instead of hard-coded values. – yuяi Jan 29 '15 at 01:01
  • 1
    The file you're trying to import using require has to use the correct module syntax in it (note - not ES6 module syntax). Usually this means defining an object as `module.exports` in the file at `path/to/config.js`. Then you should be able to import it with no problem - in fact Ember CLI does this in the Brocfile blueprint. What version of Ember CLI are you using? – Duncan Walker Jan 29 '15 at 06:23
  • I will try to change the syntax of my config file. As you can see in the question, currently I am using ES6 module syntax. I am on Ember-CLI 0.1.7. – yuяi Jan 29 '15 at 15:37
  • Using **module.exports** did the trick. If you update your answer to include that bit, I will accept it. – yuяi Jan 29 '15 at 16:58