Skip to content

Commit 05db267

Browse files
author
Gabriel Schulhof
committed
Gruntfile.js: Re-order modules specified via the --modules option such that their order is the same as the order of modules in js/jquery.mobile.js.
1 parent d67ac24 commit 05db267

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

Gruntfile.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,65 @@ module.exports = function( grunt ) {
22
"use strict";
33

44
var _ = grunt.util._,
5+
6+
// Ensure that modules specified via the --modules option are in the same
7+
// order as the one in which they appear in js/jquery.mobile.js. To achieve
8+
// this, we parse js/jquery.mobile.js and reconstruct the array of
9+
// dependencies listed therein.
10+
makeModulesList = function( modules ) {
11+
var start, end, index,
12+
modulesHash = {},
13+
fixedModules = [],
14+
jsFile = grunt.file.read( path.join( "js", "jquery.mobile.js" ) );
15+
16+
modules = modules.split( "," );
17+
18+
// This is highly dependent on the contents of js/jquery.mobile.js
19+
if ( jsFile ) {
20+
start = jsFile.indexOf( "[" );
21+
if ( start > -1 ) {
22+
start++;
23+
end = jsFile.indexOf( "]" );
24+
if ( start < jsFile.length &&
25+
end > -1 && end < jsFile.length && end > start ) {
26+
27+
// Convert list of desired modules to a hash
28+
for ( index = 0 ; index < modules.length ; index++ ) {
29+
modulesHash[ modules[ index ] ] = true;
30+
}
31+
32+
// Split list of modules from js/jquery.mobile.js into an array
33+
jsFile = jsFile
34+
.slice( start, end )
35+
.match( /"[^"]*"/gm );
36+
37+
// Add each desired module to the fixed list of modules in the
38+
// correct order
39+
for ( index = 0 ; index < jsFile.length ; index++ ) {
40+
41+
// First we need to touch up each module from js/jquery.mobile.js
42+
jsFile[ index ] = jsFile[ index ]
43+
.replace( /"/g, "" )
44+
.replace( /^.\//, "" );
45+
46+
// Then, if it's in the hash of desired modules, add it to the
47+
// list containing the desired modules in the correct order
48+
if ( modulesHash[ jsFile[ index ] ] ) {
49+
fixedModules.push( jsFile[ index ] );
50+
}
51+
}
52+
53+
// If we've found all the desired modules, we re-create the comma-
54+
// separated list and return it.
55+
if ( fixedModules.length === modules.length ) {
56+
modules = fixedModules;
57+
}
58+
}
59+
}
60+
}
61+
62+
return modules;
63+
},
564
path = require( "path" ),
665
httpPort = Math.floor( 9000 + Math.random()*1000 ),
766
name = "jquery.mobile",
@@ -186,7 +245,9 @@ module.exports = function( grunt ) {
186245

187246
mainConfigFile: "js/requirejs.config.js",
188247

189-
include: ( grunt.option( "modules" ) || "jquery.mobile" ).split( "," ),
248+
include: ( grunt.option( "modules" ) ?
249+
makeModulesList( grunt.option( "modules" ) ) :
250+
[ "jquery.mobile" ] ),
190251

191252
exclude: [
192253
"jquery",

0 commit comments

Comments
 (0)