forked from liferealized/cfwheels-asset-bundler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetBundler.cfc
300 lines (241 loc) · 15.3 KB
/
AssetBundler.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
<cfcomponent output="false">
<cffunction name="init" access="public" output="false">
<cfscript>
StructDelete(application, "assetBundler", false);
this.version = "1.0,1.1,1.1.1,1.1.2,1.1.3,1.1.4,1.1.5,1.1.6,1.1.7,1.1.8,1.3,1.3.1,1.3.2,1.3.3,1.3.4,1.4.0,1.4.1";
this.environments = "test,production";
</cfscript>
<cfreturn this />
</cffunction>
<cffunction name="generateBundle" access="public" output="false" returntype="void" mixin="application,controller">
<cfargument name="type" type="string" required="true" hint="Can be either `js`, `css` or `less`" />
<cfargument name="sources" type="string" required="false" default="" />
<cfargument name="bundle" type="string" required="false" default="" />
<cfargument name="compress" type="boolean" required="false" default="false" />
<cfscript>
// this method is used in /events/onApplicationStart.cfm to create the bundles necessary for the application to run
// this is done on application start so we don't kill an individuals request performance
// also allows bundling to work accross a server cluster
var loc = {
extension = "." & arguments.type
, relativeFolderPath = application.wheels.webPath & application.wheels.stylesheetPath & "/"
, bundleInfo = {}
, bundleContents = ""
};
if (StructKeyExists(arguments, "source"))
arguments.sources = arguments.source;
arguments.sources = $listClean(arguments.sources);
// create our application scope structs if they do not exist
if (!StructKeyExists(application, "assetBundler"))
application.assetBundler = {};
switch (arguments.type)
{
case "js":
{
loc.relativeFolderPath = application.wheels.webPath & application.wheels.javascriptPath & "/";
break;
}
case "less":
{
if (application.wheels.showErrorInformation && !StructKeyExists(variables, "generateLessCssFiles"))
$throw(type="Wheels", message="Plugin Missing", extendedInfo="You must include the less css plugin to bundle less files.");
if (ListFindNoCase(application.wheels.plugins.assetBundler.environments, application.wheels.environment))
{
generateLessCssFiles(sources=arguments.sources);
arguments.sources = mapLessCssFiles(sources=arguments.sources);
arguments.type = "css";
loc.extension = "." & arguments.type;
}
break;
}
}
if (not StructKeyExists(application.assetBundler, arguments.type))
application.assetBundler[arguments.type] = {};
// process our sources to see if we have any directories to expand
loc.array=ListToArray(arguments.sources);
for (i=1; i LTE ArrayLen(loc.array); i=i+1)
{
if (REFind("\*$", loc.array[i]))
{
// we found a star at the end of the path name so let's get all of
// the files under the designated folder for our extension type
loc.folderFiles = $getAllFilesInDirectory(directoryPath=REReplace(loc.array[i], "\*$", "", "one"), argumentCollection=loc);
arguments.sources = ListSetAt(arguments.sources, ListFind(arguments.sources, loc.array[i]), loc.folderFiles);
}
}
loc.bundleInfo.name = arguments.bundle;
loc.bundleInfo.sources = arguments.sources;
// if we are not in testing or production, do nothing
if (!ListFindNoCase(application.wheels.plugins.assetBundler.environments, application.wheels.environment))
{
application.assetBundler[arguments.type][arguments.bundle] = StructCopy(loc.bundleInfo);
return;
}
// conbine all of the files listed as one file
loc.bundleContents = $getFileContents(arguments.sources, loc.relativeFolderPath, loc.extension);
// check to see if we should compress the contents
if (arguments.compress)
loc.bundleContents = $compressContents(loc.bundleContents, arguments.type);
// store info about our bundle in the application scope
loc.bundleInfo.md5hash = lCase(Hash(loc.bundleContents));
loc.bundleInfo.createdAt = Now();
application.assetBundler[arguments.type][arguments.bundle] = StructCopy(loc.bundleInfo);
if (Find("/", arguments.bundle))
{
loc.directory = ListDeleteAt(arguments.bundle, ListLen(arguments.bundle, "/"), "/");
if (not DirectoryExists(ExpandPath(loc.relativeFolderPath & loc.directory & "/")))
$directory(action="create", directory=ExpandPath(loc.relativeFolderPath & loc.directory & "/"));
}
// build the bundle file path with the md5 hash of the file's contents
// this method ensures that browsers do not cache old versions of the .js or .css
loc.bundleFilePath = ExpandPath(loc.relativeFolderPath & arguments.bundle & "-" & loc.bundleInfo.md5hash & loc.extension);
FileWrite(loc.bundleFilePath, loc.bundleContents);
FileSetAccessMode(loc.bundleFilePath, "644");
</cfscript>
<cfreturn />
</cffunction>
<cffunction name="styleSheetLinkTag" access="public" output="false" returntype="string" mixin="controller">
<cfargument name="sources" type="string" required="false" default="" />
<cfargument name="type" type="string" required="false" default="#application.wheels.functions.styleSheetLinkTag.type#" />
<cfargument name="media" type="string" required="false" default="#application.wheels.functions.styleSheetLinkTag.media#" />
<cfargument name="bundle" type="string" required="false" default="" />
<cfreturn $callOriginalIncludeMethod($includeMethod="styleSheetLinkTag", $fileType="css", argumentCollection=arguments) />
</cffunction>
<cffunction name="javaScriptIncludeTag" access="public" output="false" returntype="string" mixin="controller">
<cfargument name="sources" type="string" required="false" default="" />
<cfargument name="type" type="string" required="false" default="#application.wheels.functions.javaScriptIncludeTag.type#" />
<cfargument name="bundle" type="string" required="false" default="" />
<cfreturn $callOriginalIncludeMethod($includeMethod="javaScriptIncludeTag", $fileType="js", argumentCollection=arguments) />
</cffunction>
<cffunction name="$callOriginalIncludeMethod" access="public" output="false" returntype="string" mixin="controller">
<cfargument name="$includeMethod" type="string" required="true" />
<cfargument name="$fileType" type="string" required="true" />
<cfargument name="sources" type="string" required="false" default="" />
<cfargument name="bundle" type="string" required="true" />
<cfargument name="type" type="string" required="true" />
<cfscript>
var originalIncludeMethod = core[arguments.$includeMethod];
if (not ListFindNoCase(application.wheels.plugins.assetBundler.environments, application.wheels.environment))
{
if (not Len(arguments.sources) and $bundleExists(bundle=arguments.bundle, type=arguments.$fileType))
arguments.sources = application.assetBundler[arguments.$fileType][arguments.bundle].sources;
StructDelete(arguments, "bundle");
return originalIncludeMethod(argumentCollection=arguments);
}
if (not Len(arguments.bundle) or not $bundleExists(bundle=arguments.bundle, type=arguments.$fileType))
{
if (not Len(arguments.sources) and $bundleExists(bundle=arguments.bundle, type=arguments.$fileType))
arguments.sources = application.assetBundler[arguments.$fileType][arguments.bundle].sources;
StructDelete(arguments, "bundle");
return originalIncludeMethod(argumentCollection=arguments);
}
// we are including a bundled item so make sure we have the file path correct
arguments.sources = arguments.bundle & "-" & application.assetBundler[arguments.$fileType][arguments.bundle].md5Hash;
StructDelete(arguments, "$includeMethod");
StructDelete(arguments, "$fileType");
StructDelete(arguments, "bundle");
StructDelete(arguments, "source");
</cfscript>
<cfreturn originalIncludeMethod(argumentCollection=arguments) />
</cffunction>
<cffunction name="$bundleExists" output="false" returntype="boolean" access="public" mixin="controller">
<cfargument name="bundle" required="true" type="string" />
<cfargument name="type" required="true" type="string" hint="can be `js` or `css`" />
<cfscript>
var returnValue = false;
if (not StructKeyExists(application, "assetBundler"))
return returnValue;
if (not StructKeyExists(application.assetBundler, arguments.type))
return returnValue;
if (not StructKeyExists(application.assetBundler[arguments.type], arguments.bundle))
return returnValue;
returnValue = true;
</cfscript>
<cfreturn returnValue />
</cffunction>
<cffunction name="$compressContents" access="public" output="false" returntype="string" mixin="application">
<cfargument name="fileContents" type="string" required="true" />
<cfargument name="type" type="string" required="true" />
<cfscript>
var loc = {};
loc.javaLoader = $createJavaLoader();
loc.stringReader = createObject("java", "java.io.StringReader").init(arguments.fileContents);
loc.stringWriter = createObject("java", "java.io.StringWriter").init();
if (LCase(arguments.type) == "css")
{
loc.yuiCompressor = loc.javaLoader.create("com.yahoo.platform.yui.compressor.CssCompressor").init(loc.stringReader);
loc.yuiCompressor.compress(loc.stringWriter, JavaCast("int", -1));
}
else if (LCase(arguments.type) == "js")
{
loc.errorReporter = loc.javaLoader.create("org.mozilla.javascript.tools.ToolErrorReporter").init(JavaCast("boolean", false));
loc.yuiCompressor = loc.javaLoader.create("com.yahoo.platform.yui.compressor.JavaScriptCompressor").init(loc.stringReader, loc.errorReporter);
loc.yuiCompressor.compress(loc.stringWriter, JavaCast("int", -1), JavaCast("boolean", true), JavaCast("boolean", false), JavaCast("boolean", false), JavaCast("boolean", false));
}
else
{
return arguments.fileContents;
}
loc.stringReader.close();
loc.compressedContents = loc.stringWriter.toString();
loc.stringWriter.close();
return loc.compressedContents;
</cfscript>
</cffunction>
<cffunction name="$createJavaLoader" access="public" output="false" returntype="any" mixin="application">
<cfscript>
var loc = {};
if (!StructKeyExists(server, "javaloader") || !IsStruct(server.javaloader))
server.javaloader = {};
if (StructKeyExists(server.javaloader, "assetbundler"))
return server.javaloader.assetbundler;
loc.relativePluginPath = application.wheels.webPath & application.wheels.pluginPath & "/assetbundler/";
loc.classPath = Replace(Replace(loc.relativePluginPath, "/", ".", "all") & "javaloader", ".", "", "one");
loc.paths = ArrayNew(1);
loc.paths[1] = ExpandPath(loc.relativePluginPath & "lib/yuicompressor-2.4.7.jar");
// set the javaLoader to the request in case we use it again
server.javaloader.assetbundler = $createObjectFromRoot(path=loc.classPath, fileName="JavaLoader", method="init", loadPaths=loc.paths, loadColdFusionClassPath=false);
</cfscript>
<cfreturn server.javaloader.assetbundler />
</cffunction>
<cffunction name="$getFileContents" access="public" output="false" returntype="string" mixin="application">
<cfargument name="fileNames" type="string" required="true" />
<cfargument name="relativeFolderPath" type="string" required="true" />
<cfargument name="extension" type="string" required="true" />
<cfargument name="delimiter" type="string" required="false" default="," />
<cfscript>
var loc = { fileContents = "" };
loc.array = ListToArray(arguments.fileNames, arguments.delimiter);
for (loc.i = 1; loc.i <= ArrayLen(loc.array); loc.i = loc.i + 1)
{
loc.itemRelativePath = arguments.relativeFolderPath & Trim(loc.array[loc.i]);
if (Reverse(arguments.extension) neq Left(Reverse(loc.itemRelativePath), Len(arguments.extension)))
loc.itemRelativePath = loc.itemRelativePath & arguments.extension;
loc.itemFilePath = ExpandPath(loc.itemRelativePath);
if (!FileExists(loc.itemFilePath))
$throw(type="Wheels.AssetFileNotFound", message="Could not find the file '#loc.itemRelativePath#'.", extendedInfo="Create a file named '#loc.array[loc.i]##arguments.extension#' in the '#arguments.relativeFolderPath#' directory (create the directory as well if it doesn't already exist).");
// get each of our files and concantenate them together
loc.file = FileRead(loc.itemFilePath);
loc.fileContents = loc.fileContents & loc.file;
}
return loc.fileContents;
</cfscript>
</cffunction>
<cffunction name="$getAllFilesInDirectory" access="public" output="false" returntype="string" mixin="application">
<cfargument name="directoryPath" type="string" required="true" />
<cfargument name="relativeFolderPath" type="string" required="true" />
<cfargument name="extension" type="string" required="true" />
<cfargument name="delimiter" type="string" required="false" default="," />
<cfscript>
var loc = { fileNames = "" };
loc.itemFolderPath = ExpandPath(arguments.relativeFolderPath & Trim(arguments.directoryPath));
loc.filesQuery = $directory(action="list", directory=loc.itemFolderPath, type="file", filter="*#arguments.extension#", recurse=true);
for (loc.i = 1; loc.i lte loc.filesQuery.Recordcount; loc.i++)
{
loc.relativePath = ListLast(ReplaceNoCase(Replace(loc.filesQuery.directory[loc.i], "\", "/", "all") & "/" & loc.filesQuery.name[loc.i], arguments.relativeFolderPath, "|", "all"), "|");
loc.fileNames = ListAppend(loc.fileNames, loc.relativePath, arguments.delimiter);
}
</cfscript>
<cfreturn loc.fileNames />
</cffunction>
</cfcomponent>