-
Notifications
You must be signed in to change notification settings - Fork 18
/
wrap.js
79 lines (60 loc) · 2.38 KB
/
wrap.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
module.exports = function(file, opts) {
// 不是 js 文件不处理。
if (!file.isJsLike || file.isPartial) {
return;
}
var content = file.getContent();
var forceNoWrap = file.wrap === false;
if (!forceNoWrap && file.isMod) {
var deps = '';
if (opts.forwardDeclaration) {
var reqs = opts.skipBuiltinModules ? [] : ['\'require\'', '\'exports\'', '\'module\''];
file.requires.forEach(function(id) {
var dep = fis.uri(id, file.dirname);
if (dep.file) {
if (dep.file.isJsLike) {
reqs.push('\'' + (dep.file.moduleId || dep.file.id) + '\'');
}
} else {
/(\.\w+)$/.test(id) ? (~opts.extList.indexOf(RegExp.$1) ? reqs.push('\'' + id.replace(/\.js$/i, '') + '\'') : '') : reqs.push('\'' + id + '\'');
}
});
deps = ' [' + reqs.join(', ') + '],';
}
var originContent = content;
if (opts.tab) {
content = fis.util.pad(' ', opts.tab) + content.split(/\n|\r\n|\r/g).join('\n' + fis.util.pad(' ', opts.tab));
}
var prefix = 'define(\'' + (file.moduleId || file.id) + '\',' + deps + ' function(require, exports, module) {\n\n';
var affix = '\n\n});\n';
content = prefix + content + affix;
if (file.moduleId !== file.id) {
file.extras.moduleId = file.moduleId;
}
// 同时修改 sourcemap 文件内容。
var derived = file.derived;
if (!derived || !derived.length) {
derived = file.extras && file.extras.derived;
}
if (opts.sourceMap && derived && derived[0] && derived[0].rExt === '.map') {
try {
var SourceMap = require('source-map');
var sourcemap = derived[0];
var json = JSON.parse(sourcemap.getContent());
var smc = new SourceMap.SourceMapConsumer(json);
var sourceNode = new SourceMap.SourceNode();
sourceNode.add(prefix);
sourceNode.add(SourceMap.SourceNode.fromStringWithSourceMap(originContent, smc));
sourceNode.add(affix);
var code_map = sourceNode.toStringWithSourceMap({
file: smc.file
});
var generater = SourceMap.SourceMapGenerator.fromSourceMap(new SourceMap.SourceMapConsumer(code_map.map.toJSON()));
sourcemap.setContent(generater.toString());
} catch (e) {
fis.log.warn('SourceMap Merge Error: %s\n%s', e.message, e.stack);
}
}
file.setContent(content);
}
}