source: Dev/trunk/src/client/util/build/transforms/optimizer/closure.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.8 KB
Line 
1define([
2        "../../buildControl",
3        "dojo/has!host-node?./sendJob:",
4        "../../fs",
5        "../../fileUtils",
6        "./stripConsole",
7        "dojo/_base/lang",
8        "dojo/has"
9], function(bc, sendJob, fs, fileUtils, stripConsole, lang, has){
10        if(has("host-node")){
11                return function(resource, text, copyright, optimizeSwitch, callback){
12                        copyright = copyright || "";
13                        if(bc.stripConsole){
14                                var tempFilename = resource.dest + ".consoleStripped.js";
15                                text = stripConsole(text);
16                                fs.writeFile(tempFilename, bc.newlineFilter(text, resource, "closureStripConsole"), resource.encoding, function(err){
17                                        if(!err){
18                                                sendJob(tempFilename, resource.dest, optimizeSwitch, copyright);
19                                        }
20                                        callback(resource, err);
21                                });
22                                return callback;
23                        }else{
24                                sendJob(resource.dest + ".uncompressed.js", resource.dest, optimizeSwitch, copyright);
25                                return 0;
26                        }
27                };
28        }
29
30        if(has("host-rhino")){
31                var JSSourceFilefromCode,
32                        closurefromCode,
33                        jscomp = 0,
34                        built = "//>>built" + bc.newline;
35
36                var ccompile = function(text, dest, optimizeSwitch, copyright, useSourceMaps){
37                        /*jshint rhino:true */
38                        /*global com:false Packages:false */
39                        if(!jscomp){
40                                JSSourceFilefromCode = java.lang.Class.forName("com.google.javascript.jscomp.JSSourceFile").getMethod("fromCode", [ java.lang.String, java.lang.String ]);
41                                closurefromCode = function(filename,content){
42                                        return JSSourceFilefromCode.invoke(null, [filename, content]);
43                                };
44                                jscomp = com.google.javascript.jscomp;
45                        }
46                        //Fake extern
47                        var externSourceFile = closurefromCode("fakeextern.js", " ");
48
49                        //Set up source input
50                        var destFilename = dest.split("/").pop(),
51                                jsSourceFile = closurefromCode(destFilename + ".uncompressed.js", String(text));
52
53                        //Set up options
54                        var options = new jscomp.CompilerOptions();
55                        lang.mixin(options, bc.optimizeOptions);
56                        // Must have non-null path to trigger source map generation, also fix version
57                        options.setSourceMapOutputPath("");
58                        options.setSourceMapFormat(jscomp.SourceMap.Format.V3);
59                        if(optimizeSwitch.indexOf(".keeplines") !== -1){
60                                options.prettyPrint = true;
61                        }
62                        var FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS;
63                        FLAG_compilation_level.setOptionsForCompilationLevel(options);
64                        var FLAG_warning_level = jscomp.WarningLevel.DEFAULT;
65                        FLAG_warning_level.setOptionsForWarningLevel(options);
66
67                        //Prevent too-verbose logging output
68                        Packages.com.google.javascript.jscomp.Compiler.setLoggingLevel(java.util.logging.Level.SEVERE);
69
70                        // Run the compiler
71                        // File name and associated map name
72                        var mapTag = useSourceMaps ? (bc.newline + "//# sourceMappingURL=" + destFilename + ".map") : "";
73                        var compiler = new Packages.com.google.javascript.jscomp.Compiler(Packages.java.lang.System.err);
74                        compiler.compile(externSourceFile, jsSourceFile, options);
75                        var result = copyright + built + compiler.toSource() + mapTag;
76
77                        if(useSourceMaps){
78                                var sourceMap = compiler.getSourceMap();
79                                sourceMap.setWrapperPrefix(copyright + built);
80                                var sb = new java.lang.StringBuffer();
81                                sourceMap.appendTo(sb, destFilename);
82                                fs.writeFile(dest + ".map", sb.toString(), "utf-8");
83                        }
84
85                        return result;
86                };
87
88                return function(resource, text, copyright, optimizeSwitch, callback){
89                        bc.log("optimize", ["module", resource.mid]);
90                        copyright = copyright || "";
91                        try{
92                                var result = ccompile(stripConsole(text), resource.dest, optimizeSwitch, copyright, bc.useSourceMaps);
93                                fs.writeFile(resource.dest, result, resource.encoding, function(err){
94                                        if(err){
95                                                bc.log("optimizeFailedWrite", ["filename", result.dest]);
96                                        }
97                                        callback(resource, err);
98                                });
99                        }catch(e){
100                                bc.log("optimizeFailed", ["module identifier", resource.mid, "exception", e + ""]);
101                                callback(resource, 0);
102                        }
103                        return callback;
104                };
105        }
106
107        throw new Error("Unknown host environment: only nodejs and rhino are supported by closure optimizer.");
108});
Note: See TracBrowser for help on using the repository browser.