1 | define([ |
---|
2 | "../buildControl", |
---|
3 | "../fs", |
---|
4 | "../fileUtils", |
---|
5 | "dojo/_base/lang", |
---|
6 | "dojo/_base/json" |
---|
7 | ], function(bc, fs, fileUtils, lang, json){ |
---|
8 | return function(resource){ |
---|
9 | var |
---|
10 | // Array of MIDs discovered in the declarative resource |
---|
11 | aggregateDeps = [], |
---|
12 | |
---|
13 | // Looks for "data-dojo-type" and "data-dojo-mids" for declarative modules |
---|
14 | interningAutoRequireRegExp = /\sdata-dojo-(?:type|mids)\s*=\s*["']([^"']+\/[^"']+)["']/gi, |
---|
15 | |
---|
16 | // Looks for '<script type="dojo/require">' blocks |
---|
17 | interningDeclarativeRequireRegExp = /<script\s+[^>]*type=["']dojo\/require["'][^>]*>([^<]*)<\/script>/gi, |
---|
18 | |
---|
19 | processAutoRequire = function(){ |
---|
20 | // Parses the resource for any MIDs that might need to be built into the layer |
---|
21 | var mids = [], |
---|
22 | str = resource.text, |
---|
23 | match; |
---|
24 | |
---|
25 | // Run the RegExp over the text |
---|
26 | while(match = interningAutoRequireRegExp.exec(str)){ |
---|
27 | match[1].split(/\s*,\s*/).forEach(function(mid){ |
---|
28 | mids.push(mid); |
---|
29 | }); |
---|
30 | } |
---|
31 | |
---|
32 | return mids; |
---|
33 | }, |
---|
34 | |
---|
35 | processDeclarativeRequire = function(){ |
---|
36 | // Parses the resource for and declarative require script blocks and |
---|
37 | // analyses the bocks for their MIDs to be included in the layer. |
---|
38 | var mids = [], |
---|
39 | str = resource.text, |
---|
40 | match; |
---|
41 | |
---|
42 | while(match = interningDeclarativeRequireRegExp.exec(str)){ |
---|
43 | // Try and convert <script type="dojo/require"> into object |
---|
44 | try{ |
---|
45 | var h = json.fromJson("{" + match[1] + "}"); |
---|
46 | }catch(e){ |
---|
47 | bc.log("declarativeRequireFailed", ["resource", resource.src, "error", e]); |
---|
48 | } |
---|
49 | // Cycle through each key and add module as dependency |
---|
50 | for(var i in h){ |
---|
51 | var mid = h[i]; |
---|
52 | if(typeof mid == "string"){ |
---|
53 | mids.push(mid); |
---|
54 | }else{ |
---|
55 | bc.log("userWarn", ["declarative require has invalid value", "resource", resource.src, "key", i, "value", mid]); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | return mids; |
---|
61 | }; |
---|
62 | |
---|
63 | // Intern the resources strings and identify any mids used in declarative syntax |
---|
64 | aggregateDeps = aggregateDeps.concat(processAutoRequire()); |
---|
65 | |
---|
66 | // Intern the resources strings and identify any declarative require script blocs and parse out the mids |
---|
67 | aggregateDeps = aggregateDeps.concat(processDeclarativeRequire()); |
---|
68 | |
---|
69 | |
---|
70 | // Iterate through the layers, identify those that contain this resource.mid, |
---|
71 | // remove it from the include array and then add this resource's includes |
---|
72 | for(var mid in bc.amdResources){ |
---|
73 | if(bc.amdResources[mid].layer){ // This resource is a layer |
---|
74 | var includes = bc.amdResources[mid].layer.include, |
---|
75 | idx = includes.indexOf(resource.mid); |
---|
76 | // Bitwise operator that returns true if the layer contains this resource |
---|
77 | if(~idx){ |
---|
78 | // Remove this resources mid from the layer's include array |
---|
79 | includes.splice(idx, 1); |
---|
80 | aggregateDeps.forEach(function(dep){ |
---|
81 | // Uniquely add appropriate mids to the layer's include array |
---|
82 | if(!(/^(require|exports|module)$/.test(dep))){ |
---|
83 | if(!~includes.indexOf(dep)){ |
---|
84 | includes.push(dep); |
---|
85 | } |
---|
86 | } |
---|
87 | }); |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | }; |
---|
93 | }); |
---|