Debian Codegraph @

util/build/transforms/writeDojo.js

raw symbols SCIP
1 define([
2 "../buildControl",
3 "../fileUtils",
4 "../fs",
5 "../stringify",
6 "./writeAmd",
7 "../process",
8 "dojo/json",
9 "dojo/text!./dojoBoot.js"
10 ], function(bc, fileUtils, fs, stringify, writeAmd, process, json, dojoBootText){
11 return function(resource, callback){
12 var
13 getUserConfig = function(){
14 if(!bc.userConfig){
15 return "function(global){ return global.dojoConfig || global.djConfig || global.require || {}; }";
16 }
17 if(typeof bc.userConfig == "string"){
18 return bc.userConfig;
19 }
20 var result = stringify(bc.userConfig);
21 if(result.unsolved){
22 bc.log("configUnresolvedValues");
23 }
24 return result;
25 },
26
27 computeLocation = function(basePath, path){
28 if(path.indexOf(basePath + "/")==0){
29 return "." + path.substring(basePath.length);
30 }
31 var
32 parts = basePath.split("/"),
33 prefix = "";
34 for(var i = parts.length-1; i>=0; i--){
35 prefix+= (prefix ? "/.." : "..");
36 var check = parts.slice(0, i).join("/") + "/";
37 if(path.indexOf(check)==0){
38 return prefix + path.substring(check.length-1);
39 }
40 }
41 return path;
42 },
43
44 getPackage = function(name){
45 // the purpose of this somewhat verbose routine is to write a minimal package object for each
46 // package, yet allow client code to pass extra (i.e., outside the scope of CJS specs) config
47 // information within the package object
48 var destPack = bc.destPackages[name],
49 result = {};
50 result.name = destPack.name;
51 if(destPack.main!="main"){
52 result.main = destPack.main;
53 }
54 // everything relative to the dojo dir
55 // TODO: making everything relative to dojo needs to be optional
56 if(name=="dojo"){
57 result.location = ".";
58 }else{
59 result.location = computeLocation(bc.destBasePath + "/dojo", destPack.location);
60 }
61 var packageDefaultConfig = bc.defaultConfig && bc.defaultConfig.packages && bc.defaultConfig.packages[name];
62 for(var p in packageDefaultConfig){
63 result[p] = packageDefaultConfig[p];
64 }
65 return result;
66 },
67
68 getDefaultConfig = function(){
69 var p, config = {packages:[], hasCache:{}};
70 if(bc.baseUrl){
71 config.baseUrl = bc.baseUrl;
72 }
73 for(p in bc.packages){
74 config.packages.push(getPackage(p));
75 }
76 for(p in bc.defaultConfig){
77 if(p!=="packages"){
78 // per-package default config was handled above
79 config[p] = bc.defaultConfig[p];
80 }
81 }
82 config = stringify(config);
83 if(config.unsolved){
84 bc.log("configUnresolvedValues");
85 }
86 return config;
87 },
88
89 stampVersion = function(text){
90 // summary:
91 // Changes the version number for dojo. Input should be the fileContents
92 // of a file that contains the version number.
93 version = bc.version;
94 if(version){
95 //First, break apart the version string.
96 var verSegments = (version+"").match(/^(\d*)\.?(\d*)\.?(\d*)\.?(.*)$/);
97 var majorValue = verSegments[1] || 0;
98 var minorValue = verSegments[2] || 0;
99 var patchValue = verSegments[3] || 0;
100 var flagValue = verSegments[4] || "";
101 //Do the final version replacement.
102 return text.replace(
103 /major:\s*\d*,\s*minor:\s*\d*,\s*patch:\s*\d*,\s*flag:\s*".*?"\s*,/g,
104 "major: " + majorValue + ", minor: " + minorValue + ", patch: " + patchValue + ", flag: \"" + flagValue + "\","
105 );
106 }else{
107 return text;
108 }
109 },
110
111 waitCount = 1, // matches *1*
112
113 errors = [],
114
115 onWriteComplete = function(err){
116 if(err){
117 errors.push(err);
118 }
119 if(--waitCount==0){
120 callback(resource, errors.length && errors);
121 }
122 },
123
124 doWrite = function(filename, text){
125 fileUtils.ensureDirectoryByFilename(filename);
126 waitCount++;
127 fs.writeFile(filename, bc.newlineFilter(text, resource, "writeDojo"), "utf8", onWriteComplete);
128 };
129
130 // the writeDojo transform...
131 try{
132 var
133 loaderText = resource.getText(),
134
135 // the default application to the loader constructor is replaced with purpose-build user and default config values
136 configText = "(" + getUserConfig() + ", " + getDefaultConfig() + ");",
137
138 // the construction of the layer is slightly different than standard, so don't pass a module to getLayerText
139 dojoLayerText = stampVersion(writeAmd.getLayerText(resource, "")),
140
141 // 1.6 compat chunk
142 dojoLayerCompat = (resource.layer.compat=="1.6" && resource.layer.include.length) ? "require(" + json.stringify(resource.layer.include) + ");" + bc.newline : "";
143
144 // assemble and write the dojo layer
145 resource.uncompressedText = loaderText + configText + dojoLayerText + (bc.dojoBootText || dojoBootText) + dojoLayerCompat;
146 doWrite(writeAmd.getDestFilename(resource), resource.layer.copyright + resource.uncompressedText);
147
148 //write any bootstraps; boots is a vector of resources that have been marked as bootable by the discovery process
149 resource.boots.forEach(function(item){
150 // don't process the dojo layer, which is === resource
151 if(item!==resource){
152 // each item is a hash of include, exclude, boot, bootText
153 var compat = (item.layer.compat=="1.6" && item.layer.include.length) ? "require(" + json.stringify(item.layer.include) + ");" + bc.newline : "";
154 item.uncompressedText= loaderText + configText + dojoLayerText + writeAmd.getLayerText(item, false) + (item.layer.bootText || bc.dojoBootText || dojoBootText) + dojoLayerCompat + compat;
155 doWrite(writeAmd.getDestFilename(item), resource.layer.copyright + item.uncompressedText);
156 }
157 });
158 onWriteComplete(0); // matches *1*
159 }catch(e){
160 if(waitCount){
161 // can't return the error since there are async processes already going
162 errors.push(e);
163 return 0;
164 }else{
165 return e;
166 }
167 }
168 return callback;
169 };
170 });