/
compileCode.ts
30 lines (26 loc) · 950 Bytes
/
compileCode.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
import { transform, TransformOptions } from 'buble';
import transpileImports from './transpileImports';
const compile = (code: string, config: TransformOptions): string => transform(code, config).code;
const startsWithJsx = (code: string): boolean => !!code.trim().match(/^</);
const wrapCodeInFragment = (code: string): string => `<React.Fragment>${code}</React.Fragment>;`;
/*
* 1. Wrap code in React Fragment if it starts with JSX element
* 2. Transform import statements into require() calls
* 3. Compile code using Buble
*/
export default function compileCode(
code: string,
compilerConfig: TransformOptions,
onError?: (err: Error) => void
): string {
try {
const wrappedCode = startsWithJsx(code) ? wrapCodeInFragment(code) : code;
const compiledCode = compile(wrappedCode, compilerConfig);
return transpileImports(compiledCode);
} catch (err) {
if (onError && err instanceof Error) {
onError(err);
}
}
return '';
}