Debian Codegraph @

util/build/transforms/writeAmd.js

raw symbols SCIP
1 define([
2 "../buildControl",
3 "../fileUtils",
4 "../fs",
5 "dojo/_base/lang",
6 "dojo/json"
7 ], function(bc, fileUtils, fs, lang, json){
8 var
9 computingLayers
10 // the set of layers being computed; use this to detect circular layer dependencies
11 = {},
12
13 computeLayerContents = function(
14 layerModule,
15 include,
16 exclude
17 ){
18 // add property layerSet (a set of mid) to layerModule that...
19 //
20 // * includes the layerModule itself
21 // * includes dependency tree of layerModule
22 // * includes all modules in layerInclude and their dependency trees
23 // * excludes all modules in layerExclude and their dependency trees
24 //
25 // note: layerSet is built exactly as given above, so included modules that are later excluded
26 // are *not* in result layerSet
27
28 if(computingLayers[layerModule.mid]){
29 bc.log("amdCircularDependency", ["module", layerModule.mid]);
30 return {};
31 }
32 computingLayers[layerModule.mid] = 1;
33
34 var
35 includeSet = {},
36 visited,
37 includePhase,
38 traverse = function(module){
39 var mid = module.mid;
40
41 if(visited[mid]){
42 return;
43 }
44 visited[mid] = 1;
45 if(includePhase){
46 includeSet[mid] = module;
47 }else{
48 delete includeSet[mid];
49 }
50 if(module!==layerModule && module.layer){
51 var layerModuleSet = module.moduleSet || computeLayerContents(module, module.layer.include, module.layer.exclude);
52 for(var p in layerModuleSet){
53 if(includePhase){
54 includeSet[p] = layerModuleSet[p];
55 }else{
56 delete includeSet[p];
57 }
58 }
59 }else{
60 for(var deps = module.deps, i = 0; deps && i<deps.length; traverse(deps[i++])){
61 }
62 }
63 };
64
65 visited = {};
66 includePhase = true;
67 traverse(layerModule);
68 include.forEach(function(mid){
69 var module = bc.amdResources[bc.getSrcModuleInfo(mid, layerModule).mid];
70 if(!module){
71 bc.log("amdMissingLayerIncludeModule", ["missing", mid, "layer", layerModule.mid]);
72 }else{
73 traverse(module);
74 }
75 });
76
77 visited = {};
78 includePhase = false;
79 exclude.forEach(function(mid){
80 var module = bc.amdResources[bc.getSrcModuleInfo(mid, layerModule).mid];
81 if(!module){
82 bc.log("amdMissingLayerExcludeModule", ["missing", mid, "layer", layerModule.mid]);
83 }else{
84 traverse(module);
85 }
86 });
87
88 layerModule.moduleSet = includeSet;
89 delete computingLayers[layerModule.mid];
90
91 // return a copy so that clients can not mutate layerModule.moduleSet
92 var result = {};
93 for(var p in includeSet){
94 result[p] = includeSet[p];
95 }
96 return result;
97 },
98
99 insertAbsMid = function(
100 text,
101 resource
102 ){
103 return (!resource.mid || resource.tag.hasAbsMid || !bc.insertAbsMids) ?
104 text : text.replace(/(define\s*\(\s*)(.*)/, "$1\"" + resource.mid + "\", $2");
105 },
106
107 pushString = function(
108 strings,
109 pair
110 ){
111 strings[pair[0]] = pair[1];
112 },
113
114 appendStringsToCache = function(
115 strings,
116 cache
117 ) {
118 for(var p in strings){
119 cache.push("'" + p + "':" + strings[p])
120 }
121 },
122
123 getPreloadL10nRootPath = function(
124 dest
125 ){
126 var match = dest.match(/(.+)\/([^\/]+)$/);
127 return match[1] + "/nls/" + match[2];
128 },
129
130 flattenRootBundle = function(resource){
131 if(resource.flattenedBundles){
132 return;
133 }
134 resource.flattenedBundles = {};
135 var scheduledToFlatten = {};
136 bc.localeList.forEach(function(locale){
137 scheduledToFlatten[locale] = 1;
138 });
139 bc.localeList.forEach(function(locale){
140 var accumulator = lang.mixin({}, resource.bundleValue.root);
141 if(!bc.localeList.discreteLocales[locale]){
142 console.log(resource.mid, locale);
143 }else{
144 bc.localeList.discreteLocales[locale].forEach(function(discreteLocale){
145 var localizedBundle = resource.localizedSet[discreteLocale];
146 if(localizedBundle && localizedBundle.bundleValue){
147 lang.mixin(accumulator, localizedBundle.bundleValue);
148 }
149 });
150
151 var set = {};
152 if(locale === "ROOT"){
153 for(var p in resource.localizedSet) set[p] = 1;
154 }else{
155 for(var p in resource.localizedSet) if(!scheduledToFlatten[p] && p.indexOf(locale) == 0 && p.length > locale.length){
156 // p is a more-specific version of locale and it is not scheduled to be flattened itself
157 set[p] = 1;
158 }
159 }
160 accumulator._localized = set;
161 }
162 resource.flattenedBundles[locale] = accumulator;
163 }
164 )
165 ;
166 },
167
168 getFlattenedBundles = function(
169 resource,
170 rootBundles
171 ){
172 rootBundles.forEach(flattenRootBundle);
173
174 var newline = bc.newline,
175 rootPath = getPreloadL10nRootPath(resource.dest.match(/(.+)(\.js)$/)[1]),
176 mid, cache;
177 bc.localeList.forEach(function(locale){
178 cache = [];
179 rootBundles.forEach(function(rootResource){
180 cache.push("'" + rootResource.prefix + rootResource.bundle + "':" + json.stringify(rootResource.flattenedBundles[locale]) + newline);
181 });
182 mid = getPreloadL10nRootPath(resource.mid) + "_" + locale;
183 var flattenedResource = {
184 src:"*synthetic*",
185 dest:rootPath + "_" + locale + ".js",
186 pid:resource.pid,
187 mid:mid,
188 pack:resource.pack,
189 deps:[],
190 tag:{flattenedNlsBundle:1},
191 encoding:'utf8',
192 text:"define(" + (bc.insertAbsMids ? "'" + mid + "',{" : "{") + newline + cache.join("," + newline) + "});",
193 getText:function(){ return this.text; }
194 };
195 if(bc.insertAbsMids){
196 flattenedResource.tag.hasAbsMid = 1;
197 }
198 bc.start(flattenedResource);
199 });
200 },
201
202 getLayerText = function(
203 resource,
204 resourceText // ===undefined (normal case) => AMD define() the resource at the end of the layer
205 // ===false => put the resource in the cache
206 // otherwise write the value of resourceText at the end of the layer (so far only used in the writeDojo transform)
207 ){
208 var newline = bc.newline,
209 rootBundles = [],
210 strings = {},
211 cache = [],
212 layer = resource.layer,
213 moduleSet = computeLayerContents(resource, layer.include, layer.exclude),
214 includeLocales = "includeLocales" in layer ? layer.includeLocales : bc.includeLocales;
215 for(var p in moduleSet){
216 // always put modules!=resource in the cache; put resource in the cache if it's a boot layer and an explicit resourceText wasn't given
217 if(p!=resource.mid || resourceText===false){
218 var module = moduleSet[p];
219 if(module.localizedSet && bc.localeList){
220 // this is a root NLS bundle and the profile is building flattened layer bundles;
221 // therefore, add this bundle to the set to be flattened, but don't write the root bundle
222 // to the cache since the loader will explicitly load the flattened bundle
223 rootBundles.push(module);
224 if(includeLocales){
225 // include the ROOT always
226 cache.push("'" + p + "':function(){" + newline + module.getText() + newline + "}");
227 // now include each locale in the layer
228 includeLocales.forEach(function(locale){
229 var parts = locale.split("-");
230 for(var i = parts.length; i > 0; i--){
231 var localizedSet = module.localizedSet[parts.slice(0, i).join("-")];
232 // see if the localized set is there
233 if(localizedSet){
234 // put the bundle in the cache
235 cache.push("'" + localizedSet.mid + "':function(){" + newline + localizedSet.getText() + newline + "}");
236 }
237 }
238 });
239 }
240 }else if(module.internStrings){
241 pushString(strings, module.internStrings());
242 }else if(module.getText){
243 cache.push("'" + p + "':function(){" + newline + module.getText() + newline + "}");
244 }else{
245 bc.log("amdMissingLayerModuleText", ["module", module.mid, "layer", resource.mid]);
246 }
247 }
248 }
249 appendStringsToCache(strings, cache);
250
251 // compute the flattened layer bundles (if any)
252 if(rootBundles.length){
253 getFlattenedBundles(resource, rootBundles);
254 // push an *now into the cache that causes the flattened layer bundles to be loaded immediately
255 cache.push("'*now':function(r){r(['dojo/i18n!*preload*" + getPreloadL10nRootPath(resource.mid) + "*" +
256 json.stringify(bc.localeList.filter(function(locale){
257 return !includeLocales || (includeLocales.indexOf(locale) == -1 && locale != "ROOT");
258 })) + "']);}" + newline);
259 }
260
261 // construct the cache text
262 if(cache.length && resource.layer.noref){
263 cache.push("'*noref':1");
264 }
265 return (cache.length ? "require({cache:{" + newline + cache.join("," + newline) + "}});" + newline : "") +
266 (resourceText===undefined ? insertAbsMid(resource.getText(), resource) : (resourceText==false ? "" : resourceText)) +
267 (resource.layer.postscript ? resource.layer.postscript : "");
268 },
269
270 getStrings = function(
271 resource
272 ){
273 var strings = {},
274 cache = [],
275 newline = bc.newline;
276 resource.deps && resource.deps.forEach(function(dep){
277 if(dep.internStrings){
278 pushString(strings, dep.internStrings());
279 }
280 });
281 appendStringsToCache(strings, cache);
282 return cache.length ? "require({cache:{" + newline + cache.join("," + newline) + "}});" + newline : "";
283 },
284
285 getDestFilename = function(
286 resource
287 ){
288 if((resource.layer && bc.layerOptimize) || (!resource.layer && bc.optimize)){
289 return resource.dest + ".uncompressed.js";
290 }
291 return resource.dest;
292 },
293
294 processNlsBundle = function(
295 resource
296 ){
297 var newline = bc.newline, text, p;
298
299 if(resource.localizedSet && resource.bundleValue){
300 // do a check that all localizations mentioned in the root actually exist
301 var missing = [];
302 for(p in resource.bundleValue){
303 if(p!="root" && resource.bundleValue[p] && !resource.localizedSet[p]){
304 missing.push("'" + p + "'");
305 }
306 }
307 if(missing.length){
308 missing.sort();
309 bc.log("missingL10n", "Root: " + resource.mid + "; missing bundles: " + missing.join(",") + ".");
310 }
311 }
312 if(resource.bundleType=="legacy"){
313 if(resource.bundleValue){
314 if(resource.localizedSet){
315 // this is the root bundle; augment with all available localizations
316 for(p in resource.localizedSet){
317 resource.bundleValue[p] = 1;
318 }
319 }
320 text = json.stringify(resource.bundleValue);
321 }else{
322 text = "// ERROR: builder was unable to evaluate source bundle; therefore, this empty conversion was written" + newline + "{}";
323 }
324 return "define(" + (bc.insertAbsMids ? "'" + resource.mid + "'," : "") + newline + text + newline + ");";
325 }else{
326 return insertAbsMid(resource.getText(), resource);
327 }
328 },
329
330 write = function(
331 resource,
332 callback
333 ){
334 if(resource.layer && (resource.layer.boot || resource.layer.discard)){
335 // resource.layer.boot layers are written by the writeDojo transform
336 return 0;
337 }
338
339 var copyright;
340 if(resource.pack){
341 copyright = resource.pack.copyrightNonlayers && (resource.pack.copyright || bc.copyright);
342 }else{
343 copyright = bc.copyrightNonlayers && bc.copyright;
344 }
345 if(!copyright){
346 copyright = "";
347 }
348
349 var text;
350 if(resource.tag.nls){
351 text = processNlsBundle(resource);
352 }else if(resource.layer){
353 // don't insertAbsMid or internStrings since that's done in getLayerText
354 text= getLayerText(resource);
355 if(resource.layer.compat=="1.6"){
356 text += "require(" + json.stringify(resource.layer.include) + ");" + bc.newline;
357 }
358 copyright = resource.layer.copyright || "";
359 }else{
360 text = insertAbsMid(resource.getText(), resource);
361 if(bc.internStrings){
362 text = getStrings(resource) + text;
363 }
364 }
365
366 // remember the uncompressed text for the optimizer
367 resource.uncompressedText = text;
368
369 var destFilename = getDestFilename(resource);
370 fileUtils.ensureDirectoryByFilename(destFilename);
371 fs.writeFile(destFilename, bc.newlineFilter(text, resource, "writeAmd"), resource.encoding, function(err){
372 callback(resource, err);
373 });
374 return callback;
375 };
376
377 write.getLayerText = getLayerText;
378 write.getDestFilename = getDestFilename;
379 write.computeLayerContents = computeLayerContents;
380
381 return write;
382 });
383