Debian Codegraph @

util/build/optimizeRunner.js

raw symbols SCIP
1 /*jshint rhino:true white:false */
2 /*global Packages:false com:false */
3 function writeFile(filename, contents, encoding, cb) {
4 if (arguments.length === 3 && typeof encoding !== "string") {
5 cb = encoding;
6 encoding = 0;
7 }
8 var
9 outFile = new java.io.File(filename),
10 outWriter;
11 if(encoding){
12 outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding);
13 }else{
14 outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile));
15 }
16
17 var os = new java.io.BufferedWriter(outWriter);
18 try{
19 os.write(contents);
20 }finally{
21 os.close();
22 }
23 if (cb) {
24 cb(0);
25 }
26 }
27
28 var built = "//>>built\n";
29
30 function sscompile(src, dest, optimizeSwitch, copyright){
31 // decode the optimize switch
32 var
33 options = optimizeSwitch.split("."),
34 comments = 0,
35 keepLines = 0,
36 strip = null;
37 while(options.length){
38 switch(options.pop()){
39 case "normal":
40 strip = "normal";
41 break;
42 case "warn":
43 strip = "warn";
44 break;
45 case "all":
46 strip = "all";
47 break;
48 case "keeplines":
49 keepLines = 1;
50 break;
51 case "comments":
52 comments = 1;
53 break;
54 }
55 }
56
57 //Use rhino to help do minifying/compressing.
58 var context = Packages.org.mozilla.javascript.Context.enter(),
59 text;
60 try{
61 // Use the interpreter for interactive input (copied this from Main rhino class).
62 context.setOptimizationLevel(-1);
63
64 text = readFile(src, "utf-8");
65 if(comments){
66 //Strip comments
67 var script = context.compileString(text, dest, 1, null);
68 text = new String(context.decompileScript(script, 0));
69
70 //Replace the spaces with tabs.
71 //Ideally do this in the pretty printer rhino code.
72 text = text.replace(/ /g, "\t");
73 }else{
74 //Apply compression using custom compression call in Dojo-modified rhino.
75 text = new String(Packages.org.dojotoolkit.shrinksafe.Compressor.compressScript(text, 0, 1, strip));
76 if(!keepLines){
77 text = text.replace(/[\r\n]/g, "");
78 }
79 }
80 }finally{
81 Packages.org.mozilla.javascript.Context.exit();
82 }
83 writeFile(dest, copyright + built + text, "utf-8");
84 }
85
86 var JSSourceFilefromCode, closurefromCode, compiler, jscomp = 0;
87 function ccompile(src, dest, optimizeSwitch, copyright, optimizeOptions, useSourceMaps){
88 optimizeOptions = optimizeOptions || {};
89 if(!jscomp){
90 JSSourceFilefromCode=java.lang.Class.forName("com.google.javascript.jscomp.SourceFile").getMethod("fromCode", [java.lang.String, java.lang.String]);
91 closurefromCode = function(filename,content){
92 return JSSourceFilefromCode.invoke(null,[filename,content]);
93 };
94 jscomp = com.google.javascript.jscomp;
95 }
96
97 //Fake extern required with 'Symbol' defined
98 var externSourceFile = closurefromCode("fakeextern.js", "function Symbol(description) {}");
99
100 //Set up source input
101 // it is possible dest could have backslashes on windows (particularly with cygwin)
102 var destFilename = dest.match(/^.+[\\\/](.+)$/)[1],
103 jsSourceFile = closurefromCode(destFilename + ".uncompressed.js", String(readFile(src, "utf-8")));
104
105 //Set up options
106 var options = new jscomp.CompilerOptions();
107 var set;
108
109 // Set the closure optimization level, use simple optimizations by default
110 var FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS;
111 if (optimizeOptions.compilationLevel) {
112 switch (optimizeOptions.compilationLevel.toUpperCase()) {
113 case "WHITESPACE_ONLY": FLAG_compilation_level = jscomp.CompilationLevel.WHITESPACE_ONLY;
114 break;
115 case "SIMPLE_OPTIMIZATIONS": FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS;
116 break;
117 case "ADVANCED_OPTIMIZATIONS": FLAG_compilation_level = jscomp.CompilationLevel.ADVANCED_OPTIMIZATIONS;
118 break;
119 }
120 }
121
122 FLAG_compilation_level.setOptionsForCompilationLevel(options);
123 var FLAG_warning_level = jscomp.WarningLevel.DEFAULT;
124 FLAG_warning_level.setOptionsForWarningLevel(options);
125
126 // force this option to false to prevent overly aggressive code elimination (#18919)
127 options.setDeadAssignmentElimination(false);
128 options.setEmitUseStrict(false);
129 options.setStrictModeInput(false);
130 options.setWarningLevel(jscomp.DiagnosticGroups.ES5_STRICT, jscomp.CheckLevel.WARNING);
131
132 for(var k in optimizeOptions){
133 // Skip compilation level option
134 if (k === 'compilationLevel') {
135 continue;
136 }
137
138 // Set functions requiring Java Enum values
139 if (k === 'languageIn') {
140 options.setLanguageIn(jscomp.CompilerOptions.LanguageMode.fromString(optimizeOptions[k].toUpperCase()));
141 continue;
142 }
143
144 if (k === 'languageOut') {
145 options.setLanguageOut(jscomp.CompilerOptions.LanguageMode.fromString(optimizeOptions[k].toUpperCase()));
146 continue;
147 }
148
149 if (k === 'variableRenaming') {
150 switch (optimizeOptions[k].toUpperCase()) {
151 case 'OFF': options.setVariableRenaming(jscomp.VariableRenamingPolicy.OFF);
152 break;
153 case 'LOCAL': options.setVariableRenaming(jscomp.VariableRenamingPolicy.LOCAL);
154 break;
155 case 'ALL': options.setVariableRenaming(jscomp.VariableRenamingPolicy.ALL);
156 break;
157 }
158 continue;
159 }
160
161 if (k === 'propertyRenaming') {
162 switch (optimizeOptions[k].toUpperCase()) {
163 case 'OFF': options.setPropertyRenaming(jscomp.PropertyRenamingPolicy.OFF);
164 break;
165 case 'ALL_UNQUOTED': options.setPropertyRenaming(jscomp.PropertyRenamingPolicy.ALL_UNQUOTED);
166 break;
167 }
168 continue;
169 }
170
171 if (k === 'checkGlobalThisLevel') {
172 switch (optimizeOptions[k].toUpperCase()) {
173 case 'ERROR': options.setCheckGlobalThisLevel(jscomp.CheckLevel.ERROR);
174 break;
175 case 'WARNING': options.setCheckGlobalThisLevel(jscomp.CheckLevel.WARNING);
176 break;
177 case 'OFF': options.setCheckGlobalThisLevel(jscomp.CheckLevel.OFF);
178 break;
179 }
180 continue;
181 }
182
183 if (k === 'uselessCode') {
184 var uselessCode = jscomp.DiagnosticGroups.CHECK_USELESS_CODE;
185 switch (optimizeOptions[k].toUpperCase()) {
186 case 'ERROR': options.setWarningLevel(uselessCode, jscomp.CheckLevel.ERROR);
187 break;
188 case 'WARNING': options.setWarningLevel(uselessCode, jscomp.CheckLevel.WARNING);
189 break;
190 case 'OFF': options.setWarningLevel(uselessCode, jscomp.CheckLevel.OFF);
191 break;
192 }
193 continue;
194 }
195
196 if (k in {
197 'removeUnusedVariables': 1,
198 'inlineVariables': 1,
199 'inlineFunctions': 1
200 }) {
201 set = "set" + k.charAt(0).toUpperCase() + k.substr(1);
202 switch (optimizeOptions[k].toUpperCase()) {
203 case 'ALL': options[set](jscomp.CompilerOptions.Reach.ALL);
204 break;
205 case 'LOCAL_ONLY': options[set](jscomp.CompilerOptions.Reach.LOCAL_ONLY);
206 break;
207 case 'NONE': options[set](jscomp.CompilerOptions.Reach.NONE);
208 break;
209 }
210 continue;
211 }
212
213 // Set functions with boolean values
214 if (k === 'dependencySorting') {
215 options.dependencyOptions.setDependencySorting(optimizeOptions[k]);
216 continue;
217 }
218
219 if (k in {
220 'assumeClosuresOnlyCaptureReferences': 1,
221 'closurePass': 1,
222 'coalesceVariableNames': 1,
223 'collapseAnonymousFunctions': 1,
224 'collapseProperties': 1,
225 'collapseVariableDeclarations': 1,
226 'computeFunctionSideEffects': 1,
227 'crossModuleCodeMotion': 1,
228 'crossModuleMethodMotion': 1,
229 'deadAssignmentElimination': 1,
230 'devirtualizePrototypeMethods': 1,
231 'extractPrototypeMemberDeclarations': 1,
232 'flowSensitiveInlineVariables': 1,
233 'foldConstants': 1,
234 'inlineConstantVars': 1,
235 'optimizeArgumentsArray': 1,
236 'removeDeadCode': 1,
237 'removeUnusedClassProperties': 1,
238 'removeUnusedPrototypeProperties': 1,
239 'removeUnusedPrototypePropertiesInExterns': 1,
240 'rewriteFunctionExpressions': 1,
241 'smartNameRemoval': 1
242 }) {
243 set = 'set' + k.charAt(0).toUpperCase() + k.substr(1);
244 options[set](optimizeOptions[k]);
245 continue;
246 }
247
248 // Otherwise assign value to key
249 options[k] = optimizeOptions[k];
250 }
251 // Must have non-null path to trigger source map generation, also fix version
252 options.setSourceMapOutputPath("");
253 options.setSourceMapFormat(jscomp.SourceMap.Format.V3);
254 if(optimizeSwitch.indexOf(".keeplines") !== -1){
255 options.prettyPrint = true;
256 }
257
258 //Prevent too-verbose logging output
259 Packages.com.google.javascript.jscomp.Compiler.setLoggingLevel(java.util.logging.Level.SEVERE);
260
261 // Run the compiler
262 // File name and associated map name
263 var mapTag = useSourceMaps ? ("\n//# sourceMappingURL=" + destFilename + ".map") : "";
264 compiler = new Packages.com.google.javascript.jscomp.Compiler(Packages.java.lang.System.err);
265
266
267 compiler.compile(externSourceFile, jsSourceFile, options);
268 writeFile(dest, copyright + built + compiler.toSource() + mapTag, "utf-8");
269
270 if(useSourceMaps){
271 var sourceMap = compiler.getSourceMap();
272 sourceMap.setWrapperPrefix(copyright + built);
273 var sb = new java.lang.StringBuffer();
274 sourceMap.appendTo(sb, destFilename);
275 writeFile(dest + ".map", sb.toString(), "utf-8");
276 }
277 }
278
279 function shutdownClosureExecutorService(){
280 try{
281 var compilerClass = compiler.getClass();
282 var compilerExecutorField = compilerClass.getDeclaredMethod("getCompilerExecutor");
283 compilerExecutorField.setAccessible(true);
284 var compilerExecutor = compilerExecutorField.invoke(compiler);
285 compilerClass = compilerExecutor.getClass();
286 compilerExecutorField = compilerClass.getDeclaredMethod("getExecutorService");
287 compilerExecutorField.setAccessible(true);
288 compilerExecutor = compilerExecutorField.invoke(compilerExecutor);
289 compilerExecutor.shutdown();
290 }catch (e){
291 print(e);
292 if("javaException" in e){
293 e.javaException.printStackTrace();
294 }
295 }
296 }
297
298 var
299 console = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System["in"])),
300 readLine = function(){
301 // the + "" convert to a Javascript string
302 return console.readLine() + "";
303 },
304 src,
305 dest,
306 optimizeSwitch;
307
308 while(1){
309 src = readLine();
310 if(src === "."){
311 break;
312 }
313 dest = readLine();
314 optimizeSwitch = readLine();
315 var options = eval("(" + readLine() + ")");
316 print(dest + ":");
317 var start = (new Date()).getTime(),
318 exception = "";
319 try{
320 if(/closure/.test(optimizeSwitch)){
321 ccompile(src, dest, optimizeSwitch, options.copyright, options.options, options.useSourceMaps);
322 }else{
323 sscompile(src, dest, optimizeSwitch, options.copyright);
324 }
325 }catch(e){
326 exception = ". OPTIMIZER FAILED: " + e;
327 }
328 print("Done (compile time:" + ((new Date()).getTime()-start)/1000 + "s)" + exception);
329 }
330
331 if (jscomp) {
332 shutdownClosureExecutorService();
333 }