forked from rollup/rollup
/
index.js
47 lines (42 loc) · 923 Bytes
/
index.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
const path = require('path');
const weak = require('weak-napi');
const rollup = require('../..');
const { wait } = require('../utils');
var shouldCollect = false;
var isCollected = false;
function onCollect() {
isCollected = true;
}
async function waitForGC() {
const startTime = process.hrtime();
do {
global.gc();
await wait(0);
} while (!isCollected && process.hrtime(startTime)[0] < 3);
}
var cache;
async function run() {
const bundle = await rollup.rollup({
input: path.resolve(__dirname, 'main.js'),
cache
});
weak(bundle, onCollect);
cache = bundle;
if (shouldCollect) {
await waitForGC();
if (!isCollected) {
throw new Error('Memory leak detected');
}
}
shouldCollect = true;
}
run()
.then(run)
.then(() => {
console.log('Success: Previous bundle was correctly garbage collected.');
process.exit(0);
})
.catch(err => {
console.error(err.message);
process.exit(1);
});