1 | function load(/*String*/fileName){ |
---|
2 | // summary: |
---|
3 | // opens the file at fileName and evals the contents as JavaScript. |
---|
4 | |
---|
5 | //Read the file |
---|
6 | var fileContents = readFile(fileName); |
---|
7 | |
---|
8 | //Eval the contents. |
---|
9 | var Context = Packages.org.mozilla.javascript.Context; |
---|
10 | var context = Context.enter(); |
---|
11 | try{ |
---|
12 | return context.evaluateString(this, fileContents, fileName, 1, null); |
---|
13 | }finally{ |
---|
14 | Context.exit(); |
---|
15 | } |
---|
16 | } |
---|
17 | |
---|
18 | function readFile(/*String*/path, /*String?*/encoding){ |
---|
19 | // summary: |
---|
20 | // reads a file and returns a string |
---|
21 | encoding = encoding || "utf-8"; |
---|
22 | var file = new java.io.File(path); |
---|
23 | var lineSeparator = "\n"; |
---|
24 | var input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)); |
---|
25 | try { |
---|
26 | var stringBuffer = new java.lang.StringBuffer(); |
---|
27 | var line = ""; |
---|
28 | while((line = input.readLine()) !== null){ |
---|
29 | stringBuffer.append(line); |
---|
30 | stringBuffer.append(lineSeparator); |
---|
31 | } |
---|
32 | //Make sure we return a JavaScript string and not a Java string. |
---|
33 | return new String(stringBuffer.toString()); //String |
---|
34 | } finally { |
---|
35 | input.close(); |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | //TODO: inlining this function since the new shrinksafe.jar is used, and older |
---|
40 | //versions of Dojo's buildscripts are not compatible. |
---|
41 | function optimizeJs(/*String fileName*/fileName, /*String*/fileContents, /*String*/copyright, /*String*/optimizeType, /*String*/stripConsole){ |
---|
42 | // summary: |
---|
43 | // either strips comments from string or compresses it. |
---|
44 | copyright = copyright || ""; |
---|
45 | |
---|
46 | //Use rhino to help do minifying/compressing. |
---|
47 | var context = Packages.org.mozilla.javascript.Context.enter(); |
---|
48 | try{ |
---|
49 | // Use the interpreter for interactive input (copied this from Main rhino class). |
---|
50 | context.setOptimizationLevel(-1); |
---|
51 | |
---|
52 | // the "packer" type is now just a synonym for shrinksafe |
---|
53 | if(optimizeType.indexOf("shrinksafe") == 0 || optimizeType == "packer"){ |
---|
54 | //Apply compression using custom compression call in Dojo-modified rhino. |
---|
55 | fileContents = new String(Packages.org.dojotoolkit.shrinksafe.Compressor.compressScript(fileContents, 0, 1, stripConsole)); |
---|
56 | if(optimizeType.indexOf(".keepLines") == -1){ |
---|
57 | fileContents = fileContents.replace(/[\r\n]/g, ""); |
---|
58 | } |
---|
59 | }else if(optimizeType == "comments"){ |
---|
60 | //Strip comments |
---|
61 | var script = context.compileString(fileContents, fileName, 1, null); |
---|
62 | fileContents = new String(context.decompileScript(script, 0)); |
---|
63 | |
---|
64 | //Replace the spaces with tabs. |
---|
65 | //Ideally do this in the pretty printer rhino code. |
---|
66 | fileContents = fileContents.replace(/ /g, "\t"); |
---|
67 | |
---|
68 | //If this is an nls bundle, make sure it does not end in a ; |
---|
69 | //Otherwise, bad things happen. |
---|
70 | if(fileName.match(/\/nls\//)){ |
---|
71 | fileContents = fileContents.replace(/;\s*$/, ""); |
---|
72 | } |
---|
73 | } |
---|
74 | }finally{ |
---|
75 | Packages.org.mozilla.javascript.Context.exit(); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | return copyright + fileContents; |
---|
80 | } |
---|
81 | |
---|
82 | build = { |
---|
83 | make: function( |
---|
84 | //The path to this file. Assumes dojo builds under it. |
---|
85 | /*String*/builderPath, |
---|
86 | |
---|
87 | //"1.1.1" or "1.3.2": used to choose directory of dojo to use. |
---|
88 | /*String*/version, |
---|
89 | |
---|
90 | //"google" or "aol" |
---|
91 | /*String*/cdnType, |
---|
92 | |
---|
93 | //comma-separated list of resource names. No double-quotes or quotes around values. |
---|
94 | /*String*/dependencies, |
---|
95 | |
---|
96 | //comments, shrinksafe, none |
---|
97 | /*String*/optimizeType){ |
---|
98 | |
---|
99 | |
---|
100 | //Validate. |
---|
101 | if(version != "1.3.2"){ |
---|
102 | return "invalid version"; |
---|
103 | } |
---|
104 | if(cdnType != "google" && cdnType != "aol"){ |
---|
105 | return "invalide CDN type"; |
---|
106 | } |
---|
107 | if(optimizeType != "comments" && optimizeType != "shrinksafe" |
---|
108 | && optimizeType != "none" && optimizeType != "shrinksafe.keepLines"){ |
---|
109 | return "invalid optimize type"; |
---|
110 | } |
---|
111 | if(!dependencies.match(/^[\w\-\,\s\.]+$/)){ |
---|
112 | return "invalid dependency list"; |
---|
113 | } |
---|
114 | |
---|
115 | //Set up full CDN path. |
---|
116 | var xdDojoPath = "http://ajax.googleapis.com/ajax/libs/dojo/"; |
---|
117 | if(cdnType == "aol"){ |
---|
118 | xdDojoPath = "http://o.aolcdn.com/dojo/"; |
---|
119 | } |
---|
120 | xdDojoPath += version; |
---|
121 | |
---|
122 | //Directory that holds dojo source distro. Direct child under the helma dir |
---|
123 | var dojoDir = builderPath + version + "/"; |
---|
124 | |
---|
125 | //Normalize the dependencies so that have double-quotes |
---|
126 | //around each dependency. |
---|
127 | var normalizedDependencies = dependencies || ""; |
---|
128 | if(normalizedDependencies){ |
---|
129 | normalizedDependencies = '"' + normalizedDependencies.split(",").join('","') + '"'; |
---|
130 | } |
---|
131 | |
---|
132 | var buildscriptDir = dojoDir + "util/buildscripts/"; |
---|
133 | |
---|
134 | //Load the libraries to help in the build. |
---|
135 | load(dojoDir + "util/buildscripts/jslib/logger.js"); |
---|
136 | load(dojoDir + "util/buildscripts/jslib/fileUtil.js"); |
---|
137 | load(dojoDir + "util/buildscripts/jslib/buildUtil.js"); |
---|
138 | load(dojoDir + "util/buildscripts/jslib/buildUtilXd.js"); |
---|
139 | load(dojoDir + "util/buildscripts/jslib/i18nUtil.js"); |
---|
140 | |
---|
141 | //Set up the build args. |
---|
142 | var kwArgs = buildUtil.makeBuildOptions([ |
---|
143 | "loader=xdomain", |
---|
144 | "version=" + version, |
---|
145 | "xdDojoPath=" + xdDojoPath, |
---|
146 | "layerOptimize=" + optimizeType |
---|
147 | ]); |
---|
148 | |
---|
149 | //Specify the basic profile for build. |
---|
150 | var profileText = 'dependencies = {' |
---|
151 | + 'layers: [' |
---|
152 | + ' {' |
---|
153 | + ' name: "dojo.js",' |
---|
154 | + ' dependencies: [' |
---|
155 | + normalizedDependencies |
---|
156 | + ' ]' |
---|
157 | + ' }' |
---|
158 | + '],' |
---|
159 | |
---|
160 | + 'prefixes: [' |
---|
161 | + ' [ "dojo", "' + dojoDir + 'dojo" ],' |
---|
162 | + ' [ "dijit", "' + dojoDir + 'dijit" ],' |
---|
163 | + ' [ "dojox", "' + dojoDir + 'dojox" ]' |
---|
164 | + ']' |
---|
165 | + '}'; |
---|
166 | |
---|
167 | //Bring the profile into existence |
---|
168 | var profileProperties = buildUtil.evalProfile(profileText, true); |
---|
169 | kwArgs.profileProperties = profileProperties; |
---|
170 | |
---|
171 | //Set up some helper variables. |
---|
172 | dependencies = kwArgs.profileProperties.dependencies; |
---|
173 | var prefixes = dependencies.prefixes; |
---|
174 | var lineSeparator = fileUtil.getLineSeparator(); |
---|
175 | var layerLegalText = fileUtil.readFile(buildscriptDir + "copyright.txt") |
---|
176 | + lineSeparator |
---|
177 | + fileUtil.readFile(buildscriptDir + "build_notice.txt"); |
---|
178 | |
---|
179 | //Manually set the loader on the dependencies object. Ideally the buildUtil.loadDependencyList() function |
---|
180 | //and subfunctions would take kwArgs directly. |
---|
181 | dependencies.loader = kwArgs.loader; |
---|
182 | |
---|
183 | //Build the layer contents. |
---|
184 | var depResult = buildUtil.makeDojoJs(buildUtil.loadDependencyList(kwArgs.profileProperties, null, buildscriptDir), kwArgs.version, kwArgs); |
---|
185 | |
---|
186 | //Grab the content from the "dojo.xd.js" layer. |
---|
187 | var layerName = depResult[1].layerName; |
---|
188 | var layerContents = depResult[1].contents; |
---|
189 | |
---|
190 | //Burn in xd path for dojo if requested, and only do this in dojo.xd.js. |
---|
191 | if(layerName.match(/dojo\.xd\.js/) && kwArgs.xdDojoPath){ |
---|
192 | layerContents = buildUtilXd.setXdDojoConfig(layerContents, kwArgs.xdDojoPath); |
---|
193 | } |
---|
194 | |
---|
195 | //Intern strings |
---|
196 | if(kwArgs.internStrings){ |
---|
197 | prefixes = dependencies["prefixes"] || []; |
---|
198 | var skiplist = dependencies["internSkipList"] || []; |
---|
199 | layerContents = buildUtil.interningRegexpMagic(layerName, layerContents, dojoDir, prefixes, skiplist); |
---|
200 | } |
---|
201 | |
---|
202 | //Minify the contents |
---|
203 | return optimizeJs(layerName, layerContents, layerLegalText, kwArgs.layerOptimize, ""); |
---|
204 | |
---|
205 | } |
---|
206 | }; |
---|