forked from dmitmel/ccloader3
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.ts
More file actions
109 lines (89 loc) · 2.46 KB
/
build.ts
File metadata and controls
109 lines (89 loc) · 2.46 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
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
import * as esbuild from 'esbuild';
import copyStaticFiles from 'esbuild-copy-static-files';
import path from 'path';
import * as fs from 'fs';
const isWatch = process.argv[2] === 'watch';
function donePlugin(outfile: string): esbuild.Plugin {
return {
name: 'done plugin',
setup(build) {
build.onEnd(async (res) => {
let code = res.outputFiles![0]?.text;
if (!code) return; // when compile errors
await fs.promises.writeFile(outfile, code);
const bytes = code.length;
const kb = bytes / 1024;
console.log(outfile, `${kb.toFixed(1)}kb`);
});
},
};
}
const commonOptions = {
format: 'esm',
platform: 'node',
target: 'es2018',
write: false,
bundle: true,
minify: false,
sourcemap: 'inline',
} as const;
function core(): esbuild.BuildOptions {
const outfile = path.join('./dist', `core.js`);
return {
entryPoints: ['packages/core/src/main.ts'],
...commonOptions,
plugins: [donePlugin(outfile)],
};
}
function runtime(): esbuild.BuildOptions {
const outfile = path.join('./dist', 'runtime', 'main.js');
return {
entryPoints: ['packages/runtime/src/_main.ts'],
...commonOptions,
plugins: [
copyStaticFiles({
src: `./packages/runtime/ccmod.json`,
dest: `./dist/runtime/ccmod.json`,
}),
copyStaticFiles({
src: `./packages/runtime/media`,
dest: `./dist/runtime/media`,
}),
copyStaticFiles({
src: `./packages/runtime/assets`,
dest: `./dist/runtime/assets`,
}),
donePlugin(outfile),
],
};
}
function ccmodServiceWorker(): esbuild.BuildOptions {
const outfile = path.join('./', 'dist-ccmod-service-worker.js');
return {
entryPoints: ['packages/core/src/service-worker.ts'],
...commonOptions,
plugins: [donePlugin(outfile)],
};
}
const modules: Array<() => esbuild.BuildOptions> = [core, runtime, ccmodServiceWorker];
async function run(): Promise<void> {
fs.promises.mkdir('./dist', { recursive: true });
if (isWatch) {
console.clear();
await Promise.all(
modules.map(async (module) => {
const ctx = await esbuild.context(module());
await ctx.watch();
}),
);
} else {
await Promise.all(
modules.map(async (module) => {
await esbuild.build(module());
}),
);
// eslint-disable-next-line no-process-exit
process.exit(); // because esbuild keeps the process alive for some reason
}
}
run();