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

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

Added Dojo 1.9.3 release.

File size: 3.2 KB
Line 
1define([
2        "../buildControl",
3        "../fileUtils",
4        "../fs",
5        "../replace"
6], function(bc, fileUtils, fs, replace) {
7        var symctr = 1,
8                m = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
9                len = m.length,
10
11                generateSym = function(name, symtbl){
12                        var ret = name; //defaults to long symbol
13                        if(bc.symbol === "short"){
14                                var s=[], c = symctr;
15                                while(c){
16                                        s.unshift(m[c%len]);
17                                        c = Math.floor(c/len);
18                                }
19                                s = "$D" + s.join('');
20                                symctr++;
21                                symtbl[s + "_"] = name;
22                                ret = s + "_";
23                        }
24                        return ret;
25                },
26
27                convertSym = function(orig_name, symtbl){
28                        var name = orig_name.replace(/\./g, "_");
29                        if(bc.symbol !== "short" && orig_name === name){
30                                if(name === 'define'){
31                                        //if the function is assigned to a variable named define, use
32                                        //DEFINE instead to prevent messing up other transform steps
33                                        name = 'DEFINE';
34                                }
35
36                                //if the original name does not have dot in it, don't use
37                                //the name directly: it will mess up the original logic, such as
38                                //in the following case:
39                                //              if(some_condition){
40                                //                      var my_special_func=function(){...};
41                                //              }else{
42                                //                      var my_special_func=function(){...};
43                                //              }
44                                //if the two anonymous functions are named my_special_func,
45                                //no matter what "some_condition" evaluates to, in the built
46                                //code, my_special_func will always be equal to the
47                                //implementation in the else branch
48                                //so we have to append something random to the name
49                                return name+"__"+Math.floor(Math.random()*10000);
50                        }
51                        return generateSym(name, symtbl);
52                },
53
54                insertSymbols = function(resource, symtbl){
55                        var content = resource.getText(),
56                                prefixes = [],
57                                addFunctionName = function(str, p1, p2, p3, p4){
58                                        return p1+p2+p3+" "+generateSym(prefixes+p2, symtbl)+p4;
59                                };
60
61                        if(resource.pid){
62                                prefixes.push(resource.pid);
63                        }
64                        if(resource.mid){
65                                prefixes.push(resource.mid.replace(/\//g,'_'));
66                        }
67                        if(!prefixes.length){
68                                var m = content.match(/dojo\.provide\("(.*)"\);/);
69                                if(m){
70                                        prefixes.push(m[1].replace(/\./g, "_"));
71                                }
72                        }
73                        if(prefixes.length){
74                                prefixes = prefixes.join('_').replace(/\.|\-/g,'_')+"_";
75                                content = content.replace(/^(\s*)(\w+)(\s*:\s*function)\s*(\(.*)$/mg, addFunctionName).
76                                        replace(/^(\s*this\.)(\w+)(\s*=\s*function)\s*(\(.*)$/mg, addFunctionName);
77                        }
78                        content = content.replace(/^(\s*)([\w\.]+)(\s*=\s*function)\s*(\(.*)/mg,function(str, p1, p2, p3, p4){
79                                return p1+p2+p3+" "+convertSym(p2, symtbl)+p4;
80                        });
81                        return content;
82                },
83
84                warningIssued = 0;
85
86        return function(resource, callback) {
87                if(bc.symbol){
88                        if(resource.tag.report){
89                                if(bc.symbol === 'short'){
90                                        bc.symbolTable = {};
91                                        resource.reports.push({
92                                                dir:".",
93                                                filename:"symboltable.txt",
94                                                content: function(){
95                                                        var symbolText = [], key, symtbl = bc.symbolTable;
96                                                        for(key in symtbl){
97                                                                symbolText.push(key + ": \"" + symtbl[key] + "\"" + bc.newline);
98                                                        }
99                                                        return symbolText.join('');
100                                                }
101                                        });
102                                }
103                        }else{
104                                if(!warningIssued){
105                                        warningIssued = 1;
106                                        bc.log("symbolsLeak", []);
107                                }
108                                fileUtils.ensureDirectoryByFilename(resource.dest);
109                                resource.text = insertSymbols(resource, bc.symbolTable);
110                        }
111                }
112                return 0;
113        };
114});
Note: See TracBrowser for help on using the repository browser.