Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
106 lines (92 loc) · 2.53 KB
/
Copy pathbuild.js
File metadata and controls
106 lines (92 loc) · 2.53 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import esbuild from 'esbuild';
import { fileURLToPath, pathToFileURL } from 'url';
import { dirname, join } from 'path';
import { copyFileSync, existsSync, mkdirSync, readdirSync, statSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const isWatch = process.argv.includes('--watch');
const isProduction = !isWatch;
// Recursive copy function
function copyRecursive(src, dest) {
if (!existsSync(dest)) {
mkdirSync(dest, { recursive: true });
}
const items = readdirSync(src);
for (const item of items) {
const srcPath = join(src, item);
const destPath = join(dest, item);
if (statSync(srcPath).isDirectory()) {
copyRecursive(srcPath, destPath);
} else {
copyFileSync(srcPath, destPath);
}
}
}
// Copy static assets
function copyAssets() {
const distDir = join(__dirname, 'dist');
const assetsDir = join(__dirname, 'assets');
if (!existsSync(distDir)) {
mkdirSync(distDir, { recursive: true });
}
// Copy HTML file
copyFileSync(join(assetsDir, 'index.html'), join(distDir, 'index.html'));
// Copy images
const imgDir = join(distDir, 'img');
if (!existsSync(imgDir)) {
mkdirSync(imgDir, { recursive: true });
}
copyFileSync(join(assetsDir, 'img', 'brand.png'), join(imgDir, 'brand.png'));
// Create js directory
const jsDir = join(distDir, 'js');
if (!existsSync(jsDir)) {
mkdirSync(jsDir, { recursive: true });
}
}
const buildOptions = {
entryPoints: ['src/index.tsx'],
bundle: true,
outfile: 'dist/js/index.js',
format: 'iife',
platform: 'browser',
target: 'es2020',
minify: isProduction,
sourcemap: !isProduction,
define: {
'process.env.NODE_ENV': isProduction ? '"production"' : '"development"',
'global': 'window'
},
loader: {
'.css': 'css',
'.png': 'file',
'.jpg': 'file',
'.jpeg': 'file',
'.gif': 'file',
'.svg': 'file'
}
};
export async function buildOnce() {
copyAssets();
await esbuild.build(buildOptions);
console.log('Build complete');
}
export async function watchAndServe() {
copyAssets();
const ctx = await esbuild.context(buildOptions);
await ctx.watch();
console.log('Watching for changes...');
await ctx.serve({
servedir: 'dist',
port: 3001,
host: 'localhost'
});
console.log('Server running at http://localhost:3001');
}
const isCLI = process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url;
if (isCLI) {
if (isWatch) {
await watchAndServe();
} else {
await buildOnce();
}
}