-
Notifications
You must be signed in to change notification settings - Fork 4
/
JSMin.cfc
executable file
·393 lines (320 loc) · 13.7 KB
/
JSMin.cfc
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
/********************************************************************************
Copyright 2009 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com
********************************************************************************
Author : Luis Majano
Description :
This is a plugin that interfaces with our own flavor of JSMin to minify
CSS and JavaScript files and also compress them into a single include file. We
have also added LESS support for compiling LESS into CSS for you or on-demand.
Configuration Settings:
jsmin_enable : boolean (defaults to true)
- flag to enable disable the packaging process
jsmin_cacheLocation : string
- the relative file location where cached minified js/css files will be stored,
this location will be expanded. ex: includes/cache
If any of the minify methods cannot find a location argument or the jsmin_cacheLocation setting
then an exception will be thrown.
Usage:
* minify(assets:string, location:relativePath) : html script or link
The main method of operation is minify(). You pass to it a list of assets to compress,
but they have to be of the same type: js or css/less. Do not alternate or weird results will
happen. This method returns a script or link include that you would output on your layouts:
<head>
#getMyPlugin("JSMin").minify('includes/js/myscripts.js,includes/js/jquery.js')#
#getMyPlugin("JSMin").minify('includes/css/site.css,includes/css/boxes.css')#
#getMyPlugin("JSMin").minify('includes/css/site.less,includes/css/boxes.less')#
// With Location
#getMyPlugin("JSMin").minify(assets='includes/css/site.less,includes/css/boxes.less',
location='includes/mycache')#
</head>
As you can see from the example above, you can easily render the minified version of all
the assets. This plugin will minify each asset and if more than 1 is declared, then it
will build a concatenated version of the js or css/less assets and cache them. If the files
are of LESS extension, then the plugin will compile the LESS into CSS files.
You can use the alternate 'location' argument to choose the location of the compressed and
minified files.
* minifyToHead(assets:string, location:relativePath) : void
This method basically sends the HTML links and script tags to the head section using
cfhtmlhead. You can use this method when calling JSMin via handlers or plugins or any other
location than layouts.
* compileLessSource(input:LESS, [ output:absolutePath ]) : CSS
This method compiles LESS source into CSS for you and returns it to you if no output argument is used,
else the compiled source is sent to the output file. An extra goody about this method is that
compilation only takes place if the source LESS file has been modified.
* compileLess(input:absolutePath, [ output:absolutePath ]) : [void | CSS]
This methods can compile an input LESS file into an output CSS file or you can omit passing
the output file argument and the method will return to you the compiled CSS.
*/
component extends="coldbox.system.Plugin" singleton{
/************************************** CONSTRUCTOR *********************************************/
// Constructor
JSMin function init(required controller){
super.init( arguments.controller );
// Plugin Properties
setpluginName( "JSMin" );
setpluginVersion( "4.1" );
setpluginDescription( "A plugin that minifies js/css/less files with style!" );
setpluginAuthor( "Ortus Solutions, Corp" );
setpluginAuthorURL( "http://www.ortussolutions.com" );
// local properties
instance.cacheMap = {};
instance.cacheLessMap = {};
instance.uuid = createobject("java", "java.util.UUID");
instance.cacheDiskLocation = "";
instance.cacheIncludeLocation = "";
//Check settings
if( settingExists( "jsmin_cacheLocation" ) ){
instance.cacheDiskLocation = expandPath( getSetting( "jsmin_cacheLocation" ) );
instance.cacheIncludeLocation = getSetting( "jsmin_cacheLocation" );
}
if( not settingExists( "jsmin_enable" ) ){
setSetting( "jsmin_enable", true );
}
// load jars via javaloader
instance.javaLoader = getPlugin( "JavaLoader" );
instance.javaLoader.appendPaths( getDirectoryFromPath( getMetaData( this ).path ) & "lib" );
// clean cachelocation on init
cleanCache();
return this;
}
/************************************** METHODS *********************************************/
/**
* Prepare source(s) statements using our fabulous jsmin compressor and send them to the head section
* @assets.hint A list of js or css/less files to compress and add to the page. They will be concatenated in order
* @location.hint The location to store the cached assets, else it defaults to the plugin's settings
*/
JSMin function minifyToHead(required assets, location=""){
$htmlhead( minify( arguments.assets, arguments.location ) );
return this;
}
/**
* Prepare source(s) statements using our fabulous jsmin compressor and return back the include HTML
* @assets.hint A list of js or css/less files to compress and add to the page. They will be concatenated in order
* @location.hint The location to store the cached assets, else it defaults to the plugin's settings
*/
function minify(required assets, location=""){
var cacheKey = hash( lcase( arguments.assets ) );
var cachedFile = "";
var cacheIncludeLocation = instance.cacheIncludeLocation;
var cacheDiskLocation = instance.cacheDiskLocation;
// Alternate cache location
if( len( arguments.location ) ){
cacheIncludeLocation = arguments.location;
cacheDiskLocation = expandPath( arguments.location );
}
// Location Checks
if( !len( cacheIncludeLocation ) AND !len( cacheDiskLocation ) ){
$throw("There are no cache include locations so I have no clue where to put the cached assets.",
"Please use the jsmin_cacheLocation setting or the 'location' arguments",
"JSMin.InvalidCacheLocations");
}
// enabled? If not, just render out links
if( not getSetting( "jsmin_enable" ) ){
return renderLinks( arguments.assets );
}
// check if assets already in cacheMap
if( !structKeyExists( instance.cacheMap, cacheKey ) ){
lock name="jsmin.#cacheKey#" type="exclusive" timeout="20" throwOntimeout="true"{
// double secure lock
if( !structKeyExists( instance.cacheMap, cacheKey ) ){
//compress assets
cachedFile = jsmin( cacheKey, arguments.assets, cacheDiskLocation );
// save in cache map
instance.cacheMap[ cacheKey ] = cacheIncludeLocation & "/" & cachedFile;
}
}
}
//return rendered cache links
return renderLinks( instance.cacheMap[ cacheKey ] );
}
/**
* Renders links according to passed in assets
* @assets.hint A list of js/css files to compress and add to the page
*/
function renderLinks(required assets){
var sb = createObject("java","java.lang.StringBuilder").init('');
var event = controller.getRequestService().getContext();
// request assets storage
event.paramValue(name="jsmin_assets", value="", private=true);
for(var x=1; x lte listLen( arguments.assets ); x=x+1){
// Get first asset
var thisAsset = listGetAt( arguments.assets, x );
var thisAssetExt = listLast(thisAsset,".");
// Is asset already loaded
if( NOT listFindNoCase( event.getValue(name="jsmin_assets", private=true), thisAsset ) ){
// Load Asset
if( thisAssetExt eq "js" ){
sb.append('<script src="#thisAsset#" type="text/javascript"></script>');
}
else{
// Do we have a LESS file?
if( thisAssetExt eq "less" ){
// Switch extensions on asset
thisAssetExt = "css";
thisAsset = replacenocase( thisAsset, ".less", ".css" );
// Compile LESS in same directory as asset file
compileLess(input=expandPath( replacenocase( thisAsset, ".css", ".less" ) ), output=expandPath( thisAsset ) );
}
// Append it to buffer.
sb.append('<link href="#thisAsset#" type="text/css" rel="stylesheet" />');
}
// Store It as Loaded
event.setValue(name="jsmin_assets",
value=listAppend( event.getValue(name="jsmin_assets", private=true), thisAsset),
private=true);
}
}
return sb.toString();
}
/**
* Clean the cache location for cached assets
* @location.hint The location to store the cached assets, else it defaults to the plugin's settings
*/
JSMin function cleanCache(location=""){
var qList = "";
var cleanLocation = instance.cacheDiskLocation;
// Location override
if( len( arguments.location ) ){
cleanLocation = expandPath( arguments.location );
}
// Abort if no locations
if( !len( cleanLocation ) ){
return this;
}
qList = directoryList( cleanLocation, false, "path", "*.cache.*" );
for(var thisFile in qList ){
fileDelete( thisFile );
}
return this;
}
/**
* Compile LESS into CSS either to an output file or return
* @input.hint The input LESS absolute file location
* @output.hint The output LESS absolute file location
*/
function compileLess(required input, output){
var compiler = instance.javaLoader.create( "org.lesscss.LessCompiler" );
// Do we have an output file
if( structKeyExists( arguments, "output") ){
// ONLY compile if output does not exist, or if it does, then if the timestamp has changed.
if( !fileExists( arguments.output ) OR isLessModified( arguments.input ) ){
// Compile directly to output file
compiler.compile( createObject("java", "java.io.File").init( arguments.input ),
createObject("java", "java.io.File").init( arguments.output ) );
// Store visited less file timestamp
instance.cacheLessMap[ hash( arguments.input ) ] = getFileInfo( arguments.input ).lastmodified;
}
}
else{
// compile back to user
return compiler.compile( createObject("java", "java.io.File").init( arguments.input ) );
}
}
/**
* Compile LESS source string into CSS
* @input.hint The input LESS source code to compile back to CSS
* @output.hint The output LESS absolute file location
*/
function compileLessSource(required string input, output){
// Compile it
var results = instance.javaLoader.create( "org.lesscss.LessCompiler" ).compile( arguments.input );
// Compile to output?
if( structKeyExists( arguments, "output") ){
fileWrite( arguments.output, results );
}
// Return it
return results;
}
/************************************** PRIVATE *********************************************/
/**
* JSMin a set of files and return the compressed version cache string file
* @cacheKey.hint The cache key in use
* @assets.hint A list of js or css files to compress and add to the page. They will be concatenated in order
* @location.hint The location on disk to store the assets
*/
private function jsmin(required cacheKey, required assets, required location){
var x=1;
var thisAsset = "";
var thisAssetExt = "";
var thisType = "1";
var tempFileName = "";
var fis = "";
var fos = "";
var compressor = "";
var compressedFiles = [];
var sb = "";
var returnAsset = "";
lock name="jsmin.#arguments.cacheKey#" type="exclusive" timeout="20" throwOntimeout="true"{
//Compress and cache files
for(x=1; x lte listLen( arguments.assets ); x++){
// Get first asset
thisAsset = trim( listGetAt( arguments.assets, x ) );
thisAssetExt = listLast( thisAsset, "." );
// Is this a less file?
if( thisAssetExt eq "less" ){
// Switch extensions on asset
thisAssetExt = "css";
thisAsset = replacenocase( thisAsset, ".less", ".css" );
// Compile LESS in same directory as asset file
compileLess(input=expandPath( replacenocase( thisAsset, ".css", ".less" ) ), output=expandPath( thisAsset ) );
}
// File Type: 1=js, 2=css
thisType = 1;
if( thisAssetExt eq "css" ){
thisType = 2;
}
//register temp file
tempFileName = instance.uuid.randomUUID() & "." & thisAssetExt;
// create inputs and outputs for compression
fis = createObject("java", "java.io.FileInputStream").init( expandPath( thisAsset ) );
fos = createObject("java", "java.io.FileOutputStream").init( arguments.location & "/" & tempFileName );
//compress
try{
//Compress with coldbox jsmin
compressor = instance.javaLoader.create( "org.coldbox.JSMin" )
.init( fis, fos, javaCast( "int", thisType ) )
.jsmin();
}
catch(any e){
fis.close();
fos.close();
$throw("Error compression asset: #thisAsset#", e.detail & e.message & e.stackTrace, "JSMin.JavaCompressionException");
}
//Close Files
fis.close();
fos.close();
// register compressed file
arrayAppend( compressedFiles, tempFileName );
}
// Concatenate Files into a single compressed one
sb = createObject("java", "java.lang.StringBuilder").init( '' );
for(x=1; x lte arrayLen( compressedFiles ); x++){
sb.append( fileRead( arguments.location & "/" & compressedFiles[ x ] ) );
fileDelete( arguments.location & "/" & compressedFiles[ x ] );
}
// Create concatenated file according to content.
// Media Query Fix
var sbString = trim( replace( sb.toString(), " and(", " and (", "all" ) );
// Class Select Fix
sbString = trim( REreplace( sbString, "\b\[class", " [class", "all" ) );
// Write it out
tempFileName = hash( sbString, "MD5" ) & ".cache." & listLast( compressedFiles[ 1 ], "." );
//write out buffer
fileWrite( arguments.location & "/" & tempFileName, sbString );
returnAsset = tempFileName;
}
return returnAsset;
}
/**
* Check if the LESS file has been modified or first time we see it.
*/
private function isLessModified(required input){
var key = hash( arguments.input );
if( structKeyExists( instance.cacheLessMap, key ) AND
getFileInfo( arguments.input ).lastmodified EQ instance.cacheLessMap[ key ] ){
return false;
}
return true;
}
}