Skip to content

feat: use ssr and async data #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/components/ReplEditor.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script setup lang="ts">
import type { Store } from '@vue/repl'
import { Repl } from '@vue/repl'
import MonacoEditor from '@vue/repl/monaco-editor'

const props = defineProps({
ssr: {
type: Boolean,
default: false,
},
store: {
type: Object as PropType<Store>,
required: true,
},
})

const colorMode = useColorMode()

const theme = computed(() => {
if (colorMode.value === 'dark')
return 'dark'
else
return 'light'
})

const previewOptions = {
headHTML: `
<script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"><\/script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css" />
<script>
window.__unocss = {
rules: [],
presets: [],
}
<\/script>
`,
}
</script>

<template>
<Repl
:store="props.store" :editor="MonacoEditor" :show-compile-output="true" :theme="theme" :preview-theme="true"
:preview-options="previewOptions" :ssr="props.ssr"
/>
</template>
29 changes: 12 additions & 17 deletions app/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
import { getVersionsBatch } from 'fast-npm-meta'
import semver from 'semver'

// todo: type
const versions = useSessionStorage<any>('versions', [])

const loadingVersions = shallowRef(false)

async function fetchVersions() {
loadingVersions.value = true
versions.value = await getVersionsBatch(['@vueuse/core', 'vue'])
loadingVersions.value = false
}

if (!versions.value.length) {
fetchVersions()
}
const { data: versions, status, error, refresh: fetchVersions } = await useAsyncData(
'versions',
async () => getVersionsBatch(['@vueuse/core', 'vue']),
{ default: () => ([]) },
)

const loadingVersions = computed(() => {
return status.value === 'pending'
})

const vueVersion = shallowRef()

const vueVersions = computed(() => {
const vue = versions.value.find(p => p.name === 'vue')
if (vue?.error)
if (vue?.error || error.value)
return []
return vue?.versions ?? []
})
Expand All @@ -32,7 +27,7 @@ const vueUseVersion = useRouteQuery('vueuse', 'latest')

const vueUseVersions = computed(() => {
const vueuse = versions.value.find(p => p.name === '@vueuse/core')
if (vueuse?.error)
if (vueuse?.error || error.value)
return []
return vueuse?.versions ?? []
})
Expand Down Expand Up @@ -68,7 +63,7 @@ const prod = useRouteQuery<string, boolean>('prod', 'false', {
<USwitch v-model="prod" label="Prod" />
<USelectMenu v-model="vueUseVersion" :items="vueUseVersionsSorted" class="w-32" icon="i-logos-vueuse" :loading="loadingVersions" />
<USelectMenu v-model="vueVersion" :items="vueVersionsSorted" class="w-32" icon="i-logos-vue" :loading="loadingVersions" />
<UButton icon="i-lucide-refresh-ccw" size="md" color="primary" variant="soft" @click="fetchVersions" />
<UButton icon="i-lucide-refresh-ccw" size="md" color="primary" variant="soft" @click="() => fetchVersions()" />
<UButton
color="neutral" variant="ghost"
:icon="colorMode.preference === 'dark' ? 'i-heroicons-moon' : 'i-heroicons-sun'"
Expand Down
39 changes: 9 additions & 30 deletions app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script setup lang="ts">
import type { OutputModes } from '@vue/repl'
import type { ShallowRef } from 'vue'
import { mergeImportMap, Repl, useStore, useVueImportMap } from '@vue/repl'
import MonacoEditor from '@vue/repl/monaco-editor'
import { mergeImportMap, useStore, useVueImportMap } from '@vue/repl'

const showOutput = useRouteQuery<string, boolean>('showOutput', 'false', {
transform: stringToBooleanTransformer,
Expand Down Expand Up @@ -81,35 +80,15 @@ watch(() => injectedVueVersion.value, (newVersion) => {
watch(() => prod.value, (newProd) => {
productionMode.value = newProd
}, { immediate: true })

const colorMode = useColorMode()

const theme = computed(() => {
if (colorMode.value === 'dark')
return 'dark'
else
return 'light'
})

const previewOptions = {
headHTML: `
<script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"><\/script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css" />
<script>
window.__unocss = {
rules: [],
presets: [],
}
<\/script>
`,
}
</script>

<template>
<client-only>
<Repl
:store="store" :editor="MonacoEditor" :show-compile-output="true" :theme="theme" :preview-theme="true"
:preview-options="previewOptions" :ssr="ssr"
/>
</client-only>
<ClientOnly>
<ReplEditor :ssr="ssr" :store="store" />
<template #fallback>
<div class="flex w-full h-full justify-center items-center px-12">
<UProgress animation="swing" />
</div>
</template>
</ClientOnly>
</template>
7 changes: 5 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
ssr: false,
compatibilityDate: '2024-11-01',
devtools: { enabled: true },
sourcemap: false,
future: {
compatibilityVersion: 4,
},
Expand All @@ -18,9 +18,12 @@ export default defineNuxtConfig({
},
build: {
rollupOptions: {
external: ['typescript'],
external: ['typescript', '@vue/compiler-sfc'],
},
},
ssr: {
noExternal: ['@vue/repl'],
},
},
css: ['~/assets/css/main.css'],
})