Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
73 lines (60 loc) · 1.54 KB
/
Copy pathbuild.mjs
File metadata and controls
73 lines (60 loc) · 1.54 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
import * as esbuild from 'esbuild';
import { readFile } from 'fs/promises';
export async function getBuildConfig() {
const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url)));
const external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.devDependencies || {}),
'react/jsx-runtime',
];
return {
entryPoints: ['src/index.ts'],
bundle: true,
minify: !process.argv.includes('--watch'),
sourcemap: true,
external,
target: 'es2020',
jsx: 'automatic',
};
}
export async function createBuildContexts(sharedConfig) {
const esmCtx = await esbuild.context({
...sharedConfig,
format: 'esm',
outfile: 'dist/esm/index.js',
banner: {
js: '"use client";',
},
});
const cjsCtx = await esbuild.context({
...sharedConfig,
format: 'cjs',
outfile: 'dist/cjs/index.js',
banner: {
js: '"use client";',
},
});
return { esmCtx, cjsCtx };
}
async function runStandaloneBuild() {
const start = performance.now();
const config = await getBuildConfig();
const { esmCtx, cjsCtx } = await createBuildContexts(config);
try {
await Promise.all([
esmCtx.rebuild(),
cjsCtx.rebuild(),
]);
} finally {
await Promise.all([
esmCtx.dispose(),
cjsCtx.dispose(),
]);
}
console.log(`⚡ Build complete - ${((performance.now() - start) / 1000).toFixed(2)}s`);
}
runStandaloneBuild().catch((e) => {
console.error('Build error:', e);
process.exit(1);
});