-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathjquery-ui-files.js
113 lines (95 loc) · 2.75 KB
/
jquery-ui-files.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
"use strict";
var stripBanner, glob, noDirectory,
banner = require( "./banner" ),
fs = require( "node:fs" ),
path = require( "node:path" ),
sqwish = require( "sqwish" ),
swc = require( "@swc/core" ),
swcOptions = require( "./swc-options" ),
util = require( "./util" ),
Files = require( "./files" ),
filesCache = {};
stripBanner = util.stripBanner;
glob = util.glob;
noDirectory = util.noDirectory;
function replaceVersion( data, version ) {
return data.replace( /@VERSION/g, version );
}
function readFile( path, version ) {
var data = fs.readFileSync( path );
if ( ( /(js|css)$/ ).test( path ) ) {
data = replaceVersion( data.toString( "utf8" ), version );
}
return data;
}
/**
* JqueryUiFiles
*/
function JqueryUiFiles( jqueryUi ) {
var cache, files;
this.cache = cache = filesCache[ jqueryUi.path ] = {};
this.cache.minified = {};
this.jqueryUi = jqueryUi;
this.readFile = function( srcpath ) {
var destpath = srcpath;
if ( !cache[ destpath ] ) {
cache[ destpath ] = {
path: destpath,
data: readFile( jqueryUi.path + srcpath, jqueryUi.pkg.version )
};
}
return cache[ destpath ];
};
this.stripJqueryUiPath = function( filepath ) {
return path.relative( jqueryUi.path, filepath );
};
glob( jqueryUi.path + "!(.*|node_modules|build)" )
.filter( noDirectory )
.map( this.stripJqueryUiPath )
.map( this.readFile );
glob( jqueryUi.path + "!(.*|node_modules|build)/**" )
.filter( noDirectory )
.map( this.stripJqueryUiPath )
.map( this.readFile );
this.componentFiles = Files( glob( jqueryUi.path + "ui/**/*.js" )
.map( this.stripJqueryUiPath )
.map( this.readFile ) );
// Convert {path:<path>, data:<data>} into {path: <data>}.
files = this.cache;
this.cache = Object.keys( files )
.reduce( function( _files, filepath ) {
_files[ filepath ] = files[ filepath ].data;
return _files;
}, {} );
this.baseThemeCss = {
data: this.get( "themes/base/theme.css" )
};
}
JqueryUiFiles.prototype = {
get: function( filepath ) {
return this.cache[ filepath ];
},
min: function( file, options ) {
var minified = this.cache.minified;
options = options || {};
if ( !minified[ file.path ] || options.skipCache ) {
minified[ file.path ] = {
path: file.path.replace( /\.([^.]*)$/, ".min.$1" )
};
if ( ( /\.js$/i ).test( file.path ) ) {
minified[ file.path ].data = swc.minifySync(
file.data.toString( "utf8" ),
swcOptions
).code;
} else if ( ( /\.css$/i ).test( file.path ) ) {
minified[ file.path ].data = sqwish.minify( file.data.toString( "utf8" ) );
}
// Update banner
minified[ file.path ].data =
banner( this.jqueryUi.pkg, null, { minify: true } ) +
stripBanner( minified[ file.path ] );
}
return minified[ file.path ];
}
};
module.exports = JqueryUiFiles;