-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.ts
More file actions
60 lines (55 loc) · 1.69 KB
/
Copy pathcomponents.ts
File metadata and controls
60 lines (55 loc) · 1.69 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
/**
* Component Compilation Utilities
*
* Profile B esbuild: bundled with external React dependencies.
*/
import * as esbuild from 'esbuild';
import type { HandlerCompileResult } from './handlers';
/**
* Compile a single remote component TypeScript file to JavaScript.
*
* Uses bundled mode with external React + Exepad runtime dependencies (Profile B):
* - bundle: true (resolves all non-external imports)
* - external: react, react/*, react-dom, react-dom/*, @exepad/* (sdk + extensions)
* - format: esm, target: es2022
*
* `@exepad/sdk` (and any `@exepad/ext-*`) MUST stay external: the runtime resolves
* these bare specifiers in the browser via the SPA import map
* (`@exepad/sdk` → `/runtime_assets/dist/exepad-sdk.js`) when it dynamically
* `import()`s the compiled component. Bundling them would fail to resolve at
* compile time and double-load React at run time.
*/
export async function compileComponent(
sourcePath: string,
outputPath: string
): Promise<HandlerCompileResult> {
try {
const result = await esbuild.build({
entryPoints: [sourcePath],
outfile: outputPath,
bundle: true,
format: 'esm',
target: 'es2022',
platform: 'browser',
external: ['react', 'react/*', 'react-dom', 'react-dom/*', '@exepad/sdk', '@exepad/*'],
minify: false,
sourcemap: false,
logLevel: 'warning',
});
if (result.errors.length > 0) {
return {
success: false,
errors: result.errors.map((e) => e.text),
};
}
return {
success: true,
outputPath,
};
} catch (error) {
return {
success: false,
errors: [error instanceof Error ? error.message : String(error)],
};
}
}