-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ts
82 lines (75 loc) · 2.08 KB
/
build.ts
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
import * as esbuild from 'esbuild';
import { denoPlugins } from '@luca/esbuild-deno-loader';
import * as path from '@std/path';
import { APP_ENTRY_POINT } from './net/server/static-assets.ts';
export interface BundleResult {
source: string;
map: string;
}
export async function bundle(
sourcePath: string,
): Promise<Record<string, BundleResult>> {
const result = await esbuild.build({
entryPoints: [
{
in: sourcePath,
out: APP_ENTRY_POINT,
},
],
plugins: [...denoPlugins()],
bundle: true,
write: false,
sourcemap: 'linked',
});
return bundleResultFromBuildResult(result);
}
export function bundleResultFromBuildResult(
buildResult: esbuild.BuildResult,
): Record<string, BundleResult> {
const result = {} as Record<string, BundleResult>;
for (const file of buildResult.outputFiles!) {
const entryPoint = path.basename(file.path).split('.')[0] as string;
let bundleResult: BundleResult | undefined = result[entryPoint];
if (!bundleResult) {
bundleResult = {} as BundleResult;
result[entryPoint] = bundleResult;
}
if (file.path.endsWith('.js')) {
bundleResult.source = file.text;
} else if (file.path.endsWith('.js.map')) {
bundleResult.map = file.text;
}
}
return result;
}
export function stopBackgroundCompiler(): void {
esbuild.stop();
}
export interface ReBuildContext {
rebuild(): Promise<Record<string, BundleResult>>;
close(): void;
}
export function isReBuildContext(
ctx: ReBuildContext | typeof esbuild,
): ctx is ReBuildContext {
return typeof (ctx as ReBuildContext).rebuild === 'function';
}
export async function createBuildContext(
entryPoints: { in: string; out: string }[],
): Promise<ReBuildContext> {
const ctx = await esbuild.context({
entryPoints,
plugins: [...denoPlugins()],
bundle: true,
write: false,
sourcemap: 'linked',
outdir: 'output',
logOverride: {
'empty-import-meta': 'silent',
},
});
return {
rebuild: async () => bundleResultFromBuildResult(await ctx.rebuild()),
close: () => ctx.dispose(),
};
}