Skip to content

Commit 708e72e

Browse files
author
Ives van Hoorne
committed
Explanation
1 parent d26dcce commit 708e72e

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed
Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,84 @@
1-
# CodeSandbox Playground
1+
# SandPacker
22

3-
> A React component that lets you embed an application with any npm dependency.
4-
> Using the technology of CodeSandbox.
3+
A bundler that completely works in the browser, utilizing browser APIs.
4+
5+
## Why?
6+
7+
Online code playgrounds are getting more popular: they provide an easy way to play with code without installation. Until a year ago it was very hard to play with bigger web applications in the browser; there was no bundler that was comparable with local bundlers and worked in the browser.
8+
9+
CodeSandbox came along, and still had a pretty basic bundler. However, as CodeSandbox got more popular its bundler got more advanced. Nowadays the bundler has comparable parity with Webpack, and it would be a shame if others couldn't use the functionality.
10+
11+
This library acts as an interface with the bundler of CodeSandbox. It allows you to run any code on a web page, from Vue projects to React projects.
12+
13+
## So what can the bundler do?
14+
15+
This is a list of features that the bundler supports, it may be outdated.
16+
17+
1. Hot Module Reloading API (`module.hot`).
18+
2. npm dependencies
19+
3. 17 transpilers
20+
4. Friendly error overlay
21+
5. Parallel transpiling
22+
6. On-demand transpiler loading
523

624
## Example usage
725

26+
There are multiple ways to use the bundler. We provided two ways, but we're open to PRs to extend ways to use this!
27+
28+
### Using the Manager
29+
30+
The Manager is a plain JavaScript implementation, you can use it by importing Manager.
31+
32+
```js
33+
import { Manager } from 'codesandbox-playground';
34+
35+
// Let's say you want to show the preview on #preview
36+
const m = new Manager('#app', {
37+
files: {
38+
'/index.js': `console.log(require('uuid'))`,
39+
},
40+
dependencies: {
41+
uuid: 'latest',
42+
},
43+
entry: '/index.js',
44+
});
45+
46+
// This will show the preview on the #preview element, you can now send new code
47+
// by calling 'sendCode(files, dependencies, entry)'. We will automatically determine
48+
// how to hot update the preview.
49+
m.sendCode(
50+
{
51+
'/index.js': `console.log('other code')`,
52+
},
53+
{
54+
uuid: 'latest',
55+
},
56+
'/index.js'
57+
);
58+
```
59+
60+
### As a React Component
61+
62+
We included a React component you can use, implementation is fairly simple.
63+
864
```jsx
9-
import Playground from 'codesandbox-playground';
65+
import Preview from 'codesandbox-playground/dist/components/Preview';
1066

1167
const files = {
12-
'index.js': `
68+
'/index.js': `
1369
import uuid from 'uuid';
1470
1571
console.log(uuid());
1672
`,
1773
};
1874

19-
export default () => <Playground files={files} />;
20-
```
21-
22-
## Inspiration
23-
24-
[react-live](https://github.com/FormidableLabs/react-live) is an inspiration for
25-
this library.
26-
27-
### Advantages of react-live
28-
29-
You should react-live when you don't want to rely on CodeSandbox to render your
30-
code, and if you want your examples to be server side rendered.
75+
const dependencies = {
76+
uuid: 'latest',
77+
};
3178

32-
### Advantages of codesandbox-playground
79+
export default () => (
80+
<Preview files={files} dependencies={dependencies} entry="/index.js" />
81+
);
82+
```
3383

34-
CodeSandbox supports multi file examples.
84+
We will automatically update the preview as the code changes.

packages/codesandbox-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"scripts": {
3232
"storybook": "start-storybook -p 6006",
3333
"build-storybook": "build-storybook",
34-
"build": "microbundle"
34+
"build": "microbundle && tsc"
3535
},
3636
"dependencies": {
3737
"codesandbox": "^1.1.20"

packages/codesandbox-playground/src/components/OpenInCodeSandbox/OpenInCodeSandbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export default class OpenInCodeSandbox extends React.Component<IFileProps> {
1212
getFileParameters = () => {
1313
const { files, dependencies } = this.props;
1414
const paramFiles = { ...files };
15+
1516
const packageJSON = {
1617
main: this.props.entry,
1718
dependencies,
1819
};
19-
2020
paramFiles['/package.json'] = {
2121
code: JSON.stringify(packageJSON, null, 2),
2222
};
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import OpenInCodeSandbox from './components/OpenInCodeSandbox/index';
22
import Preview from './components/Preview/index';
33

4-
export { OpenInCodeSandbox, Preview };
4+
import Manager from './manager/index';
5+
6+
const components = { OpenInCodeSandbox, Preview };
7+
8+
export { components, Manager };

packages/codesandbox-playground/test/index.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)