-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (81 loc) · 2.5 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
86
87
88
/**
* lego mmodule hook
* @author jerojiang
* @link http://lego.imweb.io/
*/
var lookup = require('./lookup'),
powerLego = require('./powerLego'),
opts = null; // running context opts
var entry = module.exports = function(fis, _opts) {
// set opts
opts = _opts;
fis.on('lookup:file', onLookupFile);
opts.hookHtml && fis.on('proccess:start', onProccessStart);
};
function onLookupFile(info, file) {
// 不处理相对路径的文件
// 不能跳过已经有info.id的文件,必须处理
// 例如: abc.js中require('abc') CommonJs的lookup会找到自己,
// 并不一定需要require('./abc'), 此时如果是想要lego_modules下的abc就惨了
if (file.isJsLike && info.rest && info.rest[0] !== '.') {
var ret = lookup(info.rest, opts);
if (ret && ret.file) {
info.id = ret.file.getId();
info.moduleId = ret.file.moduleId;
info.file = ret.file;
}
} else if (opts.powerLego && info.rest && powerLego.reg.test(info.rest) && !info.id) {
// 处理inline和样式
var ret = powerLego.lookup(info.rest, opts);
if (ret && ret.file) {
info.id = ret.file.getId();
info.file = ret.file;
}
}
}
function onProccessStart(file) {
// 处理html中的链接
// fis isPartial标识是否是内嵌部分
if (!file.isPartial && file._likes.isHtmlLike) {
var content = file.getContent();
if (content) {
content = content.replace(
/(<script\s[^>]*src=["'])([^?#"']+)/g,
function(str, before, src) {
if (src[0] !== '.' && !src.match(/^\w+:/i)) {
var ret = lookup(src, opts);
if (ret && ret.file) {
return before + ret.file.getId().replace(/^\/?/, '/');
}
}
return str;
}
);
file.setContent(content);
}
}
}
entry.defaultOptions = {
/**
* @type {Boolean}
* 是否处理html script的src <script src="">
*/
hookHtml: false,
/**
* @type {Object}
* 查找路径及类型
*/
paths: [
{
location: 'modules',
// modules/mod.js
// modules/mod/mod.js
type: 'mod'
},
{
location: 'lego_modules',
// lego_modules/pkgName/version/subFile
type: 'lego'
}
]
};