Skip to content

Commit 5e90b94

Browse files
committed
feat: add a playground to easy test repl different scenarios
1 parent 5e092b6 commit 5e90b94

29 files changed

+667
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"scripts": {
5050
"dev": "vite",
51+
"dev:playground": "vite -c playground/vite.config.ts",
5152
"build": "vite build",
5253
"build-preview": "vite build -c vite.preview.config.ts",
5354
"format": "prettier --write .",

playground/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Vue REPL Playground
2+
3+
Optimize `test/main` to support dynamically adding scenario validations for the REPL. Use `npm run dev:playground` to try it out.
4+
5+
## Playground Architecture
6+
7+
### Directory Structure
8+
9+
```
10+
playground/
11+
├── index.html # HTML entry file with scenario switcher
12+
├── vite.config.ts # Standalone Vite config
13+
├── vite-plugin-scenario.js # Parse scenarios directory and generate REPL configuration
14+
├── scenarios/ # Scenario directory, add dynamically
15+
│ ├── basic/ # Basic example
16+
│ ├── customMain/ # Custom main entry
17+
│ ├── pinia/ # Pinia state management example
18+
│ └── vueRouter/ # Vue Router example
19+
└── src/
20+
└── App.vue # Main application component
21+
```
22+
23+
### How It Works
24+
25+
The playground uses a directory-based scenario system, where each scenario is an independent folder under `scenarios/`. Core features include:
26+
27+
- **Virtual Module System**: A Vite plugin scans the scenario directory and generates a virtual module `virtual:playground-files`
28+
- **Dynamic Scenario Loading**: Users can switch scenarios via the UI, which automatically loads the corresponding configuration
29+
30+
### Scenario Structure
31+
32+
Each scenario directory typically contains the following files:
33+
34+
```
35+
scenarios/example-scenario/
36+
├── App.vue # Main component
37+
├── main.ts # Entry file
38+
├── import-map.json # Dependency mapping
39+
├── tsconfig.json # TypeScript config
40+
└── _meta.js # Metadata config for REPL settings
41+
```
42+
43+
The `_meta.js` file exports the scenario configuration:
44+
45+
```javascript
46+
export default {
47+
mainFile: 'main.ts', // Specify the main entry file
48+
}
49+
```
50+
51+
## Usage Example
52+
53+
### Start the Playground
54+
55+
```bash
56+
# Enter the project directory
57+
cd vue-repl
58+
59+
# Install dependencies
60+
npm install
61+
62+
# Start the development server
63+
npm run playground
64+
```
65+
66+
Visit the displayed local address (usually http://localhost:5174/) to use the playground.
67+
68+
### Add a New Scenario
69+
70+
1. Create a new folder under the `scenarios/` directory, e.g. `myScenario`
71+
2. Add the required files:
72+
73+
```
74+
myScenario/
75+
├── App.vue # Main component
76+
├── main.ts # Entry file (default entry)
77+
├── import-map.json # Dependency config
78+
├── tsconfig.json # TypeScript config
79+
└── _meta.js # Config with mainFile: 'main.ts'
80+
```
81+
82+
3. Refresh the browser, and the new scenario will automatically appear in the dropdown menu.

playground/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en" class="dark">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Playground</title>
8+
<script type="module" src="./main.ts"></script>
9+
</head>
10+
<body>
11+
<div id="app"></div>
12+
</body>
13+
</html>

playground/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createApp } from 'vue'
2+
// @ts-ignore
3+
import App from './src/App.vue'
4+
5+
createApp(App).mount('#app')

playground/scenarios/basic/App.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
<button @click="increment">Increment</button>
4+
<p>Counter: {{ counter }}</p>
5+
</div>
6+
</template>
7+
8+
<script lang="ts" setup>
9+
// a simple demo check Repl default config
10+
import { ref } from 'vue'
11+
const counter = ref(0)
12+
const increment = () => {
13+
counter.value++
14+
}
15+
</script>

playground/scenarios/basic/_meta.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
mainFile: 'App.vue',
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<h1>custom main</h1>
4+
<button @click="$hi">test globalProperties</button>
5+
</div>
6+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
mainFile: 'main.ts',
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createApp } from 'vue'
2+
import App from './App.vue'
3+
4+
const app = createApp(App)
5+
app.config.globalProperties.$hi = () => alert('hi Vue')
6+
app.mount('#app')

0 commit comments

Comments
 (0)