1 | //The functions in this file assume that buildUtil.js have been loaded. |
---|
2 | var buildUtilXd = {}; |
---|
3 | |
---|
4 | buildUtilXd.setXdDojoConfig = function(/*String*/fileContents, /*String*/url){ |
---|
5 | // summary: |
---|
6 | // sets sets up xdomain loading for a particular URL. |
---|
7 | // fileContents: |
---|
8 | // be a built dojo.js (can be uncompressed or compressed). |
---|
9 | // url: |
---|
10 | // value should be the url to the directory that contains the dojo, |
---|
11 | // dijit and dojox directories. |
---|
12 | // Example: "http://some.domain.com/dojo090" (no ending slash) |
---|
13 | |
---|
14 | //This function will inject some contents after the dojo.registerModulePath() definition. |
---|
15 | //The contents of fileName should have been a dojo.js that includes the contents |
---|
16 | //of loader_xd.js (specify loader=xdomain in the build command). |
---|
17 | |
---|
18 | //This code is not very robust. It will break if dojo.registerModulePath definition |
---|
19 | //changes to anything more advanced. |
---|
20 | var match = fileContents.match(/(dojo\.registerModulePath\s*=\s*function.*\{)/); |
---|
21 | |
---|
22 | //Find the next two } braces and in inject code after that. |
---|
23 | var endIndex = fileContents.indexOf("}", match.index); |
---|
24 | endIndex = fileContents.indexOf("}", endIndex + 1); |
---|
25 | if(fileContents.charAt(endIndex + 1) == ";"){ |
---|
26 | endIndex += 1; |
---|
27 | } |
---|
28 | endIndex +=1; |
---|
29 | |
---|
30 | var lineSeparator = fileUtil.getLineSeparator(); |
---|
31 | return fileContents.substring(0, endIndex) |
---|
32 | + lineSeparator |
---|
33 | + "if(typeof dojo.config[\"useXDomain\"] == \"undefined\"){" |
---|
34 | + "dojo.config.useXDomain = true;};\ndojo.registerModulePath(\"dojo\", \"" |
---|
35 | + url + "/dojo" |
---|
36 | + "\");\ndojo.registerModulePath(\"dijit\", \"" |
---|
37 | + url + "/dijit" |
---|
38 | + "\");\ndojo.registerModulePath(\"dojox\", \"" |
---|
39 | + url + "/dojox" |
---|
40 | + "\");" |
---|
41 | + lineSeparator |
---|
42 | + fileContents.substring(endIndex, fileContents.length); |
---|
43 | } |
---|
44 | |
---|
45 | buildUtilXd.xdgen = function( |
---|
46 | /*String*/prefixName, |
---|
47 | /*String*/prefixPath, |
---|
48 | /*Array*/prefixes, |
---|
49 | /*RegExp*/optimizeIgnoreRegExp, |
---|
50 | /*Object*/kwArgs |
---|
51 | ){ |
---|
52 | // summary: |
---|
53 | // generates the .xd.js files for a build. |
---|
54 | var jsFileNames = fileUtil.getFilteredFileList(prefixPath, /\.js$/, true); |
---|
55 | |
---|
56 | //Construct a regexp to avoid xd generating loader_xd.js, since shrinksafe |
---|
57 | //does not like the resulting javascript that is generated, because of |
---|
58 | //bug http://trac.dojotoolkit.org/ticket/2766 |
---|
59 | var loaderIgnoreRegExp = /dojo\/_base\/_loader\/loader_xd/; |
---|
60 | |
---|
61 | for(var i = 0; i < jsFileNames.length; i++){ |
---|
62 | var jsFileName = jsFileNames[i]; |
---|
63 | |
---|
64 | //Some files, like the layer files, have already been xd |
---|
65 | //processed, so be sure to skip those. |
---|
66 | if(!jsFileName.match(optimizeIgnoreRegExp) && !jsFileName.match(loaderIgnoreRegExp)){ |
---|
67 | var xdFileName = jsFileName.replace(/\.js$/, ".xd.js"); |
---|
68 | var fileContents = fileUtil.readFile(jsFileName); |
---|
69 | |
---|
70 | //Files in nls directories, except for the ones that have multiple |
---|
71 | //bundles flattened (therefore have a dojo.provide call), |
---|
72 | //need to have special xd contents. |
---|
73 | if(jsFileName.match(/\/nls\//) && fileContents.indexOf("dojo.provide(") == -1){ |
---|
74 | var xdContents = buildUtilXd.makeXdBundleContents(prefixName, prefixPath, jsFileName, fileContents, prefixes, kwArgs); |
---|
75 | }else{ |
---|
76 | xdContents = buildUtilXd.makeXdContents(fileContents, prefixes, kwArgs); |
---|
77 | } |
---|
78 | fileUtil.saveUtf8File(xdFileName, xdContents); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | //Function that generates the XD version of the module file's contents |
---|
84 | buildUtilXd.makeXdContents = function(fileContents, prefixes, kwArgs){ |
---|
85 | var dependencies = []; |
---|
86 | |
---|
87 | //Use the regexps to find resource dependencies for this module file. |
---|
88 | var depMatches = buildUtil.removeComments(fileContents).match(buildUtil.globalDependencyRegExp); |
---|
89 | if(depMatches){ |
---|
90 | for(var i = 0; i < depMatches.length; i++){ |
---|
91 | var partMatches = depMatches[i].match(buildUtil.dependencyPartsRegExp); |
---|
92 | var depCall = partMatches[1]; |
---|
93 | var depArgs = partMatches[2]; |
---|
94 | |
---|
95 | if(depCall == "requireLocalization"){ |
---|
96 | //Need to find out what locales are available so the dojo loader |
---|
97 | //only has to do one script request for the closest matching locale. |
---|
98 | var reqArgs = i18nUtil.getRequireLocalizationArgsFromString(depArgs); |
---|
99 | if(reqArgs.moduleName){ |
---|
100 | //Find the list of locales supported by looking at the path names. |
---|
101 | var locales = i18nUtil.getLocalesForBundle(reqArgs.moduleName, reqArgs.bundleName, prefixes); |
---|
102 | |
---|
103 | //Add the supported locales to the requireLocalization arguments. |
---|
104 | if(!reqArgs.localeName){ |
---|
105 | depArgs += ", null"; |
---|
106 | } |
---|
107 | |
---|
108 | depCall = "requireLocalization"; |
---|
109 | depArgs += ', "' + locales.join(",") + '"'; |
---|
110 | }else{ |
---|
111 | //Malformed requireLocalization call. Skip it. May be a comment. |
---|
112 | continue; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | dependencies.push('"' + depCall + '", ' + depArgs); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | //Build the xd file contents. |
---|
121 | var xdContentsBuffer = []; |
---|
122 | var scopeArgs = kwArgs.xdScopeArgs || "dojo, dijit, dojox"; |
---|
123 | |
---|
124 | //Start the module function wrapper. |
---|
125 | xdContentsBuffer.push((kwArgs.xdDojoScopeName || "dojo") + "._xdResourceLoaded(function(" + scopeArgs + "){\n"); |
---|
126 | |
---|
127 | //See if there are any dojo.loadInit calls |
---|
128 | var loadInitCalls = buildUtilXd.extractLoadInits(fileContents); |
---|
129 | if(loadInitCalls){ |
---|
130 | //Adjust fileContents since extractLoadInits removed something. |
---|
131 | fileContents = loadInitCalls[0]; |
---|
132 | |
---|
133 | //Add any loadInit calls to an array passed _xdResourceLoaded |
---|
134 | for(i = 1; i < loadInitCalls.length; i++){ |
---|
135 | xdContentsBuffer.push(loadInitCalls[i] + ";\n"); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | xdContentsBuffer.push("return {"); |
---|
140 | |
---|
141 | //Add in dependencies section. |
---|
142 | if(dependencies.length > 0){ |
---|
143 | xdContentsBuffer.push("depends: ["); |
---|
144 | for(i = 0; i < dependencies.length; i++){ |
---|
145 | if(i > 0){ |
---|
146 | xdContentsBuffer.push(",\n"); |
---|
147 | } |
---|
148 | xdContentsBuffer.push("[" + dependencies[i] + "]"); |
---|
149 | } |
---|
150 | xdContentsBuffer.push("],"); |
---|
151 | } |
---|
152 | |
---|
153 | //Add the contents of the file inside a function. |
---|
154 | //Pass in module names to allow for multiple versions of modules in a page. |
---|
155 | xdContentsBuffer.push("\ndefineResource: function(" + scopeArgs + "){"); |
---|
156 | //Remove requireLocalization calls, since that will mess things up. |
---|
157 | //String() part is needed since fileContents is a Java object. |
---|
158 | xdContentsBuffer.push(String(fileContents).replace(/dojo\.(requireLocalization|i18n\._preloadLocalizations)\([^\)]*\)/g, "")); |
---|
159 | xdContentsBuffer.push("\n}};});"); |
---|
160 | |
---|
161 | return xdContentsBuffer.join(""); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | buildUtilXd.makeXdBundleContents = function(prefix, prefixPath, srcFileName, fileContents, prefixes, kwArgs){ |
---|
166 | //logger.info("Flattening bundle: " + srcFileName); |
---|
167 | |
---|
168 | var bundleParts = i18nUtil.getBundlePartsFromFileName(prefix, prefixPath, srcFileName); |
---|
169 | if(!bundleParts){ |
---|
170 | return null; |
---|
171 | } |
---|
172 | var moduleName = bundleParts.moduleName; |
---|
173 | var bundleName = bundleParts.bundleName; |
---|
174 | var localeName = bundleParts.localeName; |
---|
175 | |
---|
176 | //logger.trace("## moduleName: " + moduleName + ", bundleName: " + bundleName + ", localeName: " + localeName); |
---|
177 | |
---|
178 | //If this is a dojo bundle, it will have already been flattened via the normal build process. |
---|
179 | //If it is an external bundle, then we didn't flatten it during the normal build process since |
---|
180 | //right now, we don't make copies of the external module source files. Need to figure that out at some |
---|
181 | //point, but for now, need to get flattened contents for external modules. |
---|
182 | fileContents = (prefix.indexOf("dojo") == 0) ? fileContents : i18nUtil.makeFlatBundleContents(prefix, prefixPath, srcFileName); |
---|
183 | |
---|
184 | //Final XD file contents. |
---|
185 | fileContents = 'dojo.provide("' + moduleName + '.nls.' + (localeName ? localeName + '.' : '') + bundleName + '");' |
---|
186 | + 'dojo._xdLoadFlattenedBundle("' + moduleName + '", "' + bundleName |
---|
187 | + '", "' + localeName + '", ' + fileContents + ');'; |
---|
188 | |
---|
189 | //Now make a proper xd.js file out of the content. |
---|
190 | return buildUtilXd.makeXdContents(fileContents, prefixes, kwArgs); |
---|
191 | } |
---|
192 | |
---|
193 | buildUtilXd.loadInitRegExp = /dojo\.loadInit\s*\(/g; |
---|
194 | buildUtilXd.extractLoadInits = function(/*String*/fileContents){ |
---|
195 | return buildUtil.extractMatchedParens(buildUtilXd.loadInitRegExp, fileContents); |
---|
196 | } |
---|