| 1 |
define(["../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 |
|
| 20 |
var lineEndIndex = text.indexOf("\n", foundIndex); |
| 21 |
if(lineEndIndex == -1){ |
| 22 |
lineEndIndex = text.length - 1; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
startIndex = lineEndIndex + 1; |
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 52 |
lineEndIndex = text.indexOf("\n", endMarkerIndex); |
| 53 |
if(lineEndIndex == -1){ |
| 54 |
lineEndIndex = text.length - 1; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
var shouldInclude = ((type == "exclude" && !isTrue) || (type == "include" && isTrue)); |
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 68 |
|
| 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 |
}); |