-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.config.js
More file actions
71 lines (66 loc) · 1.69 KB
/
esbuild.config.js
File metadata and controls
71 lines (66 loc) · 1.69 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
import fs from 'fs/promises';
import esbuild from 'esbuild';
import packageJson from './package.json' with { type: 'json' };
const externalDeps = [
...Object.keys(packageJson.peerDependencies ?? {}),
...Object.keys(packageJson.dependencies ?? {}),
];
const buildFormats = [
{
format: 'esm',
outfile: 'dist/esm/index.mjs',
platform: 'node',
},
{
format: 'cjs',
outfile: 'dist/cjs/index.cjs',
platform: 'node',
},
];
function buildForFormat({ format, outfile, platform }) {
return esbuild.build({
entryPoints: ['./src/index.ts'],
bundle: true,
outfile,
platform,
format,
sourcemap: true,
target: 'node18',
minify: true,
treeShaking: true,
external: externalDeps,
metafile: true,
plugins: [
{
name: 'log-bundle-size',
setup(build) {
build.onEnd(async () => {
const { size } = await fs.stat(outfile);
const sizeInMB = (size / (1024 * 1024)).toFixed(2);
console.log(`\n[${format.toUpperCase()}] ${outfile}: ${sizeInMB} MB`);
});
},
},
],
});
}
async function buildAll() {
for (const { format, outfile, platform } of buildFormats) {
const result = await buildForFormat({ format, outfile, platform });
// Output analysis to console
console.log(await esbuild.analyzeMetafile(result.metafile));
}
}
if (process.argv.includes('--watch')) {
// Watch mode - only build the first format
buildForFormat(buildFormats[0]).then((context) => {
context.watch();
console.log('Watching for changes...');
});
} else {
// Build mode with analysis
buildAll().catch((error) => {
console.error(error);
process.exit(1);
});
}