-
Notifications
You must be signed in to change notification settings - Fork 24
/
writeSource.js
39 lines (33 loc) · 1.12 KB
/
writeSource.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
/**
* @file 将依赖模块名替换成依赖模块id
* @author youngwind
*/
/**
* 将依赖模块名替换成依赖模块id
* @param {object} module 模块对象
* @returns {string} 替换模块名之后的模块内容字符串
*/
module.exports = function (module) {
let replaces = [];
let source = module.source;
if (!module.requires || !module.requires.length) return source;
module.requires.forEach(requireItem => {
if(!requireItem.nameRange || !requireItem.name || !requireItem.id) return;
let prefix = `/* ${requireItem.name} */`;
replaces.push({
from: requireItem.nameRange[0],
to: requireItem.nameRange[1],
value: prefix + requireItem.id
});
});
// 排序,从后往前地替换模块名,这样才能保证正确替换所有的模块
replaces.sort((a, b) => {
return b.from - a.from;
});
// 逐个替换模块名为模块id
replaces.forEach(replace => {
let target = source.substring(replace.from, replace.to);
source = source.replace(target, replace.value);
});
return source;
};