Skip to content

Instantly share code, notes, and snippets.

@mleavitt
Created April 6, 2013 19:45
Show Gist options
  • Save mleavitt/5327358 to your computer and use it in GitHub Desktop.
Save mleavitt/5327358 to your computer and use it in GitHub Desktop.

Revisions

  1. mleavitt created this gist Apr 6, 2013.
    40 changes: 40 additions & 0 deletions wrapAMD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    wrapAMD: function (output) {
    var dependencies = ['handlebars'];
    for ( var i = 0; i < output.length; i++ ) {

    // find partials defined as partials['partial-name']
    var template = output[i]
    , partialStart = 'partials\\[\''
    , partialEnd = '\\\']'
    , partialsRegex = new RegExp( partialStart + '[^\n]*' + partialEnd, 'g')
    , dependency = template.match(partialsRegex)
    , l = dependency ? dependency.length-1 : -1;

    // isolate the partial name and add it to the dependencies array
    while( l > -1 ) {
    dependencies.push(dependency[l].replace(new RegExp(partialStart), '').replace(new RegExp(partialEnd), '').replace(/\s/g, ''));
    l--;
    }

    // find partials defined as partials.partialName
    partialStart = 'partials\\.';
    partialEnd = ',';
    partialsRegex = /partials\.[^\s]*/g;
    dependency = template.match(partialsRegex);
    l = dependency ? dependency.length-1 : -1;

    // isolate the partial name and add it to the dependencies array
    while( l > -1 ) {
    dependencies.push(dependency[l].replace(new RegExp(partialStart), '').replace(new RegExp(partialEnd), '').replace(/\s/g, ''));
    l--;
    }

    }

    // remove redundant dependencies
    dependencies = _.uniq(dependencies)

    output.unshift("define(['" + dependencies.join("','") + "'], function(Handlebars) {");
    output.push("});");
    return output;
    }