ember.jsember-cliember-cli-addons

Ember addon to add files to root directory


I have an addon which needs to copy a set of JS files from their bower directory to the Ember app's root of /dist (this is for scoping rules associated with service workers). I thought maybe I could use the treeForApp hook but while I'm getting no errors I'm also not getting the desired result.

The index.js is:

const Funnel = require('broccoli-funnel');

module.exports = {
  name: 'ember-upup',
  treeForApp: function(tree) {
    tree = new Funnel(tree, { include: 
      [ 
        'bower_components/upup/dist/upup.min.js',
        'bower_components/upup/dist/upup.sw.min.js' 
      ]});

    return this._super.treeForApp.call(this, tree);
  },

Note: I thought I might be able to solve this problem by simply copying the javascript files as part of index.js postBuild hook but while this DOES put the JS files into the dist folder's root it is not served by ember-cli's ember serve apparently if not pushed through one of it's build pipelines.

Stefan Penner has now pointed out the the dist directory is for developers to look at but serving is actually done within the tmp directory structure ... this explains why my "hack" didn't work.


Solution

  • It looks like my initial attempt wasn't entirely far off. To make it work you need to hook into the treeForPublic hook like so:

    const path = require('path');
    const Funnel = require('broccoli-funnel');
    const mergeTrees = require('broccoli-merge-trees');
    const JS_FILES = ['upup.min.js', 'upup.sw.min.js'];
    
    module.exports = {
      treeForPublic: function() {
        const upupPath = path.join(this.app.bowerDirectory, 'upup/dist');
        const publicTree = this._super.treeForPublic.apply(this, arguments);
        const trees = [];
        if (publicTree) {
          trees.push(publicTree);
        }
        trees.push(new Funnel(upupPath, {
          include: JS_FILES,
          destDir: '/'
        }));
    
        return mergeTrees(trees);
      }   
    }
    

    Hope that helps.