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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/**
* Special build task to handle various jQuery build requirements.
* Compiles JS modules into one bundle, sets the custom AMD name,
* and includes/excludes specified modules
*/
"use strict";
module.exports = function( grunt ) {
const fs = require( "fs" );
const path = require( "path" );
const rollup = require( "rollup" );
const slimBuildFlags = require( "./lib/slim-build-flags" );
const rollupFileOverrides = require( "./lib/rollup-plugin-file-overrides" );
const srcFolder = path.resolve( `${ __dirname }/../../src` );
const read = function( fileName ) {
return grunt.file.read( `${ srcFolder }/${ fileName }` );
};
// Catch `// @CODE` and subsequent comment lines event if they don't start
// in the first column.
const wrapper = read( "wrapper.js" )
.split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/ );
const inputFileName = "jquery.js";
const inputRollupOptions = {
input: `${ srcFolder }/${ inputFileName }`
};
const outputRollupOptions = {
// The ESM format is not actually used as we strip it during
// the build; it's just that it doesn't generate any extra
// wrappers so there's nothing for us to remove.
format: "esm",
intro: wrapper[ 0 ]
.replace( /\n*$/, "" ),
outro: wrapper[ 1 ]
.replace( /^\n*/, "" )
};
const fileOverrides = new Map();
function getOverride( filePath ) {
return fileOverrides.get( path.resolve( filePath ) );
}
function setOverride( filePath, source ) {
// We want normalized paths in overrides as they will be matched
// against normalized paths in the file overrides Rollup plugin.
fileOverrides.set( path.resolve( filePath ), source );
}
grunt.registerMultiTask(
"build",
"Build jQuery ECMAScript modules, " +
"(include/exclude modules with +/- flags), embed date/version",
async function() {
const done = this.async();
try {
const flags = this.flags;
const optIn = flags[ "*" ];
let name = grunt.option( "filename" );
const minimum = this.data.minimum;
const removeWith = this.data.removeWith;
const excluded = [];
const included = [];
let version = grunt.config( "pkg.version" );
// We'll skip printing the whole big exclusions for a bare `build:*:*:slim` which
// usually comes from `custom:slim`.
const isPureSlim = !!( flags.slim && flags[ "*" ] &&
Object.keys( flags ).length === 2 );
delete flags[ "*" ];
if ( flags.slim ) {
delete flags.slim;
for ( const flag of slimBuildFlags ) {
flags[ flag ] = true;
}
}
/**
* 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
*/
const excludeList = ( 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 be included or excluded
*/
const excluder = flag => {
let additional;
const m = /^(\+|-|)([\w\/-]+)$/.exec( flag );
const exclude = m[ 1 ] === "-";
const 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 {
// `selector` is a special case as we don't just remove
// the module, but we replace it with `selector-native`
// which re-uses parts of the `src/selector` folder.
if ( module !== "selector" ) {
excludeList(
fs.readdirSync( `${ srcFolder }/${ module }` ),
module
);
}
} catch ( e ) {
grunt.verbose.writeln( e );
}
}
additional = removeWith[ module ];
// Check removeWith list
if ( additional ) {
excludeList( additional.remove || additional );
if ( additional.include ) {
included.push( ...additional.include );
grunt.log.writeln( "+" + additional.include );
}
}
} else {
grunt.log.error( "Module \"" + module + "\" is a minimum requirement." );
}
} else {
grunt.log.writeln( flag );
included.push( module );
}
};
// Filename can be passed to the command line using
// command line options
// e.g. grunt build --filename=jquery-custom.js
name = name ? `dist/${ name }` : this.data.dest;
// 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 ( const flag in flags ) {
excluder( flag );
}
// Remove the jQuery export from the entry file, we'll use our own
// custom wrapper.
setOverride( inputRollupOptions.input,
read( inputFileName ).replace( /\n*export default jQuery;\n*/, "\n" ) );
// Replace exports/global with a noop noConflict
if ( excluded.includes( "exports/global" ) ) {
const index = excluded.indexOf( "exports/global" );
setOverride( `${ srcFolder }/exports/global.js`,
"import jQuery from \"../core.js\";\n\n" +
"jQuery.noConflict = function() {};" );
excluded.splice( index, 1 );
}
// Set a desired AMD name.
let amdName = grunt.option( "amd" );
if ( amdName != null ) {
if ( amdName ) {
grunt.log.writeln( "Naming jQuery with AMD name: " + amdName );
} else {
grunt.log.writeln( "AMD name now anonymous" );
}
// Remove the comma for anonymous defines
setOverride( `${ srcFolder }/exports/amd.js`,
read( "exports/amd.js" )
.replace( /(\s*)"jquery"(,\s*)/,
amdName ? "$1\"" + amdName + "\"$2" : "" ) );
}
grunt.verbose.writeflags( excluded, "Excluded" );
grunt.verbose.writeflags( included, "Included" );
// Indicate a Slim build without listing all the exclusions
// to save space.
if ( isPureSlim ) {
version += " slim";
// Append excluded modules to version.
} else if ( excluded.length ) {
version += " -" + excluded.join( ",-" );
}
if ( excluded.length ) {
// Set pkg.version to version with excludes or with the "slim" marker,
// so minified file picks it up but skip the commit hash the same way
// it's done for the full build.
const commitlessVersion = version.replace( " " + process.env.COMMIT, "" );
grunt.config.set( "pkg.version", commitlessVersion );
grunt.verbose.writeln( "Version changed to " + commitlessVersion );
// Replace excluded modules with empty sources.
for ( const module of excluded ) {
setOverride(
`${ srcFolder }/${ module }.js`,
// The `selector` module is not removed, but replaced
// with `selector-native`.
module === "selector" ? read( "selector-native.js" ) : ""
);
}
}
// Turn off opt-in if necessary
if ( !optIn ) {
// Remove the default inclusions, they will be overwritten with the explicitly
// included ones.
setOverride( inputRollupOptions.input, "" );
}
// Import the explicitly included modules.
if ( included.length ) {
setOverride( inputRollupOptions.input,
getOverride( inputRollupOptions.input ) + included
.map( module => `import "./${module}.js";` )
.join( "\n" ) );
}
const bundle = await rollup.rollup( {
...inputRollupOptions,
plugins: [ rollupFileOverrides( fileOverrides ) ]
} );
const { output: [ { code } ] } = await bundle.generate( outputRollupOptions );
const compiledContents = code
// Embed Version
.replace( /@VERSION/g, version )
// Embed Date
// yyyy-mm-ddThh:mmZ
.replace(
/@DATE/g,
( new Date() ).toISOString()
.replace( /:\d+\.\d+Z$/, "Z" )
);
grunt.file.write( name, compiledContents );
grunt.log.ok( `File '${ name }' created.` );
done();
} catch ( 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
//
// There's also a special "slim" alias that resolves to the jQuery Slim build
// configuration:
//
// grunt custom:slim
grunt.registerTask( "custom", function() {
const args = this.args;
const modules = args.length ?
args[ 0 ].split( "," ).join( ":" ) :
"";
grunt.log.writeln( "Creating custom build...\n" );
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "uglify", "dist" ] );
} );
};
|