/
plugin.js
52 lines (44 loc) 路 1.3 KB
/
plugin.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
module.exports = function (babel) {
const { types: t } = babel;
return {
visitor: {
Program(path) {
const { file } = this;
const { sourceFileName } = file.opts.generatorOpts;
// This injects the sourcesContent, though I don't imagine anyone's
// doing it.
file.code = {
[sourceFileName]: file.code,
'bar.js': '<bar />',
'baz.js': 'baz();',
};
},
CallExpression(path) {
const callee = path.node;
const { loc } = callee;
// This filename will cause a second source file to be generated in the
// output sourcemap.
loc.filename = "bar.js";
loc.start.column = 1;
loc.end.column = 4;
const node = t.stringLiteral('bar');
node.loc = loc;
path.replaceWith(node);
},
Function(path) {
const callee = path.node;
const { loc } = callee;
// This filename will cause a second source file to be generated in the
// output sourcemap.
loc.filename = "baz.js";
loc.start.column = 0;
loc.start.line = 1;
loc.end.column = 3;
loc.end.line = 1;
const node = t.stringLiteral('baz');
node.loc = loc;
path.replaceWith(node);
},
},
};
};