-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add code transformer to
<Demo>
component (#7894)
- Loading branch information
Showing
6 changed files
with
1,213 additions
and
16 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/docusaurus-theme/src/components/demo/code_transformer.ts
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,36 @@ | ||
const IMPORT_REGEX = /^import [^'"]* from ['"]([^.'"\n ][^'"\n ]*)['"];?/gm; | ||
const DEFAULT_EXPORT_REGEX = /export default /; | ||
const COMPONENT_ONLY_REGEX = /^\(?</; | ||
|
||
/** | ||
* Transforms input JS/TS source code to a react-live compatible syntax. | ||
* react-live uses the surcase library to transform input source code into | ||
* browser-readable JavaScript. | ||
* | ||
* While surcase does support CommonJS and ESM import/export statements, | ||
* it's not trivial to expose our internal React and EUI exports through it | ||
* and because we already control the execution scope of the interactive demos | ||
* it isn't really necessary to implement a smart `require()` replacement. | ||
* | ||
* Returning an IIFE is necessary when the source code is more than just | ||
* a JSX component definition (e.g. it contains a variable definition | ||
* or `export default` statement). | ||
* | ||
* @see {@link https://github.com/alangpierce/sucrase} | ||
* @see {@link https://github.com/FormidableLabs/react-live/blob/master/packages/react-live/src/utils/transpile/index.ts} | ||
*/ | ||
export const demoCodeTransformer = (code: string) => { | ||
// Remove ESM imports | ||
code = code.replace(IMPORT_REGEX, ''); | ||
|
||
// Handle ESM default exports | ||
code = code.replace(DEFAULT_EXPORT_REGEX, 'return '); | ||
|
||
// If the demo is JSX only return as-is | ||
if (COMPONENT_ONLY_REGEX.test(code)) { | ||
return code; | ||
} | ||
|
||
// If the demo is more than just JSX wrap in an immediately invoked function expression | ||
return `(() => { ${code} })()`; | ||
} |
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,8 +1,11 @@ | ||
import React from 'react'; | ||
import * as EUI from '@elastic/eui'; | ||
|
||
export const demoScope: Record<string, unknown> = { | ||
export const demoDefaultScope: Record<string, unknown> = { | ||
// React | ||
React, | ||
...React, | ||
|
||
// EUI exports | ||
...EUI, | ||
}; |
Oops, something went wrong.