-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
40 lines (36 loc) · 953 Bytes
/
index.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
import * as esbuild from "esbuild-wasm";
import { unpkgPathPlugin } from "./plugins/unpkg-path-plugin";
import { fetchPlugin } from "./plugins/fetch-plugin";
let service: esbuild.Service;
const bundle = async (rawCode: string) => {
if (!service) {
service = await esbuild.startService({
worker: true,
wasmURL: "https://unpkg.com/esbuild-wasm@0.8.27/esbuild.wasm",
});
}
try {
const result = await service.build({
entryPoints: ["index.js"],
bundle: true,
write: false,
plugins: [unpkgPathPlugin(), fetchPlugin(rawCode)],
define: {
"process.env.NODE_ENV": '"production"',
global: "window",
},
jsxFactory: "_React.createElement",
jsxFragment: "_React.Fragment",
});
return {
code: result.outputFiles[0].text,
err: "",
};
} catch (err: any) {
return {
code: "",
err: err.message,
};
}
};
export default bundle;