Ember 2, ember-cli, custom addon, hook to modify text in js before final integrity

After this question Modify app.js and vendor.js before the final integrity seal. How to? I’m building an addon for modify the js before the final integrity step (with ember-cli-sri).

lib/modify-js-before-build/index.js

/* eslint-env node */
'use strict';

module.exports = {
  name: 'modify-js-before-build',

  isDevelopingAddon() {
    return true;
  },

  postprocessTree(type, tree) {
    
    // WHAT TO DO!?

  }

};

lib/modify-js-before-build/package.json

{ ...,
"ember-addon": {
    "before": "ember-cli-sri"
  }
}

But I’m stucked here, I don’t know how to do (I do not know very well broccoli). How can I tell it to add some text in my app.js and vendor.js before ember-cli-sri and other build steps?

I found this one: ember-cli-one-script/index.js at master · DavyJonesLocker/ember-cli-one-script · GitHub. They are using broccoli-concat and broccoli-merge-trees. Something similar to me? But how?

Broccoli works on the concept of merging “trees” of files (i.e. a folders and files in a file system). Plugins get given a number of input trees and an output tree to populate. Files that should be in the output tree must be copied over. This can be done with fs.readFileSync and fs.writeFileSync, but there are a number of basic Broccoli plugins which you can build on to avoid common tasks. One such plugin is Broccoli filter which is a “Helper base class for Broccoli plugins that map input files into output files one-to-one”. The most basic use of this I could fine is Broccoli IIFE which wraps the contents of every JavaScript file with a function

Once you’ve got this Broccoli plugin working how you want, then you can import it to the addon index.js and use it in a manner similar to ember-cli-one-script (i.e only operate on the ‘all’ tree). Both application and vendor files will be in assets in tree (the second argument of postprocessTree

1 Like