-
Notifications
You must be signed in to change notification settings - Fork 8
/
esbuild.config.mjs
90 lines (80 loc) · 2.6 KB
/
esbuild.config.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
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 fs from 'fs';
import path from 'path';
const isDev = process.argv.includes('--dev');
console.log(`Starting esbuild in ${isDev ? 'development' : 'production'} mode...`);
const sharedConfig = {
bundle: true,
target: 'es2020',
define: {
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
},
sourcemap: isDev,
minify: !isDev,
};
function buildMain() {
return esbuild.build({
...sharedConfig,
entryPoints: ['./src/main.ts'],
outfile: 'dist/main.js',
platform: 'node',
external: ['electron'],
});
}
function buildRenderer() {
return esbuild.build({
...sharedConfig,
entryPoints: ['./src/index.tsx'],
outfile: 'dist/bundle.js',
platform: 'browser',
loader: { '.js': 'jsx', '.jsx': 'jsx', '.ts': 'tsx', '.tsx': 'tsx' },
});
}
// Recursive function to copy files and directories
function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
fs.readdirSync(src).forEach((childItemName) => {
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
}
function copyStaticFiles() {
const publicPath = path.resolve('public');
const distPath = path.resolve('dist');
const resourcesPath = path.resolve('src/resources');
// Copy index.html
const sourceHtmlPath = path.join(publicPath, 'index.html');
const destHtmlPath = path.join(distPath, 'index.html');
if (fs.existsSync(sourceHtmlPath)) {
fs.copyFileSync(sourceHtmlPath, destHtmlPath);
console.log('index.html copied to dist folder');
} else {
console.error(`Source index.html not found at ${sourceHtmlPath}`);
process.exit(1);
}
// Copy the resources folder
const destResourcesPath = path.join(distPath, 'resources');
if (fs.existsSync(resourcesPath)) {
copyRecursiveSync(resourcesPath, destResourcesPath);
console.log('Resources folder copied to dist folder');
} else {
console.error(`Resources folder not found at ${resourcesPath}`);
}
}
Promise.all([buildMain(), buildRenderer()])
.then(() => {
copyStaticFiles();
console.log('Build completed.');
})
.catch((err) => {
console.error('Build failed:', err);
process.exit(1);
});