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

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

Added Dojo 1.9.3 release.

File size: 2.6 KB
Line 
1/*jshint white:false */
2define([
3        "../../buildControl",
4        "dojo/has!host-node?./sendJob:",
5        "../../fs",
6        "dojo/has"
7], function(bc, sendJob, fs, has){
8        if(has("host-node")){
9                return function(resource, text, copyright, optimizeSwitch/*, callback*/){
10                        copyright = copyright || "";
11                        sendJob(resource.dest + ".uncompressed.js", resource.dest, optimizeSwitch, copyright);
12                        return 0;
13                };
14        }
15
16        if(has("host-rhino")){
17                var sscompile = function(text, dest, optimizeSwitch, copyright){
18                        /*jshint rhino:true */
19                        /*global Packages:false */
20                        // decode the optimize switch
21                        var
22                                options = optimizeSwitch.split("."),
23                                comments = 0,
24                                keepLines = 0,
25                                strip = null;
26                        while(options.length){
27                                switch(options.pop()){
28                                case "normal":
29                                        strip = "normal";
30                                        break;
31                                case "warn":
32                                        strip = "warn";
33                                        break;
34                                case "all":
35                                        strip = "all";
36                                        break;
37                                case "keeplines":
38                                        keepLines = 1;
39                                        break;
40                                case "comments":
41                                        comments = 1;
42                                        break;
43                                }
44                        }
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                                if(comments){
53                                        //Strip comments
54                                        var script = context.compileString(text, dest, 1, null);
55                                        text = new String(context.decompileScript(script, 0));
56
57                                        //Replace the spaces with tabs.
58                                        //Ideally do this in the pretty printer rhino code.
59                                        text = text.replace(/    /g, "\t");
60                                }else{
61                                        //Apply compression using custom compression call in Dojo-modified rhino.
62                                        text = new String(Packages.org.dojotoolkit.shrinksafe.Compressor.compressScript(text, 0, 1, strip));
63                                        if(!keepLines){
64                                                text = text.replace(/[\r\n]/g, "");
65                                        }
66                                }
67                        }finally{
68                                Packages.org.mozilla.javascript.Context.exit();
69                        }
70                        return copyright + "//>>built" + bc.newline + text;
71                };
72
73                return function(resource, text, copyright, optimizeSwitch, callback){
74                        bc.log("optimize", ["module", resource.mid]);
75                        copyright = copyright || "";
76                        try{
77                                var result = sscompile(text, resource.dest, optimizeSwitch, copyright);
78
79                                fs.writeFile(resource.dest, result, resource.encoding, function(err){
80                                        if(err){
81                                                bc.log("optimizeFailedWrite", ["filename", result.dest]);
82                                        }
83                                        callback(resource, err);
84                                });
85                        }catch(e){
86                                bc.log("optimizeFailed", ["module identifier", resource.mid, "exception", e + ""]);
87                                callback(resource, 0);
88                        }
89                        return callback;
90                };
91        }
92
93        throw new Error("Unknown host environment: only nodejs and rhino are supported by shrinksafe optimizer.");
94});
Note: See TracBrowser for help on using the repository browser.