forked from openpatch/hyperbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildPackage.mjs
More file actions
86 lines (75 loc) · 2.05 KB
/
buildPackage.mjs
File metadata and controls
86 lines (75 loc) · 2.05 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import chalk from "chalk";
import { build } from "esbuild";
import { existsSync, readFileSync } from "fs";
import { join } from "path";
const ignorePackages = [];
export const buildPackage = async (path) => {
const split = path.split("/");
const packageName = split[split.length - 1];
if (ignorePackages.includes(packageName)) {
return;
}
const entries = [];
let entry = `${path}/src/index.ts`;
if (!existsSync(entry)) {
entry = `${path}/src/index.tsx`;
}
const isEntryExists = existsSync(entry);
let packageJSON;
try {
packageJSON = readFileSync(join(`${path}/package.json`), "utf-8");
} catch (e) {
return;
}
if (!isEntryExists || !packageJSON) {
throw new Error(`Entry file missing from ${packageName}`);
}
entries.push(entry);
const bundle = JSON.parse(packageJSON).bundle || [];
const external = [];
if (bundle !== "all") {
external.push(
...[
...Object.keys(JSON.parse(packageJSON)?.dependencies || {}).filter(
(p) => !bundle.includes(p),
),
...Object.keys(JSON.parse(packageJSON)?.devDependencies || {}).filter(
(p) => !bundle.includes(p),
),
...Object.keys(JSON.parse(packageJSON)?.peerDependencies || {}),
],
);
}
external.push("path");
external.push("fs");
const platform = JSON.parse(packageJSON)?.platform || "browser";
const commonConfig = {
entryPoints: entries,
outbase: path + "/src",
outdir: `${path}/dist`,
sourcemap: true,
minify: false,
bundle: true,
platform,
external,
};
// await build({
// ...commonConfig,
// outExtension: {
// ".js": ".cjs.js",
// },
// format: "cjs",
// }).catch((e) => {
// throw new Error(`CJS Build failed for ${packageName} \n ${e}`);
// });
await build({
...commonConfig,
// outExtension: {
// ".js": ".esm.mjs",
// },
format: "esm",
}).catch((e) => {
throw new Error(`ESM Build failed for ${packageName} \n ${e}`);
});
console.log(`build ${chalk.green("success")} - ${packageName}`);
};