workflow: add prod/dev toggle to sfc playground

This commit is contained in:
Evan You 2022-05-17 12:10:29 +08:00
parent 70c2d5bbc0
commit 57d3a0566f
4 changed files with 47 additions and 16 deletions

View File

@ -13,7 +13,7 @@
},
"dependencies": {
"vue": "3.2.33",
"@vue/repl": "^1.0.0",
"@vue/repl": "^1.1.0",
"file-saver": "^2.0.5",
"jszip": "^3.6.0"
}

View File

@ -1,7 +1,7 @@
<script setup lang="ts">
import Header from './Header.vue'
import { Repl, ReplStore } from '@vue/repl'
import { watchEffect } from 'vue'
import { ref, watchEffect } from 'vue'
const setVH = () => {
document.documentElement.style.setProperty('--vh', window.innerHeight + `px`)
@ -9,8 +9,11 @@ const setVH = () => {
window.addEventListener('resize', setVH)
setVH()
const hash = location.hash.slice(1)
const useDevMode = ref(hash.startsWith('__DEV__'))
const store = new ReplStore({
serializedState: location.hash.slice(1),
serializedState: useDevMode.value ? hash.slice(7) : hash,
defaultVueRuntimeURL: import.meta.env.PROD
? `${location.origin}/vue.runtime.esm-browser.js`
: `${location.origin}/src/vue-dev-proxy`
@ -19,16 +22,28 @@ const store = new ReplStore({
// enable experimental features
const sfcOptions = {
script: {
inlineTemplate: !useDevMode.value,
reactivityTransform: true
}
}
// persist state
watchEffect(() => history.replaceState({}, '', store.serialize()))
watchEffect(() => {
const newHash = store
.serialize()
.replace(/^#/, useDevMode.value ? `#__DEV__` : `#`)
history.replaceState({}, '', newHash)
})
function toggleDevMode() {
const dev = (useDevMode.value = !useDevMode.value)
sfcOptions.script.inlineTemplate = !dev
store.setFiles(store.getFiles())
}
</script>
<template>
<Header :store="store" />
<Header :store="store" :dev="useDevMode" @toggle-dev="toggleDevMode" />
<Repl
@keydown.ctrl.s.prevent
@keydown.meta.s.prevent

View File

@ -8,7 +8,8 @@ import Download from './icons/Download.vue'
import GitHub from './icons/GitHub.vue'
// @ts-ignore
const { store } = defineProps(['store'])
const props = defineProps(['store', 'dev'])
const { store } = props
const currentCommit = __COMMIT__
const activeVersion = ref(`@${currentCommit}`)
@ -112,6 +113,14 @@ async function fetchVersions(): Promise<string[]> {
</li>
</ul>
</div>
<button
title="Toggle development production mode"
class="toggle-dev"
:class="{ dev }"
@click="$emit('toggle-dev')"
>
{{ dev ? 'DEV' : 'PROD' }}
</button>
<button title="Toggle dark mode" class="toggle-dark" @click="toggleDark">
<Sun class="light" />
<Moon class="dark" />
@ -126,13 +135,10 @@ async function fetchVersions(): Promise<string[]> {
>
<Download />
</button>
<button
title="View on GitHub"
class="github"
>
<button title="View on GitHub" class="github">
<a
href="https://github.com/vuejs/core/tree/main/packages/sfc-playground"
target="_blank"
href="https://github.com/vuejs/core/tree/main/packages/sfc-playground"
target="_blank"
>
<GitHub />
</a>
@ -228,6 +234,16 @@ h1 img {
top: 22px;
}
.toggle-dev {
color: #f07178;
font-size: 12px;
line-height: var(--nav-height);
}
.toggle-dev.dev {
color: #c3e88d;
}
.toggle-dark svg {
width: 18px;
height: 18px;

View File

@ -242,13 +242,13 @@ importers:
packages/sfc-playground:
specifiers:
'@vitejs/plugin-vue': ^1.10.2
'@vue/repl': ^1.0.0
'@vue/repl': ^1.1.0
file-saver: ^2.0.5
jszip: ^3.6.0
vite: ^2.9.8
vue: 3.2.33
dependencies:
'@vue/repl': 1.0.0_vue@packages+vue
'@vue/repl': 1.1.0_vue@packages+vue
file-saver: 2.0.5
jszip: 3.7.1
vue: link:../vue
@ -1349,8 +1349,8 @@ packages:
engines: {node: '>= 0.12.0'}
dev: true
/@vue/repl/1.0.0_vue@packages+vue:
resolution: {integrity: sha512-cDcQuWKZuA0Y0JYEpiQS/ZAEGP/RrfkcK+zKm5H8tUjfD8XIxYHY+sQGoY6FSkz/gAOQJocrsaPgt7ddKL0inQ==}
/@vue/repl/1.1.0_vue@packages+vue:
resolution: {integrity: sha512-qmu05PZdBtdkSacQgRdqmOnRbzJZlryee/d6OFZfRMwthHI8Ek3GLvTXkSAzhjgWXdENBPOBIoYW3gOZTVfNzA==}
peerDependencies:
vue: ^3.2.13
dependencies: