After much googling and no good examples, here's what I did.
Assuming you're using yeoman with scaffolding by yo ember
generator and using grunt build
to build the dist.
Yo ember-generator v0.8 uses grunt-replace. Upgrade to that version. In short, I'm using grunt-replace to add global vars to index.html. Here's how.
Add this script tag to app/index.html before the combined-scripts.js block:
#app/index.html
<script>
/*
simplify grunt-replace strategy by placing env vars here.
*/
window.MYCOMPANYCOM_ENV = {
env: '@@env',
apiHost: '@@apiHost'
};
</script>
/* ADD THE ABOVE BEFORE THIS SECTION */
<!-- build:js(.tmp) scripts/main.js -->
<script src="scripts/combined-scripts.js"></script>
<!-- endbuild -->
And change the replace config in Gruntfile.js to this:
module.exports = function (grunt) {
var appConfig = grunt.file.readJSON('app_config.json');
replace: {
app: {
options: {
patterns: [
{
json: appConfig['app']
}
]
},
files: [
{src: '<%= yeoman.app %>/index.html', dest: '.tmp/index.html'}
]
},
dist: {
options: {
patterns: [
{
json: appConfig['dist']
}
]
},
files: [
{src: '<%= yeoman.dist %>/index.html', dest: '<%= yeoman.dist %>/index.html'}
]
}
}
Create a new file at ./app_config.json
{
"app": {
"env": "development",
"apiHost": "http://localhost:8080"
},
"dist": {
"env": "dist",
"apiHost": "/"
}
}
Now your app scripts have access to a global vars defined in app_config.json.
I won't go into more detail but this works fine in development. For grunt build
, I moved the replace:dist
step to the end of the build steps, and replaced the @@ember variable in index.html with a path to the bower component.