Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export { themeB } from './themeB';
// etc...
```

## Additional Code Transformations

A hook into the internal processing of code is available via the `processCode` option, which is a path to a file that exports a function that receives the code as entered into the editor, and returns the new code to be rendered.

One example is [wrapping code in an IIFE for state support](https://github.com/seek-oss/playroom/issues/66).

## TypeScript Support

If a `tsconfig.json` file is present in your project, static prop types are parsed using [react-docgen-typescript](https://github.com/styleguidist/react-docgen-typescript) to provide better autocompletion in the Playroom editor.
Expand Down
1 change: 1 addition & 0 deletions lib/defaultModules/processCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (code) => code;
3 changes: 3 additions & 0 deletions lib/makeWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ module.exports = async (playroomConfig, options) => {
__PLAYROOM_ALIAS__USE_SCOPE__: playroomConfig.scope
? relativeResolve(playroomConfig.scope)
: require.resolve('./defaultModules/useScope'),
__PLAYROOM_ALIAS__PROCESS_CODE__: playroomConfig.processCode
? relativeResolve(playroomConfig.processCode)
: require.resolve('./defaultModules/processCode'),
},
},
module: {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"release": {
"success": false
},
"jest": {
"moduleNameMapper": {
"^__PLAYROOM_ALIAS__PROCESS_CODE__$": "<rootDir>/lib/defaultModules/processCode"
}
},
"repository": {
"type": "git",
"url": "https://github.com/seek-oss/playroom.git"
Expand Down
14 changes: 12 additions & 2 deletions src/utils/compileJsx.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { transform } from '@babel/standalone';
/* eslint-disable-next-line import/no-unresolved */
/* @ts-expect-error: This import is webpack magic */
import processCode from '__PLAYROOM_ALIAS__PROCESS_CODE__';

export const compileJsx = (code: string) =>
transform(`<React.Fragment>${code.trim() || ''}</React.Fragment>`, {
export const compileJsx = (code: string) => {
const processedCode = processCode(code);

if (typeof processedCode !== 'string') {
throw new Error('processCode function must return a string of code.');
}

return transform(`<React.Fragment>${processedCode.trim()}</React.Fragment>`, {
presets: ['react'],
}).code;
};

export const validateCode = (code: string) => {
try {
Expand Down