-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
50 lines (39 loc) · 1.51 KB
/
build.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
const {SourceNode, SourceMapConsumer, SourceMapGenerator} = require('source-map');
const fs = require('fs');
function lines(source, filename) {
return source.trim().split('\n').map((line, lineNumber) => {
return new SourceNode(lineNumber + 1, 0, filename, `${line}\n`)
});
}
function dumbUglify(source, filename) {
/* TODO what are line, column, and source here for? */
const node = new SourceNode(1, 0, filename,
source.split('\n').map((line, lineNumber) => {
return new SourceNode(lineNumber + 1, 0, filename, line)
})
);
return node;
}
function write({code, map}, filename) {
fs.writeFileSync(`dist/${filename}`, code);
fs.writeFileSync(`dist/${filename}.map`, map.toString());
}
async function run() {
const a = fs.readFileSync(`src/a.js`, 'utf8');
const b = fs.readFileSync(`src/b.js`, 'utf8');
const bundleNode = new SourceNode(1, 0, 'why.js', [
...lines(a, 'a.js'),
new SourceNode(1, 0, null, 'console.log(`generated`);\n'),
...lines(b, 'b.js')
]);
const bundle = bundleNode.toStringWithSourceMap();
const bundleConsumer = await new SourceMapConsumer(bundle.map.toString());
bundle.map.setSourceContent('a.js', a);
bundle.map.setSourceContent('b.js', b);
write(bundle, 'bundle.js');
const uglified = dumbUglify(bundle.code, 'bundle.js').toStringWithSourceMap();
uglified.map.applySourceMap(bundleConsumer, 'bundle.js');
uglified.map.setSourceContent('bundle.js', bundle.code);
write(uglified, 'bundle.min.js');
}
run().catch(e => setImmediate(() => {throw e}))