forked from material-table-core/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.js
71 lines (60 loc) · 1.68 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
const { build } = require('esbuild');
const { red, green, yellow, italic } = require('chalk');
const rimraf = require('rimraf');
const path = require('path');
const fs = require('fs');
const { log } = console;
const { stdout, stderr, exit } = process;
const BUILD_DIR = 'dist'; // relative to root of project (no trailing slash)
stdout.write(yellow(`-Cleaning build artifacts from : '${BUILD_DIR}' `));
rimraf(path.resolve(__dirname, BUILD_DIR), async (error) => {
if (error) {
stderr.write(red(`err cleaning '${BUILD_DIR}' : ${error.stderr}`));
exit(1);
}
log(green.italic('successfully cleaned build artifacts'));
const options = {
entryPoints: getFilesRecursive('./src', '.js'),
minify: true,
// format: 'esm',
outdir: `${BUILD_DIR}`,
loader: {
'.js': 'jsx'
}
};
log(yellow('-Begin bundling'));
try {
await build(options);
log(
green(`\nSuccessfully bundled to '${BUILD_DIR}'`),
yellow('\n[note]'),
italic.green(': this path is relative to the root of this project)')
);
} catch {
stderr.write(red(`\nerror bundling : ${error.stderr}`));
exit(1);
} finally {
exit(0);
}
});
/**
* Helper functions
*/
function getFilesRecursive(dirPath, fileExtension = '') {
const paths = traverseDir(dirPath);
if (fileExtension === '') {
return paths;
}
return paths.filter((p) => p.endsWith(fileExtension));
}
function traverseDir(dir, filePaths = []) {
fs.readdirSync(dir).forEach((file) => {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
traverseDir(fullPath, filePaths);
} else {
filePaths.push(fullPath);
}
});
return filePaths;
}