-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathpackage.js
393 lines (339 loc) · 10.4 KB
/
package.js
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"use strict";
var indexTemplate,
amdBuilder = require( "builder-amd" ),
banner = require( "./banner" ),
extend = require( "util" )._extend,
fs = require( "node:fs" ),
handlebars = require( "handlebars" ),
jqueryCssBuilder = require( "builder-jquery-css" ),
path = require( "node:path" ),
sqwish = require( "sqwish" ),
ThemeRoller = require( "./themeroller" ),
semver = require( "semver" ),
swc = require( "@swc/core" ),
swcOptions = require( "./swc-options" );
indexTemplate = handlebars.compile( fs.readFileSync( __dirname + "/../template/zip/index.html", "utf8" ) );
function getJsBundleIntro( version ) {
if ( semver.lt( version, "1.13.0-a" ) ) {
return "(function( factory ) {\n" +
" if ( typeof define === \"function\" && define.amd ) {\n" +
"\n" +
" // AMD. Register as an anonymous module.\n" +
" define([ \"jquery\" ], factory );\n" +
" } else {\n" +
"\n" +
" // Browser globals\n" +
" factory( jQuery );\n" +
" }\n" +
"}(function( $ ) {";
} else {
return "( function( factory ) {\n" +
" \"use strict\";\n" +
"\n" +
" if ( typeof define === \"function\" && define.amd ) {\n" +
"\n" +
" // AMD. Register as an anonymous module.\n" +
" define( [ \"jquery\" ], factory );\n" +
" } else {\n" +
"\n" +
" // Browser globals\n" +
" factory( jQuery );\n" +
" }\n" +
"} )( function( $ ) {\n" +
"\"use strict\";\n";
}
}
function getJsBundleOutro( version ) {
if ( semver.lt( version, "1.13.0-a" ) ) {
return "}));";
} else {
return "} );";
}
}
// Switch to native Promise.withResolvers() once Node.js 22 is our baseline.
function getPromiseWithResolvers() {
let resolve, reject;
const promise = new Promise( ( res, rej ) => {
resolve = res;
reject = rej;
} );
return { promise, resolve, reject };
}
function camelCase( input ) {
return input.toLowerCase().replace( /[-/](.)/g, function( match, group1 ) {
return group1.toUpperCase();
} );
}
function stripBanner( data ) {
if ( data instanceof Buffer ) {
data = data.toString( "utf8" );
}
return data.replace( /^\s*\/\*[\s\S]*?\*\/\s*/g, "" );
}
/**
* scope( css, scope )
* - css [ String ]: CSS content.
* - scopeString [ String ]: The scope-string that will be added before each css ".ui*" selector.
*
* Returns the scoped css.
*/
function scope( css, scopeString ) {
return css.replace( /(\.ui[^\n,}]*)/g, scopeString + " $1" );
}
function Package( files, runtime ) {
this.jsBundle = getPromiseWithResolvers();
this.jsFileNames = runtime.components.map( function( component ) {
return component + ".js";
} );
this.pkgJson = JSON.parse( files[ "package.json" ].toString( "utf-8" ) );
this.structureCss = getPromiseWithResolvers();
this.structureCssFileNames = [];
this.structureMinCss = getPromiseWithResolvers();
this.themeCss = getPromiseWithResolvers();
this.themeCssFileNames = [];
this.themeMinCss = getPromiseWithResolvers();
this.zipBasedir = "jquery-ui-" + this.pkgJson.version + ".custom";
this.zipFilename = this.zipBasedir + ".zip";
}
extend( Package.prototype, {
"AUTHORS.txt": "AUTHORS.txt",
"LICENSE.txt": "LICENSE.txt",
"package.json": "package.json",
"external/jquery/jquery.js": function() {
if ( !this.runtime.components.length ) {
return null;
}
return this.files[ "external/jquery/jquery.js" ];
},
"images": function( callback ) {
var self = this;
this.themeCss.promise.then( function( css ) {
if ( css === null ) {
return callback( null, null );
}
self.themeroller.generateImages( callback );
} ).catch( callback );
},
"index.html": function() {
var version = this.pkgJson.version;
if ( !this.runtime.components.length ) {
return null;
}
return indexTemplate( {
ui: this.runtime.components.reduce( function( sum, component ) {
sum[ component.replace( /^.+\//, "" ) ] = true;
return sum;
}, {} ),
version: version
} );
},
"jquery-ui.css": function( callback ) {
var self = this,
pkgJson = this.pkgJson,
structureCssFileNames = this.structureCssFileNames,
themeCssFileNames = this.themeCssFileNames;
Promise
.all( [ this.structureCss.promise, this.themeCss.promise ] )
.then( function( [ structureCss, themeCss ] ) {
var _banner = banner( pkgJson, structureCssFileNames.concat( themeCssFileNames ), {
customThemeUrl: self.customThemeUrl
} );
themeCss = themeCss ? "\n" + themeCss : "";
callback( null, _banner + structureCss + themeCss );
} )
.catch( callback );
},
"jquery-ui.js": function( callback ) {
var jsBundle = this.jsBundle,
jsFileNames = this.jsFileNames,
pkgJson = this.pkgJson;
if ( !this.runtime.components.length ) {
return callback( null, null );
}
amdBuilder( this.files, {
appDir: "ui",
exclude: [ "jquery" ],
include: this.runtime.components,
onBuildWrite: ( id, path, contents ) => {
var name;
if ( id === "jquery" ) {
return contents;
}
name = camelCase( id.replace( /ui\//, "" ).replace( /\.js$/, "" ) );
if ( semver.lt( this.pkgJson.version, "1.13.0-a" ) ) {
return contents
// Remove UMD wrapper.
.replace( /\( ?function\( factory[\s\S]*?\( ?function\( [^\)]* \) \{/, "" )
.replace( /\} ?\) ?\);\s*?$/, "" )
// Replace return exports for var =.
.replace( /\nreturn/, "\nvar " + name + " =" );
} else {
return contents
// Remove UMD wrappers of UI & jQuery Color.
.replace( /\( ?function\( ?(?:root, ?)?factory\b[\s\S]*?\( ?(?:this, ?)?function\( ?[^\)]* ?\) ?\{(?:\s*"use strict";\n)?/, "" )
.replace( /\} ?\);\s*?$/, "" )
// Replace return exports for var =.
.replace( /\nreturn/, "\nvar " + name + " =" );
}
},
optimize: "none",
useStrict: semver.gte( this.pkgJson.version, "1.13.0-a" ),
paths: {
jquery: "../external/jquery/jquery"
},
wrap: {
start: getJsBundleIntro( this.pkgJson.version ),
end: getJsBundleOutro( this.pkgJson.version )
}
}, function( error, js ) {
var _banner;
if ( error ) {
jsBundle.reject( error );
return callback( error );
}
// Remove leftover define created during rjs build
js = js.replace( /define\(".*/, "" );
jsBundle.resolve( js );
_banner = banner( pkgJson, jsFileNames );
callback( null, _banner + js );
} );
},
"jquery-ui.structure.css": function( callback ) {
var self = this,
structureCssFileNames = this.structureCssFileNames,
runtime = this.runtime,
structureCss = this.structureCss,
themeCss = this.themeCss;
if ( !this.runtime.components.length ) {
structureCss.resolve( "" );
return callback( null, null );
}
jqueryCssBuilder( this.files, "structure", {
appDir: "ui",
include: this.runtime.components,
onCssBuildWrite: function( _path, data ) {
if ( data === undefined ) {
throw new Error( "onCssBuildWrite failed (data is undefined) for path " + _path );
}
structureCssFileNames.push( path.basename( _path ) );
return stripBanner( data );
},
paths: {
jquery: "../external/jquery/jquery"
}
}, function( error, css ) {
if ( error ) {
structureCss.reject( error );
return callback( error );
}
// Scope all rules due to specificity issue with tabs (see #87)
if ( runtime.scope ) {
css = scope( css, runtime.scope );
}
structureCss.resolve( css );
// Add Banner
themeCss.promise.then( function() {
var banner = self.files[ "themes/base/theme.css" ]
.replace( /\*\/[\s\S]*/, "*/" )
.replace( /\n.*\n.*themeroller.*/, "" );
banner = banner ? banner + "\n" : "";
callback( null, banner + css );
} );
} );
},
"jquery-ui.theme.css": function() {
var css;
if ( this.runtime.themeVars === null ) {
this.themeCss.resolve( null );
return null;
}
this.themeCssFileNames.push( "theme.css" );
if ( this.runtime.jqueryUi ) {
this.themeroller = new ThemeRoller( {
vars: this.runtime.themeVars,
jqueryUi: this.runtime.jqueryUi
} );
} else if ( this.runtime.jQueryUiVersion ) {
this.themeroller = new ThemeRoller( {
vars: this.runtime.themeVars,
version: this.runtime.jQueryUiVersion
} );
} else {
this.themeroller = new ThemeRoller( {
vars: this.runtime.themeVars
} );
}
this.customThemeUrl = this.themeroller.url();
css = this.themeroller.css();
if ( this.runtime.scope ) {
css = scope( css, this.runtime.scope );
}
this.themeCss.resolve( stripBanner( css ) );
return css;
},
"jquery-ui.min.css": function( callback ) {
var self = this,
pkgJson = this.pkgJson,
structureCssFileNames = this.structureCssFileNames,
themeCssFileNames = this.themeCssFileNames;
Promise
.all( [ this.structureMinCss.promise, this.themeMinCss.promise ] )
.then( function( [ structureMinCss, themeMinCss ] ) {
var _banner = banner( pkgJson, structureCssFileNames.concat( themeCssFileNames ), {
customThemeUrl: self.customThemeUrl,
minify: true
} );
themeMinCss = themeMinCss || "";
callback( null, _banner + structureMinCss + themeMinCss );
} )
.catch( callback );
},
"jquery-ui.min.js": function( callback ) {
var jsFileNames = this.jsFileNames,
pkgJson = this.pkgJson;
if ( !this.runtime.components.length ) {
return callback( null, null );
}
this.jsBundle.promise.then( function( js ) {
var _banner = banner( pkgJson, jsFileNames, { minify: true } );
var minJs = swc.minifySync( js, swcOptions ).code;
callback( null, _banner + minJs );
} ).catch( callback );
},
"jquery-ui.structure.min.css": function( callback ) {
var pkgJson = this.pkgJson,
structureMinCss = this.structureMinCss;
if ( !this.runtime.components.length ) {
structureMinCss.resolve();
return callback( null, null );
}
this.structureCss.promise.then( function( css ) {
var _banner = banner( pkgJson, null, { minify: true } ),
minCss = sqwish.minify( css );
structureMinCss.resolve( minCss );
callback( null, _banner + minCss );
}, function( error ) {
structureMinCss.reject( error );
callback( error );
} );
},
"jquery-ui.theme.min.css": function( callback ) {
var pkgJson = this.pkgJson,
themeMinCss = this.themeMinCss;
this.themeCss.promise.then( function( css ) {
var _banner, minCss;
if ( css === null ) {
themeMinCss.resolve( null );
return callback( null, null );
}
_banner = banner( pkgJson, null, { minify: true } );
minCss = sqwish.minify( css );
themeMinCss.resolve( minCss );
callback( null, _banner + minCss );
}, function( error ) {
themeMinCss.reject( error );
callback( error );
} );
}
} );
module.exports = Package;