-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): add typescript support for live code blocks (#2824) #3984
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,55 @@ import clsx from 'clsx'; | |
|
||
import styles from './styles.module.css'; | ||
|
||
function Playground({children, theme, transformCode, ...props}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed
|
||
const typescriptExtensions = ['ts', 'tsx']; | ||
const transformJs = (code) => `${code};`; | ||
|
||
function Playground({children, theme, className, scope}) { | ||
const [tsTranspileError, setTsTranspileError] = React.useState(null); | ||
const transformTs = React.useCallback((code) => { | ||
try { | ||
// eslint-disable-next-line global-require | ||
const {code: transformed} = require('@babel/standalone').transform(code, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like it does work? |
||
filename: 'transformedCode.ts', | ||
presets: [ | ||
'react', | ||
[ | ||
'typescript', | ||
{ | ||
isTSX: true, | ||
allExtensions: true, | ||
}, | ||
], | ||
], | ||
}); | ||
setTsTranspileError(null); | ||
|
||
return transformed; | ||
} catch (e) { | ||
setTsTranspileError(e.message); | ||
} | ||
|
||
return '() => null;'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case if something goes wrong in the transpilation, render nothing. Theoretically it should never reach here. |
||
}, []); | ||
|
||
const isTypescriptCode = React.useMemo( | ||
() => | ||
typescriptExtensions.some((extension) => className.endsWith(extension)), | ||
[className], | ||
); | ||
|
||
const transformCode = React.useMemo(() => { | ||
return isTypescriptCode ? transformTs : transformJs; | ||
}, [isTypescriptCode, transformTs]); | ||
|
||
return ( | ||
<LiveProvider | ||
scope={scope} | ||
code={children.replace(/\n$/, '')} | ||
transformCode={transformCode || ((code) => `${code};`)} | ||
transformCode={transformCode} | ||
theme={theme} | ||
{...props}> | ||
language={isTypescriptCode ? 'typescript' : 'javascript'} | ||
className={className}> | ||
<div | ||
className={clsx( | ||
styles.playgroundHeader, | ||
|
@@ -35,7 +77,7 @@ function Playground({children, theme, transformCode, ...props}) { | |
</div> | ||
<div className={styles.playgroundPreview}> | ||
<LivePreview /> | ||
<LiveError /> | ||
{tsTranspileError ? <pre>{tsTranspileError}</pre> : <LiveError />} | ||
</div> | ||
</LiveProvider> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test broke it's a happy new year.