aboutsummaryrefslogtreecommitdiffstats
path: root/build/build.js
blob: 249b2792441cddcd5e9f75cb0ad3040f405d8b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
 * Special concat/build task to handle various jQuery build requirements
 * Concats AMD modules, removes their definitions, and includes/excludes specified modules
 */

module.exports = function( grunt ) {

	var fs = require("fs"),
		srcFolder = __dirname + "/../src/",
		rdefineEnd = /\}\);[^}\w]*$/,
		// This is temporary until the skipSemiColonInsertion option makes it to NPM
		requirejs = require("./r"),
		config = {
			baseUrl: "src",
			name: "jquery",
			out: "dist/jquery.js",
			// We have multiple minify steps
			optimize: "none",
			skipSemiColonInsertion: true,
			wrap: {
				startFile: "src/intro.js",
				endFile: "src/outro.js"
			},
			onBuildWrite: convert
		};

	/**
	 * Strip all definitions generated by requirejs
	 * Convert "var" modules to var declarations
	 * "var module" means the module only contains a return statement that should be converted to a var declaration
	 * This is indicated by including the file in any "var" folder
	 * @param {String} name
	 * @param {String} path
	 * @param {String} contents The contents to be written (including their AMD wrappers)
	 */
	function convert( name, path, contents ) {
		// Convert var modules
		if ( /\/var\//.test( path ) ) {
			contents = contents
				.replace( /define\([\w\W]*?return/, "var " + (/var\/([\w-]+)/.exec(name)[1]) + " =" )
				.replace( rdefineEnd, "" );

		// Sizzle treatment
		} else if ( /\/sizzle$/.test( name ) ) {
			contents = "var Sizzle =\n" + contents
				// Remove EXPOSE lines from Sizzle
				.replace( /\/\/\s*EXPOSE[\w\W]*\/\/\s*EXPOSE/, "return Sizzle;" );

		} else {

			// Ignore jQuery's return statement (the only necessary one)
			if ( name !== "jquery" ) {
				contents = contents
					.replace( /return\s+[^\}]+(\}\);[^\w\}]*)$/, "$1" );
			}

			// Remove define wrappers, closure ends, and empty declarations
			contents = contents
				.replace( /define\([^{]*?{/, "" )
				.replace( rdefineEnd, "" )
				// Remove empty definitions
				.replace( /define\(\[[^\]]+\]\)[\W\n]+$/, "" );
		}
		return contents;
	}

	grunt.registerMultiTask(
		"build",
		"Concatenate source, remove sub AMD definitions, (include/exclude modules with +/- flags), embed date/version",
	function() {
		var flag, index,
			done = this.async(),
			flags = this.flags,
			name = this.data.dest,
			minimum = this.data.minimum,
			removeWith = this.data.removeWith,
			excluded = [],
			included = [],
			version = grunt.config( "pkg.version" ),
			/**
			 * Recursively calls the excluder to remove on all modules in the list
			 * @param {Array} list
			 * @param {String} [prepend] Prepend this to the module name. Indicates we're walking a directory
			 */
			excludeList = function( list, prepend ) {
				if ( list ) {
					prepend = prepend ? prepend + "/" : "";
					list.forEach(function( module ) {
						// Exclude var modules as well
						if ( module === "var" ) {
							excludeList( fs.readdirSync( srcFolder + prepend + module ), prepend + module );
							return;
						}
						if ( prepend ) {
							// Skip if this is not a js file and we're walking files in a dir
							if ( !(module = /([\w-\/]+)\.js$/.exec( module )) ) {
								return;
							}
							// Prepend folder name if passed
							// Remove .js extension
							module = prepend + module[1];
						}

						// Avoid infinite recursion
						if ( excluded.indexOf( module ) === -1 ) {
							excluder( "-" + module );
						}
					});
				}
			},
			/**
			 * Adds the specified module to the excluded or included list, depending on the flag
			 * @param {String} flag A module path relative to the src directory starting with + or - to indicate whether it should included or excluded
			 */
			excluder = function( flag ) {
				var m = /^(\+|\-|)([\w\/-]+)$/.exec( flag ),
					exclude = m[1] === "-",
					module = m[2];

				if ( exclude ) {
					// Can't exclude certain modules
					if ( minimum.indexOf( module ) === -1 ) {
						// Add to excluded
						if ( excluded.indexOf( module ) === -1 ) {
							grunt.log.writeln( flag );
							excluded.push( module );
							// Exclude all files in the folder of the same name
							// These are the removable dependencies
							// It's fine if the directory is not there
							try {
								excludeList( fs.readdirSync( srcFolder + module ), module );
							} catch( e ) {
								grunt.verbose.writeln( e );
							}
						}
						// Check removeWith list
						excludeList( removeWith[ module ] );
					} else {
						grunt.log.error( "Module \"" + module + "\" is a mimimum requirement.");
						if ( module === "selector" ) {
							grunt.log.error( "If you meant to replace Sizzle, use -sizzle instead." );
						}
					}
				} else {
					grunt.log.writeln( flag );
					included.push( module );
				}
			};

		// append commit id to version
		if ( process.env.COMMIT ) {
			version += " " + process.env.COMMIT;
		}

		// figure out which files to exclude based on these rules in this order:
		//  dependency explicit exclude
		//  > explicit exclude
		//  > explicit include
		//  > dependency implicit exclude
		//  > implicit exclude
		// examples:
		//  *                  none (implicit exclude)
		//  *:*                all (implicit include)
		//  *:*:-css           all except css and dependents (explicit > implicit)
		//  *:*:-css:+effects  same (excludes effects because explicit include is trumped by explicit exclude of dependency)
		//  *:+effects         none except effects and its dependencies (explicit include trumps implicit exclude of dependency)
		for ( flag in flags ) {
			if ( flag !== "*" ) {
				excluder( flag );
			}
		}

		// Handle Sizzle exclusion
		// Replace with selector-native
		if ( (index = excluded.indexOf("sizzle")) > -1 ) {
			config.rawText = {
				selector: "define(['./selector-native']);"
			};
			excluded.splice( index, 1 );
		}

		grunt.verbose.writeflags( excluded, "Excluded" );
		grunt.verbose.writeflags( included, "Included" );

		// append excluded modules to version
		if ( excluded.length ) {
			version += " -" + excluded.join( ",-" );
			// set pkg.version to version with excludes, so minified file picks it up
			grunt.config.set( "pkg.version", version );
			grunt.verbose.writeln( "Version changed to " + version );
			// Have to use shallow or core will get excluded since it is a dependency
			config.excludeShallow = excluded;
		}
		config.include = included;

		/**
		 * Handle Final output from the optimizer
		 * @param {String} compiled
		 */
		config.out = function( compiled ) {
			compiled = compiled
				// Embed Version
				.replace( /@VERSION/g, version )
				// Embed Date
				// yyyy-mm-ddThh:mmZ
				.replace( /@DATE/g, ( new Date() ).toISOString().replace( /:\d+\.\d+Z$/, "Z" ) );

			// Write concatenated source to file
			grunt.file.write( name, compiled );
		};

		// Trace dependencies and concatenate files
		requirejs.optimize( config, function( response ) {
			grunt.verbose.writeln( response );
			grunt.log.ok( "File '" + name + "' created." );
			done();
		}, function( err ) {
			done( err );
		});
	});

	// Special "alias" task to make custom build creation less grawlix-y
	// Translation example
	//
	//   grunt custom:+ajax,-dimensions,-effects,-offset
	//
	// Becomes:
	//
	//   grunt build:*:*:+ajax:-dimensions:-effects:-offset
	grunt.registerTask( "custom", function() {
		var args = [].slice.call(arguments),
			modules = args.length ? args[0].replace(/,/g, ":") : "";

		grunt.log.writeln( "Creating custom build...\n" );

		grunt.task.run([ "build:*:*:" + modules, "pre-uglify", "uglify", "dist" ]);
	});
};