broccolijs / broccoli-concat

Concatenate broccoli trees
MIT License
18 stars 51 forks source link

How to concat from multiple source trees? #2

Closed toranb closed 10 years ago

toranb commented 10 years ago

I'm new to broccoli (thanks for writing this much needed module btw) !

Here is my question. I have a templates tree that is my compiled handlebars templates. I then have a vendor tree that is all my stuff concatted (jquery/ember/etc)

How can I use this concat module to put them both together, such that I output a single JS file ?

Here is that I have so far (note -its returning several files today -not a single JS file)

module.exports = function (broccoli) {
  var concat = require('broccoli-concat')
  var filterTemplates = require('broccoli-template')
  var pickFiles = require('broccoli-static-compiler')

  function preprocess (tree) {
    tree = filterTemplates(tree, {
      extensions: ['handlebars'],
      compileFunction: 'Ember.Handlebars.compile'
    })
    return tree
  }

  var sourceTree = broccoli.makeTree('js');
  var templates = pickFiles(sourceTree, {
    srcDir: '/templates/',
    destDir: '/compiled'
  })
  var appTemplates = preprocess(templates);

  var sourceTree = broccoli.makeTree('js');
  var appJs = concat(sourceTree, {
    inputFiles: [
      'vendor/jquery/jquery.js',
      'vendor/handlebars/handlebars.min.js',
      'vendor/ember/ember.js',
      'app.js',
      'other.js'
    ],
    outputFile: '/deps.min.js'
  });

  return [appJs, appTemplates];
}
rlivsey commented 10 years ago

Hi @toranb, you should be able to use broccoli.MergedTree to join two trees together and then pass that to concat to produce one file.

Eg something like:

var allJS = new broccoli.MergedTree([app, vendor]);
var merged = concat(allJS, {
  inputFiles: ["**/*.js"],
  outputFile: "all.js"
});

hth

toranb commented 10 years ago

Excellent! thanks for the quick reply!