/
_config.js
97 lines (83 loc) · 2.47 KB
/
_config.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
89
90
91
92
93
94
95
96
97
const assert = require('assert');
const { readFileSync } = require('fs');
const buble = require('buble');
const MagicString = require('magic-string');
const { SourceMapConsumer } = require('source-map');
const getLocation = require('../../getLocation');
module.exports = {
description: 'get combined sourcemap in transforming with loader',
options: {
plugins: [
{
load(id) {
const code = readFileSync(id, 'utf-8');
const out = buble.transform(code, {
transforms: { modules: false },
sourceMap: true,
source: id
});
return { code: out.code, map: out.map };
}
},
{
async transform(code, id) {
const sourcemap = this.getCombinedSourcemap();
const smc = await new SourceMapConsumer(sourcemap);
const s = new MagicString(code);
if (/foo.js$/.test(id)) {
testFoo(code, smc);
s.prepend('console.log("foo start");\n\n');
s.append('\nconsole.log("foo end");');
} else {
testMain(code, smc);
s.appendRight(code.indexOf('console'), 'console.log("main start");\n\n');
s.append('\nconsole.log("main end");');
}
return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
}
},
{
async transform(code, id) {
const sourcemap = this.getCombinedSourcemap();
const smc = await new SourceMapConsumer(sourcemap);
const s = new MagicString(code);
if (/foo.js$/.test(id)) {
testFoo(code, smc);
s.prepend('console.log("-- foo ---");\n\n');
s.append('\nconsole.log("-----");');
} else {
testMain(code, smc);
s.appendRight(code.indexOf('console'), 'console.log("-- main ---");\n\n');
s.append('\nconsole.log("-----");');
}
return {
code: s.toString(),
map: s.generateMap({ hires: true })
};
}
}
]
},
async test(code, map) {
const smc = await new SourceMapConsumer(map);
testFoo(code, smc);
testMain(code, smc);
}
};
function testFoo(code, smc) {
const generatedLoc = getLocation(code, code.indexOf(42));
const originalLoc = smc.originalPositionFor(generatedLoc);
assert.ok(/foo/.test(originalLoc.source));
assert.equal(originalLoc.line, 1);
assert.equal(originalLoc.column, 25);
}
function testMain(code, smc) {
const generatedLoc = getLocation(code, code.indexOf('info'));
const originalLoc = smc.originalPositionFor(generatedLoc);
assert.ok(/main/.test(originalLoc.source));
assert.equal(originalLoc.line, 3);
assert.equal(originalLoc.column, 8);
}