-
Notifications
You must be signed in to change notification settings - Fork 908
/
Copy pathbuild.mjs
68 lines (60 loc) · 1.8 KB
/
build.mjs
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 path from 'node:path';
import fs from 'node:fs';
import { globSync } from 'glob';
import * as esbuild from 'esbuild';
import * as tsup from 'tsup';
async function build(relativePath) {
const packageJson = path.resolve(relativePath, 'package.json');
if (!fs.existsSync(packageJson)) {
return;
}
const tasks = [];
const pkg = relativePath.split(path.sep).slice(2)[0];
const files = ['index.ts'];
if (pkg === 'radix-ui') {
files.push('internal.ts');
}
const entryPoints = files.map((file) => `${relativePath}/src/${file}`);
const dist = `${relativePath}/dist`;
const esbuildConfig = {
entryPoints: entryPoints,
external: ['@radix-ui/*'],
packages: 'external',
bundle: true,
sourcemap: true,
format: 'cjs',
target: 'es2022',
outdir: dist,
};
tasks.push(esbuild.build(esbuildConfig).then(() => console.log(`CJS: Built ${relativePath}`)));
tasks.push(
esbuild
.build({
...esbuildConfig,
format: 'esm',
outExtension: { '.js': '.mjs' },
})
.then(() => console.log(`ESM: Built ${relativePath}`))
);
// tsup is used to emit d.ts files only (esbuild can't do that).
//
// Notes:
// 1. Emitting d.ts files is super slow for whatever reason.
// 2. It could have fully replaced esbuild (as it uses that internally),
// but at the moment its esbuild version is somewhat outdated.
// It’s also harder to configure and esbuild docs are more thorough.
tasks.push(
tsup
.build({
entry: entryPoints,
format: ['cjs', 'esm'],
dts: { only: true },
outDir: dist,
silent: true,
external: [/@radix-ui\/.+/],
})
.then(() => console.log(`TSC: Built ${relativePath}`))
);
await Promise.all(tasks);
}
globSync('packages/*/*').forEach(build);