-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (67 loc) · 2.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var Resource = require('feather2-resource');
function isEmpty(obj){
for(var i in obj){
return false;
}
return true;
}
function joinSrc(info, pagelet){
var head = '', bottom = '';
info.threeUrls.css.forEach(function(link){
head += '<link rel="stylesheet" href="' + link + '" type="text/css" />';
});
info.threeUrls.headJs.forEach(function(script){
head += '<script src="' + script + '"></script>';
});
if(info.requires && pagelet){
head += '<script>require.config(' + JSON.stringify(info.requires) + ')</script>';
}
info.threeUrls.bottomJs.forEach(function(script){
bottom += '<script src="' + script + '"></script>';
});
return {
head: head,
bottom: bottom
}
}
module.exports = function(ret){
var options = {}, autoPack = feather.config.get('autoPack.type');
if(autoPack == 'combo'){
options.combo = feather.config.get('autoPack.options');
}
var ResourceObject = new Resource(ret.map, options);
feather.util.map(ret.src, function(subpath, file){
if(file.isHtmlLike){
var id = file.id;
var info = ResourceObject.getResourceInfo(id), content = file.getContent();
if(!isEmpty(info.requires.map) && !file.isPagelet){
var mapFile = feather.file.wrap(feather.project.getProjectPath() + '/static/m_/' + feather.util.md5(subpath, 7) + '.js');
mapFile.setContent('require.config(' + JSON.stringify(info.requires) + ');');
ret.pkg[mapFile.subpath] = mapFile;
info.threeUrls.headJs.push(mapFile.getUrl());
delete info.requires;
}
var srcs = joinSrc(info, file.isPagelet);
if(/<!--(?:FEATHER )?STATIC POSITION:HEAD-->|<\/head>/i.test(content)){
content = content.replace(/<!--(?:FEATHER )?STATIC POSITION:HEAD-->|(<\/head>)/i, function(all, tag){
return srcs.head + (tag || '');
});
}else{
content = srcs.head + content;
}
if(/<!--(?:FEATHER )?STATIC POSITION:BOTTOM-->|<\/body>/i.test(content)){
content = content.replace(/<!--(?:FEATHER )?STATIC POSITION:BOTTOM-->|(<\/body>)/i, function(all, tag){
return srcs.bottom + (tag || '');
});
}else{
content = content + srcs.bottom;
}
if(file.isPagelet){
content = content.replace(/\/\*PAGELET_ASYNCS_PLACEHOLDER:[\s\S]+?\*\//, (
JSON.stringify(info.pageletAsyncs) || '').slice(1, -1));
}
file.setContent(content);
ret.pkg[subpath] = file;
}
});
};