-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
108 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'esbuild-wasm/esbuild.wasm' { | ||
const wasmURL: string; | ||
export default wasmURL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
import { transform } from '@babel/standalone'; | ||
import memoizeOne from 'memoize-one'; | ||
import * as esbuild from 'esbuild-wasm'; | ||
import esbuildWasmUrl from 'esbuild-wasm/esbuild.wasm'; | ||
|
||
const init = esbuild.initialize({ | ||
wasmURL: `/${esbuildWasmUrl}`, | ||
}); | ||
|
||
export const ReactFragmentPragma = 'R_F'; | ||
export const ReactCreateElementPragma = 'R_cE'; | ||
|
||
export const openFragmentTag = '<>'; | ||
export const closeFragmentTag = '</>'; | ||
|
||
export const compileJsx = (code: string) => | ||
transform(`${openFragmentTag}${code.trim()}${closeFragmentTag}`, { | ||
presets: [ | ||
[ | ||
'react', | ||
{ | ||
pragma: ReactCreateElementPragma, | ||
pragmaFrag: ReactFragmentPragma, | ||
}, | ||
export const compileJsxWithBabel = memoizeOne((code: string) => { | ||
const result = transform( | ||
`${openFragmentTag}${code.trim()}${closeFragmentTag}`, | ||
{ | ||
presets: [ | ||
[ | ||
'react', | ||
{ | ||
pragma: ReactCreateElementPragma, | ||
pragmaFrag: ReactFragmentPragma, | ||
}, | ||
], | ||
], | ||
], | ||
}).code; | ||
} | ||
); | ||
return result.code; | ||
}); | ||
|
||
export const compileJsx = memoizeOne(async (code: string) => { | ||
await init; | ||
const result = await esbuild.transform( | ||
`${openFragmentTag}${code.trim()}${closeFragmentTag}`, | ||
{ | ||
loader: 'jsx', | ||
jsxFactory: ReactCreateElementPragma, | ||
jsxFragment: ReactFragmentPragma, | ||
} | ||
); | ||
|
||
return result.code; | ||
}); | ||
|
||
export const validateCode = (code: string) => { | ||
export const validateCode = (code: string): true | Error => { | ||
try { | ||
compileJsx(code); | ||
compileJsxWithBabel(code); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
return err as Error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,4 @@ export const isValidLocation = ({ | |
cursor, | ||
snippet: breakoutString, | ||
}) | ||
); | ||
) === true; |