-
Notifications
You must be signed in to change notification settings - Fork 29
/
compile.js
executable file
·54 lines (46 loc) · 1.59 KB
/
compile.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
const fs = require("fs");
const mkdirp = require("mkdirp");
const pathlib = require("path");
const browserify = require("browserify");
const stream = require("stream");
const { benchmarkDir, nodejsDistDir, quickjsDistDir } = require("./utils");
// mkdir dist dir
mkdirp.sync(nodejsDistDir);
mkdirp.sync(quickjsDistDir);
// shim code for quickjs
const quickjsShimCode = `
import * as os from 'os';
globalThis.global = globalThis;
globalThis.setTimeout = globalThis.setTimeout || os.setTimeout;
`;
async function compile(srcFilename) {
return new Promise((resolve) => {
console.log(`compiling ${srcFilename} ...`);
const benchmarkTemplate = fs.readFileSync(
pathlib.join(__dirname, "benchmark_template.js"),
"utf-8"
);
const benchmarkFilePath = pathlib.join(benchmarkDir, srcFilename);
// inject require path
const finalBenchmarkContent = benchmarkTemplate.replace(
"{{{benchmark_file}}}",
benchmarkFilePath
);
// construct readable stream for browserify
const readableStream = new stream.Readable();
readableStream.push(finalBenchmarkContent);
readableStream.push(null);
// bundle and output to dist
browserify(readableStream, { basedir: benchmarkDir }).bundle(function (
err,
buf
) {
const nodejsTarget = pathlib.join(nodejsDistDir, srcFilename);
const quickjsTarget = pathlib.join(quickjsDistDir, srcFilename);
fs.writeFileSync(nodejsTarget, `${buf.toString()}`);
fs.writeFileSync(quickjsTarget, `${quickjsShimCode} ${buf.toString()}`);
resolve();
});
});
}
exports = module.exports = compile;