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 aa502a4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions build/tasks/options/requirejs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
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 } );

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 +58,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 aa502a4

Please sign in to comment.