This repository was archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.js
More file actions
75 lines (67 loc) · 2.31 KB
/
esbuild.js
File metadata and controls
75 lines (67 loc) · 2.31 KB
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
import * as childProcess from 'child_process';
import * as fs from "fs";
import * as path from "path";
import * as esbuild from 'esbuild';
import argParser from 'args-parser';
const args = argParser(process.argv);
const packageJson = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"));
const getOutput = (command, options) => {
try {
return childProcess.execSync(command, options).toString().trim()
} catch (e) {
return '';
}
}
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
build.onResolve({filter: /[A-Z]:\/*/}, async () => ({external: false}));
build.onResolve({filter: /\$\/*/}, async () => ({external: false}));
build.onResolve({filter: /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/}, args => ({path: args.path, external: true}))
},
}
const config = JSON.stringify({
'version': packageJson.version,
'commitHash': getOutput('git rev-parse HEAD', {cwd: process.cwd()}) || '',
'commitCount': parseInt(getOutput('git rev-list --count HEAD', {cwd: process.cwd()}) || '0'),
'buildDate': new Date().toISOString(),
'port': args.port || (args.dev ? 3006 : 80),
'dev': args.dev,
})
const typePlugin = {
name: 'TypeScriptDeclarationsPlugin',
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) return
fs.writeSync('src/module/config.ts', `export default ${config}`)
childProcess.execSync('tsc')
})
}
}
function copyFolderSync(from, to) {
fs.mkdirSync(to);
fs.readdirSync(from).forEach(element => {
if (fs.lstatSync(path.join(from, element)).isFile()) {
fs.copyFileSync(path.join(from, element), path.join(to, element));
} else {
copyFolderSync(path.join(from, element), path.join(to, element));
}
});
}
fs.writeFileSync('src/module/config.ts', `export default ${config}`)
childProcess.execSync('tsc --module es2022')
try {
fs.rmdirSync('gcp', {recursive: true})
} catch (e) {
}
copyFolderSync('build/module/gcp', 'gcp')
esbuild.build({
entryPoints: ['./src/runner.ts'],
outfile: 'build/runner.js',
bundle: true,
plugins: [makeAllPackagesExternalPlugin],
platform: 'node',
format: 'esm',
}).then(() => {
console.log('✔ Build successful.')
})