-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.ts
36 lines (30 loc) · 1.13 KB
/
esbuild.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
import { BuildOptions, BuildResult, Service, TransformOptions, TransformResult } from 'esbuild';
import { version } from 'esbuild-wasm/package.json';
const getService = async (): Promise<Service> => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const esbuild = require('esbuild-wasm/esm/browser');
const service = await esbuild.startService({
worker: true,
wasmURL: `https://unpkg.com/esbuild-wasm@${version}/esbuild.wasm`,
});
return service;
};
const wasmBuild = async (options: BuildOptions): Promise<BuildResult> => {
const service = await getService();
try {
return service.build(options);
} finally {
//service.stop();
}
};
const wasmTransform = async (input: string, options: TransformOptions): Promise<TransformResult> => {
const service = await getService();
try {
return service.transform(input, options);
} finally {
// service.stop();
}
};
const build: typeof import('esbuild').build = __NODE__ ? require('esbuild').build : wasmBuild;
const transform: typeof import('esbuild').transform = __NODE__ ? require('esbuild').transform : wasmTransform;
export { build, transform };