This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.cjs
74 lines (68 loc) · 2.05 KB
/
esbuild.cjs
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
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const {dependencies,peerDependencies}=require('./package.json');
// Select all typescript files of src directory as entry points
const entryPoints = fs.readdirSync(path.join(process.cwd(), "src"))
.filter(
(file) =>
file.endsWith(".ts") &&
fs.statSync(path.join(process.cwd(), "src", file)).isFile()
)
.map((file) => `src/${file}`);
const params = {};
process.argv.forEach(function (val, index, array) {
if (val.includes('=')) {
const keyVal = val.split('=');
params[keyVal[0]] = keyVal[1];
}
});
console.log("esbuild.js params=", params);
if (params.watch === 'true') {
params.watch = {
onRebuild: (error, result) => {
if (error) console.error('watch build failed:', error)
else {
onBuild();
console.log('watch build succeeded:', result);
}
}
}
}
const options = {
entryPoints: entryPoints,
bundle: true,
sourcemap: true,
minify: false,
splitting: false,
format: 'esm',
target: ['esnext'],
define: { global: "window" },
external: Object.keys(dependencies).concat(Object.keys(peerDependencies)),
watch: params.watch
};
esbuild
.build({ ...options, outfile: 'lib/index.mjs.js' }).then(result => {
if (options.watch) {
onBuild();
}
console.log(params.watch ? 'watching...' : '', result);
})
.catch(() => process.exit(1));
function onBuild() {
exec('yarn run ts-types', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
esbuild
.build({ ...options, format: "cjs", splitting: false, outfile: 'lib/index.js' }).then(result => {
console.log('cjs built', result);
})
.catch(() => process.exit(1));