Include Files at the Root of Your Ember Project

I recently had a need to include two files at the root of my ember project - a service worker and a manifest file.

I sought fine-grained control over my service worker, a la not using the fabulous ember-service-worker plugin.

To accomplish this, I made a few small changes to my ember-cli-build file.

The first two changes involve:

          //ember-cli-build.js

module.exports = function(defaults) {
  var EmberApp = require('ember-cli/lib/broccoli/ember-app');

  const mergeTrees = require('broccoli-merge-trees'); //add this dependency
  const Funnel = require('broccoli-funnel'); //add this dependency
  const STATIC_FILES = ['manifest.json', 'service-worker.js'] //define your files here
          
        

Then, create a tree using the STATIC_FILES constant defined above, and in that tree, instruct broccoli to put the tree's files in the root directory:

          
var staticTree = new Funnel("./public/assets", {
  include: STATIC_FILES,
  destDir: '.'
})
          
        

On the next build, the service worker and manifest files should be in the root of the dist/ folder.

The service worker will be fingerprinted and minified. In most situations this is great, but not this one.

To prevent the service worker from being fingerprinted and minified, we can instruct broccoli to exclude it from those processes:

          
var app = new EmberApp(defaults, {
  minifyJS: {
    options: {
      exclude: ['assets/service-worker.js'] //needs the path
    }
  },
  fingerprint: {
    exclude: ['service-worker.js'] //just needs the filename
  },

          
        

That's it! Now, both files will be in the root directory, unmodified.


thanks to https://highlightjs.org/ for making an awesome syntax highlighter!