forked from jodyheavener/storybook-docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.ts
68 lines (56 loc) · 1.55 KB
/
compile.ts
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
import { resolve } from "path";
import { context, BuildOptions } from "esbuild";
import rimraf from "rimraf";
const args = process.argv;
const flagNames = ["watch", "build"] as const;
const flags = flagNames.reduce((acc, name) => {
acc[name] = !!args.slice(2).find((s) => s.startsWith(`--${name}`));
return acc;
}, {}) as Record<typeof flagNames[number], boolean>;
const packageDirIndex = args.indexOf("--package");
const packageDir = args[packageDirIndex + 1];
if (packageDirIndex < 0 || !packageDir) {
throw new Error("Missing --package flag");
}
const srcPath = resolve(__dirname, packageDir, "src");
const distPath = resolve(__dirname, packageDir, "dist");
const run = async () => {
rimraf(distPath);
const baseOptions: BuildOptions = {
entryPoints: ["index.ts", "preview.tsx", "preset.ts"].map((e) =>
resolve(srcPath, e)
),
platform: "node",
outdir: distPath,
minify: flags.build,
};
const esm = await context({
...baseOptions,
format: "esm",
outExtension: {
".js": ".mjs",
},
});
const cjs = await context({
...baseOptions,
format: "cjs",
outExtension: {
".js": ".cjs",
},
});
if (flags.watch) {
console.log("Watching for changes...");
await Promise.all([esm.watch(), cjs.watch()]);
} else {
console.log(`Building ${packageDir}...`);
await Promise.all([esm.rebuild(), cjs.rebuild()]);
await esm.dispose();
await cjs.dispose();
}
};
run().catch((err: unknown) => {
if (err instanceof Error) {
console.error(err.stack);
}
process.exit(1);
});