Search code examples
ember-cli

Dynamic assets prefix in Ember-CLI build-generated index.html


I asked this question earlier today, and then deleted it, cause I thought I found an answer that was too obvious to post on here. Basically, how do you change something like this in dist/index.html:

<script src="assets/my.js"></script>

to something like this:

<script src="http://my.assets.com/assets/my.js"></script>

I instantly realized that I can just set the src in app/index.html and it will appear in dist/index.html.

But now I'm realizing that there's a better, if slightly more complex, solution - one that allows different settings in different environments. So I am re-adding the question and posting the answer below.


Solution

  • The solution requires ember-cli-inline-content

    Brocfile.js:

    global require, module, process;
    

    ...

    if (process.env.EMBER_ENV === 'development') {
      app.options.inlineContent = {
        assetPrefix: {
          content:  'http://my.assets.com/'
        }
      };
    }
    

    index.html:

    <script src="{{content-for 'assetPrefix'}}assets/my.js"></script>