Skip to content

Commit 57d3a05

Browse files
committed
workflow: add prod/dev toggle to sfc playground
1 parent 70c2d5b commit 57d3a05

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

packages/sfc-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"dependencies": {
1515
"vue": "3.2.33",
16-
"@vue/repl": "^1.0.0",
16+
"@vue/repl": "^1.1.0",
1717
"file-saver": "^2.0.5",
1818
"jszip": "^3.6.0"
1919
}

packages/sfc-playground/src/App.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<script setup lang="ts">
22
import Header from './Header.vue'
33
import { Repl, ReplStore } from '@vue/repl'
4-
import { watchEffect } from 'vue'
4+
import { ref, watchEffect } from 'vue'
55
66
const setVH = () => {
77
document.documentElement.style.setProperty('--vh', window.innerHeight + `px`)
88
}
99
window.addEventListener('resize', setVH)
1010
setVH()
1111
12+
const hash = location.hash.slice(1)
13+
const useDevMode = ref(hash.startsWith('__DEV__'))
14+
1215
const store = new ReplStore({
13-
serializedState: location.hash.slice(1),
16+
serializedState: useDevMode.value ? hash.slice(7) : hash,
1417
defaultVueRuntimeURL: import.meta.env.PROD
1518
? `${location.origin}/vue.runtime.esm-browser.js`
1619
: `${location.origin}/src/vue-dev-proxy`
@@ -19,16 +22,28 @@ const store = new ReplStore({
1922
// enable experimental features
2023
const sfcOptions = {
2124
script: {
25+
inlineTemplate: !useDevMode.value,
2226
reactivityTransform: true
2327
}
2428
}
2529
2630
// persist state
27-
watchEffect(() => history.replaceState({}, '', store.serialize()))
31+
watchEffect(() => {
32+
const newHash = store
33+
.serialize()
34+
.replace(/^#/, useDevMode.value ? `#__DEV__` : `#`)
35+
history.replaceState({}, '', newHash)
36+
})
37+
38+
function toggleDevMode() {
39+
const dev = (useDevMode.value = !useDevMode.value)
40+
sfcOptions.script.inlineTemplate = !dev
41+
store.setFiles(store.getFiles())
42+
}
2843
</script>
2944

3045
<template>
31-
<Header :store="store" />
46+
<Header :store="store" :dev="useDevMode" @toggle-dev="toggleDevMode" />
3247
<Repl
3348
@keydown.ctrl.s.prevent
3449
@keydown.meta.s.prevent

packages/sfc-playground/src/Header.vue

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Download from './icons/Download.vue'
88
import GitHub from './icons/GitHub.vue'
99
1010
// @ts-ignore
11-
const { store } = defineProps(['store'])
11+
const props = defineProps(['store', 'dev'])
12+
const { store } = props
1213
1314
const currentCommit = __COMMIT__
1415
const activeVersion = ref(`@${currentCommit}`)
@@ -112,6 +113,14 @@ async function fetchVersions(): Promise<string[]> {
112113
</li>
113114
</ul>
114115
</div>
116+
<button
117+
title="Toggle development production mode"
118+
class="toggle-dev"
119+
:class="{ dev }"
120+
@click="$emit('toggle-dev')"
121+
>
122+
{{ dev ? 'DEV' : 'PROD' }}
123+
</button>
115124
<button title="Toggle dark mode" class="toggle-dark" @click="toggleDark">
116125
<Sun class="light" />
117126
<Moon class="dark" />
@@ -126,13 +135,10 @@ async function fetchVersions(): Promise<string[]> {
126135
>
127136
<Download />
128137
</button>
129-
<button
130-
title="View on GitHub"
131-
class="github"
132-
>
138+
<button title="View on GitHub" class="github">
133139
<a
134-
href="https://github.com/vuejs/core/tree/main/packages/sfc-playground"
135-
target="_blank"
140+
href="https://github.com/vuejs/core/tree/main/packages/sfc-playground"
141+
target="_blank"
136142
>
137143
<GitHub />
138144
</a>
@@ -228,6 +234,16 @@ h1 img {
228234
top: 22px;
229235
}
230236
237+
.toggle-dev {
238+
color: #f07178;
239+
font-size: 12px;
240+
line-height: var(--nav-height);
241+
}
242+
243+
.toggle-dev.dev {
244+
color: #c3e88d;
245+
}
246+
231247
.toggle-dark svg {
232248
width: 18px;
233249
height: 18px;

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)