-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.mjs
74 lines (61 loc) · 1.79 KB
/
esbuild.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
import esbuild from 'esbuild';
import {copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync} from 'node:fs';
import {join} from 'node:path';
const PACKAGE_JSON = 'package.json';
const readPackageJson = () => {
const packageJson = join(process.cwd(), PACKAGE_JSON);
const json = readFileSync(packageJson, 'utf8');
const {peerDependencies, files} = JSON.parse(json);
return {
workspacePeerDependencies: peerDependencies ?? {},
packageJsonFiles: files ?? []
};
};
const {workspacePeerDependencies, packageJsonFiles} = readPackageJson();
const dist = join(process.cwd(), 'dist');
const createDistFolder = () => {
if (!existsSync(dist)) {
mkdirSync(dist);
}
};
const buildBrowser = (entryPoints) => {
esbuild
.build({
entryPoints,
outdir: 'dist',
bundle: true,
sourcemap: true,
minify: true,
splitting: false,
format: 'esm',
define: {global: 'window'},
target: ['esnext'],
platform: 'browser',
conditions: ['browser'],
external: [...Object.keys(workspacePeerDependencies)]
})
.catch(() => process.exit(1));
};
const bundleFiles = () => {
const entryPoints = readdirSync(join(process.cwd(), 'src'))
.filter(
(file) =>
!file.includes('test') &&
!file.includes('spec') &&
!file.includes('mock') &&
statSync(join(process.cwd(), 'src', file)).isFile()
)
.map((file) => `src/${file}`);
buildBrowser(entryPoints);
};
const copyFiles = () => {
const copyFile = (filename) => copyFileSync(join(process.cwd(), filename), join(dist, filename));
packageJsonFiles.filter((entry) => !entry.includes('*')).forEach(copyFile);
copyFile(PACKAGE_JSON);
};
export const build = () => {
createDistFolder();
bundleFiles();
copyFiles();
};
build();