Skip to content

Commit

Permalink
Refactor to shared utility function for addonPreprocessTree / addonPo…
Browse files Browse the repository at this point in the history
…stprocessTree.
  • Loading branch information
rwjblue committed Jan 4, 2017
1 parent c085479 commit be7d4c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 82 deletions.
17 changes: 5 additions & 12 deletions lib/broccoli/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ const omitBy = require('ember-cli-lodash-subset').omitBy;
const isNull = require('ember-cli-lodash-subset').isNull;
const Funnel = require('broccoli-funnel');
const funnelReducer = require('broccoli-funnel-reducer');
let logger = require('heimdalljs-logger')('ember-cli:ember-app');
const logger = require('heimdalljs-logger')('ember-cli:ember-app');
const addonProcessTree = require('../utilities/addon-process-tree');

let SECRET_DEPRECATION_PREVENTION_SYMBOL = crypto.randomBytes(8).toString('hex');

let DEFAULT_CONFIG = {

storeConfigInMeta: true,
autoRun: true,
outputPaths: {
Expand Down Expand Up @@ -552,15 +554,7 @@ EmberApp.prototype.addonTreesFor = function(type) {
@return {Tree} Processed tree
*/
EmberApp.prototype.addonPostprocessTree = function(type, tree) {
let workingTree = tree;

this.project.addons.forEach(addon => {
if (addon.postprocessTree) {
workingTree = addon.postprocessTree(type, workingTree);
}
});

return workingTree;
return addonProcessTree(this.project, 'postprocessTree', type, tree);
};


Expand Down Expand Up @@ -597,8 +591,7 @@ EmberApp.prototype.addonPostprocessTree = function(type, tree) {
@return {Tree} Processed tree
*/
EmberApp.prototype.addonPreprocessTree = function(type, tree) {
return this.project.addons.reduce((workingTree, addon) =>
(addon.preprocessTree ? addon.preprocessTree(type, workingTree) : workingTree), tree);
return addonProcessTree(this.project, 'preprocessTree', type, tree);
};

/**
Expand Down
73 changes: 3 additions & 70 deletions lib/models/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ let ADDON_TREE_CACHE = {
function _resetTreeCache() {
ADDON_TREE_CACHE.clear();
}
var addonProcessTree = require('../utilities/addon-process-tree');

function warn(message) {
if (this.ui) {
Expand Down Expand Up @@ -358,81 +359,13 @@ let addonProto = {
}).filter(Boolean);
},

/**
Runs addon post-processing on a given tree and returns the processed tree.
This enables addons to do process immediately **after** the preprocessor for a
given type is run, but before concatenation occurs. If an addon wishes to
apply a transform before the preprocessors run, they can instead implement the
preprocessTree hook.
To utilize this addons implement `postprocessTree` hook.
An example, would be to apply some broccoli transform on all JS files, but
only after the existing pre-processors have run.
```js
module.exports = {
name: 'my-cool-addon',
postprocessTree: function(type, tree) {
if (type === 'js') {
return someBroccoliTransform(tree);
}
return tree;
}
}
```
@private
@method addonPostprocessTree
@param {String} type Type of tree
@param {Tree} tree Tree to process
@return {Tree} Processed tree
*/
_addonPostprocessTree: function addonPostprocessTree(type, tree) {
return this.addons.reduce(function(workingTree, addon) {
return addon.postprocessTree ? addon.postprocessTree(type, workingTree) : workingTree;
}, tree);
return addonProcessTree(this, 'postprocessTree', type, tree);
},


/**
Runs addon pre-processing on a given tree and returns the processed tree.
This enables addons to do process immediately **before** the preprocessor for a
given type is run. If an addon wishes to apply a transform after the
preprocessors run, they can instead implement the postprocessTree hook.
To utilize this addons implement `preprocessTree` hook.
An example, would be to remove some set of files before the preprocessors run.
```js
var stew = require('broccoli-stew');
module.exports = {
name: 'my-cool-addon',
preprocessTree: function(type, tree) {
if (type === 'js' && type === 'template') {
return stew.rm(tree, someGlobPattern);
}
return tree;
}
}
```
@private
@method addonPreprocessTree
@param {String} type Type of tree
@param {Tree} tree Tree to process
@return {Tree} Processed tree
*/
_addonPreprocessTree: function addonPreprocessTree(type, tree) {
return this.addons.reduce(function(workingTree, addon) {
return addon.preprocessTree ? addon.preprocessTree(type, workingTree) : workingTree;
}, tree);
return addonProcessTree(this, 'preprocessTree', type, tree);
},

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/utilities/addon-process-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = function addonProcessTree(projectOrAddon, hook, processType, tree) {
return projectOrAddon.addons.reduce((workingTree, addon) => {
if (addon[hook]) {
return addon[hook](processType, workingTree);
}

return workingTree;
}, tree);
};

0 comments on commit be7d4c4

Please sign in to comment.