-
Notifications
You must be signed in to change notification settings - Fork 314
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
5 changed files
with
215 additions
and
27 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,101 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import {useEffect, useState, useRef} from 'react'; | ||
import * as stylex from '@stylexjs/stylex'; | ||
import {WebContainer} from '@webcontainer/api'; | ||
import {files} from './playground-utils/files'; | ||
|
||
async function wcSpawn(instance, ...args) { | ||
console.log('Running:', args.join(' ')); | ||
const process = await instance.spawn(...args); | ||
process.output.pipeTo( | ||
new WritableStream({ | ||
write(data) { | ||
console.log(data); | ||
}, | ||
}), | ||
); | ||
const exitCode = await process.exit; | ||
if (exitCode !== 0) { | ||
console.log('Command Failed:', args.join(' '), 'with exit code', exitCode); | ||
throw new Error('Command Failed', args.join(' ')); | ||
} | ||
|
||
console.log('Command Successful:', args.join(' ')); | ||
return process; | ||
} | ||
|
||
async function makeWebcontainer() { | ||
console.log('Booting WebContainer...'); | ||
const instance = await WebContainer.boot(); | ||
console.log('Boot successful!'); | ||
|
||
console.log('Mounting files...'); | ||
await instance.mount(files); | ||
console.log('Mounted files!'); | ||
|
||
console.log('Installing dependencies...'); | ||
await wcSpawn(instance, 'npm', ['install']); | ||
console.log('Installed dependencies!'); | ||
|
||
return instance; | ||
} | ||
|
||
export default function Playground() { | ||
const instance = useRef(null); | ||
const [output, setOutput] = useState(''); | ||
|
||
const build = async () => { | ||
const containerInstance = instance.current; | ||
if (!containerInstance) return; | ||
|
||
await wcSpawn(containerInstance, 'npm', ['run', 'build']); | ||
const output = await containerInstance.fs.readFile('output.js', 'utf-8'); | ||
setOutput(output); | ||
}; | ||
|
||
useEffect(() => { | ||
makeWebcontainer().then((i) => { | ||
instance.current = i; | ||
build(); | ||
}); | ||
() => { | ||
instance.current.unmount(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div {...stylex.props(styles.container)}> | ||
<pre {...stylex.props(styles.textarea)}> | ||
{files['input.js'].file.contents} | ||
</pre> | ||
<pre {...stylex.props(styles.textarea)}>{output}</pre> | ||
</div> | ||
); | ||
} | ||
|
||
const styles = stylex.create({ | ||
container: { | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
height: stylex.firstThatWorks('calc(100dvh - 60px)', 'calc(100vh - 60px)'), | ||
borderBottomWidth: 2, | ||
borderBottomStyle: 'solid', | ||
borderBottomColor: 'var(--cyan)', | ||
}, | ||
textarea: { | ||
width: '50%', | ||
height: '100%', | ||
borderWidth: 0, | ||
borderStyle: 'none', | ||
}, | ||
}); |
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,98 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
export const files = { | ||
'.babelrc.js': { | ||
file: { | ||
contents: ` | ||
const tsSyntaxPlugin = require('@babel/plugin-syntax-typescript'); | ||
const jsxSyntaxPlugin = require('@babel/plugin-syntax-jsx'); | ||
module.exports = { | ||
plugins: [ | ||
tsSyntaxPlugin, | ||
jsxSyntaxPlugin, | ||
[ | ||
"@stylexjs/babel-plugin", | ||
{ | ||
dev: true, | ||
stylexSheetName: "<>", | ||
genConditionalClasses: true, | ||
unstable_moduleResolution: { | ||
type: "commonJS", | ||
rootDir: '/', | ||
}, | ||
}, | ||
], | ||
], | ||
}; | ||
`, | ||
}, | ||
}, | ||
'package.json': { | ||
file: { | ||
contents: ` | ||
{ | ||
"name": "stylex-playground", | ||
"version": "1.0.0", | ||
"description": "Playground using WebContainers", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "babel ./input.js -o ./output.js" | ||
}, | ||
"dependencies": { | ||
"@stylexjs/stylex": "0.2.0-beta.27", | ||
"@stylexjs/babel-plugin": "0.2.0-beta.27", | ||
"@babel/cli": "latest", | ||
"@babel/core": "latest", | ||
"@babel/plugin-syntax-typescript": "latest", | ||
"@babel/plugin-syntax-jsx": "latest" | ||
} | ||
} | ||
`, | ||
}, | ||
}, | ||
'input.js': { | ||
file: { | ||
contents: ` | ||
import * as React from 'react'; | ||
import * as stylex from "@stylexjs/stylex"; | ||
export default function Card({ onClick, children, theme, variant, em = false }) { | ||
const props = stylex.props(theme, styles.base, em && styles.emphasise, variantStyles[variant]); | ||
} | ||
const styles = stylex.create({ | ||
base: { | ||
appearance: "none", | ||
borderStyle: "none", | ||
backgroundColor: "blue", | ||
color: "white", | ||
borderRadius: 4, | ||
paddingBlock: 4, | ||
paddingInline: 8, | ||
}, | ||
emphasise: { | ||
transform: "scale(1.1)", | ||
} | ||
}); | ||
const variantStyles = stylex.create({ | ||
danger: { | ||
backgroundColor: "red", | ||
color: "white", | ||
}, | ||
primary: { | ||
fontWeight: "bold", | ||
}, | ||
}); | ||
`, | ||
}, | ||
}, | ||
}; |
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.