[483] | 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, jscomp = 0; |
---|
| 87 | function ccompile(src, dest, optimizeSwitch, copyright, optimizeOptions, useSourceMaps){ |
---|
| 88 | if(!jscomp){ |
---|
| 89 | JSSourceFilefromCode=java.lang.Class.forName("com.google.javascript.jscomp.JSSourceFile").getMethod("fromCode", [java.lang.String, java.lang.String]); |
---|
| 90 | closurefromCode = function(filename,content){ |
---|
| 91 | return JSSourceFilefromCode.invoke(null,[filename,content]); |
---|
| 92 | }; |
---|
| 93 | jscomp = com.google.javascript.jscomp; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | //Fake extern |
---|
| 97 | var externSourceFile = closurefromCode("fakeextern.js", " "); |
---|
| 98 | |
---|
| 99 | //Set up source input |
---|
| 100 | // it is possible dest could have backslashes on windows (particularly with cygwin) |
---|
| 101 | var destFilename = dest.match(/^.+[\\\/](.+)$/)[1], |
---|
| 102 | jsSourceFile = closurefromCode(destFilename + ".uncompressed.js", String(readFile(src, "utf-8"))); |
---|
| 103 | |
---|
| 104 | //Set up options |
---|
| 105 | var options = new jscomp.CompilerOptions(); |
---|
| 106 | for(var k in optimizeOptions){ |
---|
| 107 | options[k] = optimizeOptions[k]; |
---|
| 108 | } |
---|
| 109 | // Must have non-null path to trigger source map generation, also fix version |
---|
| 110 | options.setSourceMapOutputPath(""); |
---|
| 111 | options.setSourceMapFormat(jscomp.SourceMap.Format.V3); |
---|
| 112 | if(optimizeSwitch.indexOf(".keeplines") !== -1){ |
---|
| 113 | options.prettyPrint = true; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | var FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS; |
---|
| 117 | FLAG_compilation_level.setOptionsForCompilationLevel(options); |
---|
| 118 | var FLAG_warning_level = jscomp.WarningLevel.DEFAULT; |
---|
| 119 | FLAG_warning_level.setOptionsForWarningLevel(options); |
---|
| 120 | |
---|
| 121 | //Prevent too-verbose logging output |
---|
| 122 | Packages.com.google.javascript.jscomp.Compiler.setLoggingLevel(java.util.logging.Level.SEVERE); |
---|
| 123 | |
---|
| 124 | // Run the compiler |
---|
| 125 | // File name and associated map name |
---|
| 126 | var mapTag = useSourceMaps ? ("\n//# sourceMappingURL=" + destFilename + ".map") : "", |
---|
| 127 | compiler = new Packages.com.google.javascript.jscomp.Compiler(Packages.java.lang.System.err); |
---|
| 128 | compiler.compile(externSourceFile, jsSourceFile, options); |
---|
| 129 | writeFile(dest, copyright + built + compiler.toSource() + mapTag, "utf-8"); |
---|
| 130 | |
---|
| 131 | if(useSourceMaps){ |
---|
| 132 | var sourceMap = compiler.getSourceMap(); |
---|
| 133 | sourceMap.setWrapperPrefix(copyright + built); |
---|
| 134 | var sb = new java.lang.StringBuffer(); |
---|
| 135 | sourceMap.appendTo(sb, destFilename); |
---|
| 136 | writeFile(dest + ".map", sb.toString(), "utf-8"); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | var |
---|
| 142 | console = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System["in"])), |
---|
| 143 | readLine = function(){ |
---|
| 144 | // the + "" convert to a Javascript string |
---|
| 145 | return console.readLine() + ""; |
---|
| 146 | }, |
---|
| 147 | src, |
---|
| 148 | dest, |
---|
| 149 | optimizeSwitch; |
---|
| 150 | |
---|
| 151 | while(1){ |
---|
| 152 | src = readLine(); |
---|
| 153 | if(src === "."){ |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | dest = readLine(); |
---|
| 157 | optimizeSwitch = readLine(); |
---|
| 158 | var options = eval("(" + readLine() + ")"); |
---|
| 159 | print(dest + ":"); |
---|
| 160 | var start = (new Date()).getTime(), |
---|
| 161 | exception = ""; |
---|
| 162 | try{ |
---|
| 163 | if(/closure/.test(optimizeSwitch)){ |
---|
| 164 | ccompile(src, dest, optimizeSwitch, options.copyright, options.options, options.useSourceMaps); |
---|
| 165 | }else{ |
---|
| 166 | sscompile(src, dest, optimizeSwitch, options.copyright); |
---|
| 167 | } |
---|
| 168 | }catch(e){ |
---|
| 169 | exception = ". OPTIMIZER FAILED: " + e; |
---|
| 170 | } |
---|
| 171 | print("Done (compile time:" + ((new Date()).getTime()-start)/1000 + "s)" + exception); |
---|
| 172 | } |
---|