source: Dev/trunk/src/client/util/build/transforms/dojoPragmas.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 
1define(["../buildControl"], function(bc) {
2        var evalPragma= function(code, kwArgs, fileName) {
3                        return !!eval("(" + code + ")");
4                };
5
6        return function(resource) {
7                if(!bc.applyDojoPragmas){
8                        return;
9                }
10                if(typeof resource.text!="string"){
11                        return;
12                }
13
14                var
15                        foundIndex = -1,
16                        startIndex = 0,
17                        text= resource.text;
18                while((foundIndex = text.indexOf("//>>", startIndex)) != -1){
19                        //Found a conditional. Get the conditional line.
20                        var lineEndIndex = text.indexOf("\n", foundIndex);
21                        if(lineEndIndex == -1){
22                                lineEndIndex = text.length - 1;
23                        }
24
25                        //Increment startIndex past the line so the next conditional search can be done.
26                        startIndex = lineEndIndex + 1;
27
28                        //Break apart the conditional.
29                        var conditionLine = text.substring(foundIndex, lineEndIndex + 1);
30                        var matches = conditionLine.match(/(exclude|include)Start\s*\(\s*["'](\w+)["']\s*,(.*)\)/);
31                        if(matches){
32                                var type = matches[1];
33                                var marker = matches[2];
34                                var condition = matches[3];
35                                var isTrue = false;
36                                //See if the condition is true.
37                                try{
38                                        isTrue = evalPragma(condition, bc, resource.src);
39                                }catch(e){
40                                        bc.log("dojoPragmaEvalFail", ["module", resource.mid, "expression", conditionLine, "error", e]);
41                                        return;
42                                }
43
44                                //Find the endpoint marker.
45                                var endRegExp = new RegExp('\\/\\/\\>\\>\\s*' + type + 'End\\(\\s*[\'"]' + marker + '[\'"]\\s*\\)', "g");
46                                var endMatches = endRegExp.exec(text.substring(startIndex, text.length));
47                                if(endMatches){
48
49                                        var endMarkerIndex = startIndex + endRegExp.lastIndex - endMatches[0].length;
50
51                                        //Find the next line return based on the match position.
52                                        lineEndIndex = text.indexOf("\n", endMarkerIndex);
53                                        if(lineEndIndex == -1){
54                                                lineEndIndex = text.length - 1;
55                                        }
56
57                                        //Should we include the segment?
58                                        var shouldInclude = ((type == "exclude" && !isTrue) || (type == "include" && isTrue));
59
60                                        //Remove the conditional comments, and optionally remove the content inside
61                                        //the conditional comments.
62                                        var startLength = startIndex - foundIndex;
63                                        text = text.substring(0, foundIndex)
64                                                + (shouldInclude ? text.substring(startIndex, endMarkerIndex) : "")
65                                                + text.substring(lineEndIndex + 1, text.length);
66
67                                        //Move startIndex to foundIndex, since that is the new position in the file
68                                        //where we need to look for more conditionals in the next while loop pass.
69                                        startIndex = foundIndex;
70                                }else{
71                                        bc.log("dojoPragmaInvalid", ["module", resource.mid, "expression", conditionLine]);
72                                        return;
73                                }
74                        }else if(/^\/\/>>\s*noBuildResolver\s*$/.test(conditionLine)){
75                                resource.noBuildResolver = 1;
76                        }
77                }
78                resource.text= text;
79        };
80});
Note: See TracBrowser for help on using the repository browser.