|
1 | | -# CodeSandbox Playground |
| 1 | +# SandPacker |
2 | 2 |
|
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 |
5 | 23 |
|
6 | 24 | ## Example usage |
7 | 25 |
|
| 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 | + |
8 | 64 | ```jsx |
9 | | -import Playground from 'codesandbox-playground'; |
| 65 | +import Preview from 'codesandbox-playground/dist/components/Preview'; |
10 | 66 |
|
11 | 67 | const files = { |
12 | | - 'index.js': ` |
| 68 | + '/index.js': ` |
13 | 69 | import uuid from 'uuid'; |
14 | 70 |
|
15 | 71 | console.log(uuid()); |
16 | 72 | `, |
17 | 73 | }; |
18 | 74 |
|
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 | +}; |
31 | 78 |
|
32 | | -### Advantages of codesandbox-playground |
| 79 | +export default () => ( |
| 80 | + <Preview files={files} dependencies={dependencies} entry="/index.js" /> |
| 81 | +); |
| 82 | +``` |
33 | 83 |
|
34 | | -CodeSandbox supports multi file examples. |
| 84 | +We will automatically update the preview as the code changes. |
0 commit comments