-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (60 loc) · 2.48 KB
/
index.js
File metadata and controls
76 lines (60 loc) · 2.48 KB
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
/*
压缩前分析require-async
*/
'use strict';
var SCRIPT_REG = /<!--(?:(?!\[if [^\]]+\]>)[\s\S])*?-->|(<script[^>]*>)([\s\S]*?)<\/script>/ig;
var REQUIRE_ASYNC_REG = /"(?:[^\\"\r\n\f]|\\[\s\S])*"|'(?:[^\\'\n\r\f]|\\[\s\S])*'|(?:\/\/[^\r\n\f]+|\/\*[\s\S]*?(?:\*\/|$))|require\.async\(([\s\S]+?)(?=,\s*function\(|\))/g, URL_REG = /['"]([^'"]+)['"]/g;
var USE_REQUIRE = feather.config.get('require.use');
var path = require('path');
function toRealPath(content, file){
return content.replace(REQUIRE_ASYNC_REG, function($0, $1){
if($1){
return 'require.async(' + $1.replace(URL_REG, function(all, $1){
if($1){
if(!feather.util.isRemoteUrl($1) && $1[0] != '/'){
var tmpFile = $1[0] == '.' ? new feather.file(path.resolve(file.dirname, $1)) : feather.file.wrap($1);
if(tmpFile.isFile() && tmpFile.exists()){
$1 = tmpFile.subpath;
}
}
file.extras.async.push($1);
return '"' + $1 + '"';
}
return all;
});
}
return $0;
});
}
module.exports = function(content, file, conf){
file.extras.async = [];
if(!USE_REQUIRE) return content;
if(file.isHtmlLike){
content = content.replace(SCRIPT_REG, function($0, $1, $2){
if($2){
return $1 + toRealPath($2, file) + '</script>';
}
return $0;
});
var sameJs = feather.file.wrap(file.id.replace(/\.[^\.]+$/, '.js'));
if(sameJs.exists()){
var url = sameJs.getUrl(feather.compile.settings.hash, feather.compile.settings.domain);
if(file.extras.async.indexOf(sameJs.subpath) == -1
&& file.extras.headJs.indexOf(url) == -1
&& file.extras.bottomJs.indexOf(url) == -1
){
if(/<\/body>/.test(content)){
content = content.replace(/<\/body>/, function(){
return '<script>require.async("' + sameJs.subpath + '");</script></body>';
});
}else{
content += '<script>require.async("' + sameJs.subpath + '");</script>';
}
file.extras.async.push(sameJs.subpath);
}
}
}else if(file.isJsLike){
content = toRealPath(content, file);
}
return content;
};