source: Dev/branches/rest-dojo-ui/client/util/buildscripts/jslib/buildUtilXd.js @ 257

Last change on this file since 257 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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