-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.config.mjs
More file actions
90 lines (79 loc) · 2.17 KB
/
build.config.mjs
File metadata and controls
90 lines (79 loc) · 2.17 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
87
88
89
90
import esbuild from 'esbuild';
import { readFileSync } from 'fs';
import { resolve } from 'path';
// Read package.json to get external dependencies
const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
const external = Object.keys({
...packageJson.dependencies,
...packageJson.peerDependencies,
});
const baseConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
platform: 'node',
target: 'node18',
external,
sourcemap: true,
minify: process.env.NODE_ENV === 'production',
keepNames: true,
treeShaking: true,
metafile: true,
logLevel: 'info',
};
async function build() {
try {
// Build ESM
console.log('Building ESM...');
const esmResult = await esbuild.build({
...baseConfig,
format: 'esm',
outfile: 'dist/index.mjs',
banner: {
js: `// @concero/operator-utils v${packageJson.version} - ESM build`,
},
});
// Build CJS
console.log('Building CJS...');
const cjsResult = await esbuild.build({
...baseConfig,
format: 'cjs',
outfile: 'dist/index.js',
banner: {
js: `// @concero/operator-utils v${packageJson.version} - CJS build`,
},
});
// Print build analysis if requested
if (process.env.ANALYZE) {
const esmMeta = esmResult.metafile;
const cjsMeta = cjsResult.metafile;
console.log('\nESM Build Analysis:');
console.log(await esbuild.analyzeMetafile(esmMeta, { verbose: true }));
console.log('\nCJS Build Analysis:');
console.log(await esbuild.analyzeMetafile(cjsMeta, { verbose: true }));
}
console.log('\n✅ Build completed successfully!');
} catch (error) {
console.error('❌ Build failed:', error);
process.exit(1);
}
}
// Watch mode
if (process.argv.includes('--watch')) {
console.log('Starting watch mode...');
const contexts = await Promise.all([
esbuild.context({
...baseConfig,
format: 'esm',
outfile: 'dist/index.mjs',
}),
esbuild.context({
...baseConfig,
format: 'cjs',
outfile: 'dist/index.js',
}),
]);
await Promise.all(contexts.map(ctx => ctx.watch()));
console.log('Watching for changes...');
} else {
build();
}