-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.js
162 lines (136 loc) · 4.31 KB
/
esbuild.config.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const esbuild = require('esbuild');
const autoprefixer = require('autoprefixer');
const tailwindcss = require('tailwindcss');
const postCssPlugin = require('@deanc/esbuild-plugin-postcss');
const { esbuildPluginAliasPath } = require('esbuild-plugin-alias-path');
const { htmlPlugin } = require('@craftamap/esbuild-plugin-html');
const { blog } = require('./tailwind.config');
const { txt, time, loadingAnimation, clearLineAndDebug } = require('@charo164/helper');
const { existsSync, readdir, rmSync } = require('fs');
const { resolve, join } = require('path');
const StaticServer = require('static-server');
const watch = require('node-watch');
const args = process.argv.slice(2);
const nodeEnv = process.env.NODE_ENV || 'production';
const isDevMode = nodeEnv === 'development';
const ENTRY_POINTS = [resolve('src/index.tsx')];
const OUTDIR = resolve('public');
const MAP_FILE_REG = /^.*\.map$/gi;
const server = new StaticServer({
rootPath: './', // required, the root of the server file tree
port: 3000, // required, the port to listen
name: 'my-http-server', // optional, will set "X-Powered-by" HTTP header
cors: '*', // optional, defaults to undefined
followSymlink: true, // optional, defaults to a 404 error
});
switch (args[0]) {
case 'dev':
dev().catch((err) => {
clearLineAndDebug('start', err.message || 'Build error!');
process.exit(1);
});
break;
case 'start':
if (!existsSync(OUTDIR)) {
throw new Error(`Run ${txt.cyan("'npm run build'")} before.`);
}
server.start(function () {
clearLineAndDebug('serve', `Server listening to ${server.port}`);
});
break;
case 'build':
build().catch((err) => {
clearLineAndDebug('start', err.message || 'Build error!');
process.exit(1);
});
default:
break;
}
async function dev() {
time.start();
let loader = loadingAnimation('Dev starting...');
const appBuilder = await startBuild();
const watcher = watch(resolve('src'), { recursive: true });
watcher.on('change', async function (e, path) {
const changedFile = 'src/' + path.split('src/')[1];
clearLineAndDebug('watch', `${txt.cyan('File changed:')} ${changedFile}`);
try {
await appBuilder.rebuild();
} catch (err) {
clearLineAndDebug('watch', txt.red(err.message || 'Rebuild error!'));
}
});
watcher.on('ready', async function () {
clearInterval(loader);
console.clear();
time.end();
clearLineAndDebug('watch', 'Ready!');
clearLineAndDebug('build', txt.green(`Builded in ${time._time()}ms`));
server.start(function () {
clearLineAndDebug('serve', `Server listening to ${server.port}`);
});
loader = loadingAnimation('watching for file changes...');
});
watcher.on('error', function (err) {
clearLineAndDebug('watch', txt.red(err.message || 'Watch error!'));
process.exit(1);
});
}
async function build() {
time.start();
let loader = loadingAnimation('Build starting...');
const appBuilder = await startBuild();
await clear();
clearInterval(loader);
console.clear();
time.end();
clearLineAndDebug('build', 'Finish!');
clearLineAndDebug('build', txt.green(`Builded in ${time._time()}ms`));
console.log(txt.cyan(await esbuild.analyzeMetafile(appBuilder.metafile)));
process.exit();
}
async function startBuild() {
return await builder(ENTRY_POINTS, OUTDIR, [
postCssPlugin({
plugins: [tailwindcss(blog), autoprefixer],
}),
]);
}
/**
*
* @param {string[]} entryPoints
* @param {string} outdir
* @param {string[]} plugins
*/
async function builder(entryPoints, outdir, plugins = []) {
const builder = await esbuild.build({
bundle: true,
minify: !isDevMode,
sourcemap: isDevMode,
metafile: true,
platform: 'browser',
incremental: true,
entryPoints,
loader: {
'.mp3': 'file',
'.png': 'file',
},
outdir,
color: true,
plugins: [...plugins, esbuildPluginAliasPath({ alias: { '@': './src' } })],
});
return builder;
}
function clear() {
return new Promise((res, rej) => {
readdir(OUTDIR, { encoding: 'utf8', withFileTypes: true }, (err, files) => {
if (err) return rej(err);
files.map((f) => {
if (f.isFile() && f.name.match(MAP_FILE_REG)) {
rmSync(resolve(join(OUTDIR, f.name)), { force: true });
}
});
res(files);
});
});
}