-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.dev.js
107 lines (91 loc) · 3.34 KB
/
build.dev.js
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
107
const chokidar = require('chokidar');
const { build } = require('esbuild');
const path = require('path');
const fs = require('fs');
async function buildEntrypoint(entryPoint, outdir, target, format, platform) {
try {
await build({
entryPoints: [entryPoint],
bundle: true,
outdir,
target,
format,
platform,
minify: false,
});
const folderName = entryPoint.includes('server') ? 'server' : 'client';
console.log(
(await import('chalk')).default.green(
`[${folderName}]: Built successfully!`,
),
);
} catch (error) {
console.error(
(await import('chalk')).default.red(
`Error building ${entryPoint}: ${error.message}`,
),
);
}
}
async function watchFolder(entryPoint, outDir, target, format, platform) {
const chalk = (await import('chalk')).default;
await buildEntrypoint(entryPoint, outDir, target, format, platform);
// Using the derived folder name to log the correct folder
const folderName = entryPoint.includes('server') ? 'server' : 'client';
console.log(chalk.yellow(`[${folderName}]: Watching for changes...`));
chokidar
.watch(path.dirname(entryPoint), {
ignoreInitial: true,
})
.on('change', async (event, path) => {
// Remove everything before the folder name
event = event.replace(__dirname, '');
console.log(
chalk.yellow(
`[${folderName}]: File changed: ${event} - Rebuilding...`,
),
);
await buildEntrypoint(entryPoint, outDir, target, format, platform);
});
}
async function watchAll() {
const folders = ['server', 'client'];
for (const folder of folders) {
const folderPath = path.join(__dirname, folder);
// Find server.ts or client.ts (our entrypoints)
let entryPoint = fs.readdirSync(folderPath);
if (!entryPoint) {
console.log(
(await import('chalk')).default.red(
`[${folder}]: No results returned from readdirSync. Please make sure the ${folder} folder contains a entrypoint file (server.ts or client.ts).`,
),
);
process.exit(1);
}
// Check if we have an entrypoint
if (!entryPoint.includes(`${folder}.ts`)) {
console.log(
(await import('chalk')).default.red(
`[${folder}]: No entrypoint found! Please create a entrypoint file (server.ts or client.ts) in the ${folder} folder.`,
),
);
process.exit(1);
}
entryPoint = entryPoint.find((file) => {
return file.endsWith('.ts') && file === `${folder}.ts`;
});
console.log(
(await import('chalk')).default.yellow(
`[${folder}]: Building entrypoint: ${entryPoint}...`,
),
);
await watchFolder(
path.join(folderPath, entryPoint),
path.join(__dirname, 'dist', folder),
folder === 'client' ? ['chrome58'] : undefined,
folder === 'client' ? 'iife' : 'cjs',
folder === 'server' ? 'node' : undefined,
);
}
}
watchAll();