Skip to content

Commit

Permalink
refactor: upgrade @vue/repl (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
makedopamine authored Jul 15, 2024
1 parent ea20dd6 commit 23e3820
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 331 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@unocss/reset": "^0.60.4",
"@vue/repl": "^3.4.0",
"@vue/repl": "^4.2.1",
"@vueuse/core": "^10.10.0",
"element-plus": "^2.7.5",
"semver": "^7.6.2",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 12 additions & 48 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<script setup lang="ts">
import { Repl, type SFCOptions } from '@vue/repl'
import { Repl } from '@vue/repl'
import Monaco from '@vue/repl/monaco-editor'
import type { ImportMap } from '@/utils/import-map'
import type { UserOptions } from '@/composables/store'
import { useStore } from './composables/store'
const loading = ref(true)
const replRef = ref<InstanceType<typeof Repl>>()
// enable experimental features
const sfcOptions: SFCOptions = {
script: {
propsDestructure: true,
},
}
const previewOptions = {
headHTML: `
<script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"><\/script>
Expand All @@ -25,52 +18,20 @@ const previewOptions = {
`,
}
const initialUserOptions: UserOptions = {}
const dark = useDark()
const theme = new URLSearchParams(location.search).get('theme')
if (theme === 'dark') {
dark.value = true
}
const pr = new URLSearchParams(location.search).get('pr')
if (pr) {
initialUserOptions.showHidden = true
initialUserOptions.styleSource = `https://preview-${pr}-element-plus.surge.sh/bundle/index.css`
}
const store = useStore({
serializedState: location.hash.slice(1),
userOptions: initialUserOptions,
pr,
initialized: () => {
loading.value = false
},
})
if (pr) {
const map: ImportMap = {
imports: {
'element-plus': `https://preview-${pr}-element-plus.surge.sh/bundle/index.full.min.mjs`,
'element-plus/': 'unsupported',
},
}
store.state.files[IMPORT_MAP].code = JSON.stringify(map, undefined, 2)
}
if (pr || dark.value) {
const url = `${location.origin}${location.pathname}#${store.serialize()}`
history.replaceState({}, '', url)
}
if (store.pr) {
if (!store.userOptions.styleSource)
store.userOptions.styleSource = `https://preview-${store.pr}-element-plus.surge.sh/bundle/index.css`
store.versions.elementPlus = 'preview'
}
// eslint-disable-next-line unicorn/prefer-top-level-await
store.init().then(() => (loading.value = false))
if (!store.pr && store.userOptions.styleSource) {
store.pr = store.userOptions.styleSource.split('-', 2)[1]
}
// eslint-disable-next-line no-console
console.log('Store:', store)
Expand All @@ -82,7 +43,13 @@ const handleKeydown = (evt: KeyboardEvent) => {
}
// persist state
watchEffect(() => history.replaceState({}, '', `#${store.serialize()}`))
watchEffect(() =>
history.replaceState(
{},
'',
`${location.origin}${location.pathname}#${store.serialize()}`,
),
)
const refreshPreview = () => {
replRef.value?.reload()
Expand All @@ -97,10 +64,7 @@ const refreshPreview = () => {
:theme="dark ? 'dark' : 'light'"
:store="store"
:editor="Monaco"
show-compile-output
auto-resize
:preview-options="previewOptions"
:sfc-options="sfcOptions"
:clear-console="false"
@keydown="handleKeydown"
/>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getSupportedVueVersions,
} from '@/utils/dependency'
import type { Ref } from 'vue'
import type { ReplStore, VersionKey } from '@/composables/store'
import type { Store, VersionKey } from '@/composables/store'
const appVersion = import.meta.env.APP_VERSION
const replVersion = import.meta.env.REPL_VERSION
Expand All @@ -18,7 +18,7 @@ const dark = useDark()
const toggleDark = useToggle(dark)
const { store } = defineProps<{
store: ReplStore
store: Store
}>()
interface Version {
Expand Down
Loading

0 comments on commit 23e3820

Please sign in to comment.