Skip to content

Commit

Permalink
Build: Use esprima to remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Apr 12, 2015
1 parent 6bab5b6 commit 0c67730
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions build/tasks/options/requirejs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
module.exports = function( grunt ) {
var path = require( "path" );
var path = require( "path" ),
esprima = require( "esprima" ),
grabFactory = function( contents ) {
var range,
parsedFile = esprima.parse( contents, { range: true } );

// Descend into the parsed file to find the factory function. If we do not find it we
// return the contents as-is, which is correct.
if ( parsedFile && parsedFile.body && parsedFile.body[ 0 ] &&
parsedFile.body[ 0 ].expression && parsedFile.body[ 0 ].expression.arguments &&
parsedFile.body[ 0 ].expression.arguments[ 0 ] &&
parsedFile.body[ 0 ].expression.arguments[ 0 ].range &&
parsedFile.body[ 0 ].expression.arguments[ 0 ].range.length >= 2 ) {

range = parsedFile.body[ 0 ].expression.arguments[ 0 ].range;

// Turn the factory into an IIFE
contents = "( " +
contents.substr( range[ 0 ], range[ 1 ] - range[ 0 ] ) +
" )( jQuery )";
}
return contents;
};

return {
js: {
Expand Down Expand Up @@ -38,9 +60,9 @@ module.exports = function( grunt ) {
},

onBuildWrite: function( moduleName, path, contents ) {
return contents.replace( /@VERSION/g, grunt.config.process(
return grabFactory( contents.replace( /@VERSION/g, grunt.config.process(
"<%= version %>"
) );
) ) );
}
}
}
Expand Down

0 comments on commit 0c67730

Please sign in to comment.