-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
executable file
·46 lines (41 loc) · 1.21 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
#!/usr/bin/env node
const { readFile, writeFile } = require("fs");
const { promisify } = require("util");
const esbuild = require("esbuild");
const ts = require("typescript");
const { rewrite } = require("rewrite-imports");
const read = promisify(readFile);
const write = promisify(writeFile);
const esbuildCommon = {
entryPoints: ["./index.ts"],
platform: "node",
target: ["node18.12.0"],
};
(async function () {
//start esbuild process
let result = esbuild.build({
format: "esm",
outfile: "./dist/index.mjs",
...esbuildCommon,
});
// run .d.ts generation now since it takes a while
const program = ts.createProgram(["index.ts"], {
declaration: true,
emitDeclarationOnly: true,
outDir: "./dist",
});
program.emit();
//wait for esbuild to finish then rewrite imports for cjs version
await result;
let output = await read("./dist/index.mjs", "utf8");
await write(
"./dist/index.js",
rewrite(output).replace(
/export {.*sveltePlugin as default.*};/s,
"module.exports = sveltePlugin;",
),
);
})().catch((err) => {
console.error("ERROR", err.stack || err);
process.exit(1);
});