Skip to content

Commit

Permalink
feat: support navigating to virtual file
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 26, 2023
1 parent 48c65fd commit ff27b92
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/devtools/client/components/AssetDetails.vue
Expand Up @@ -6,6 +6,8 @@ const props = defineProps<{
asset: AssetInfo
}>()
const openInEditor = useOpenInEditor()
const imageMeta = computedAsync(() => {
if (props.asset.type !== 'image')
return undefined
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/client/components/ComposableItem.vue
Expand Up @@ -8,6 +8,7 @@ const props = defineProps<{
}>()
const copy = useCopy()
const openInEditor = useOpenInEditor()
const name = computed(() => props.item.as || props.item.name)
const usageCount = computed(() => props.metadata?.injectionUsage?.[name.value]?.count || 0)
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/client/components/FilepathItem.vue
Expand Up @@ -5,6 +5,7 @@ const props = defineProps<{
subpath?: boolean
}>()
const openInEditor = useOpenInEditor()
const config = useServerConfig()
const parsed = computed(() => (props.filepath && config.value)
? parseReadablePath(props.filepath, config.value.rootDir)
Expand Down
4 changes: 3 additions & 1 deletion packages/devtools/client/components/ModuleItem.vue
Expand Up @@ -30,6 +30,8 @@ const iconBase = 'https://api.nuxtjs.org/api/ipx/s_80,f_webp/gh/nuxt/modules/mai
const avatarBase = 'https://api.nuxtjs.org/api/ipx/s_44,f_webp/gh_avatar/'
const githubBase = 'https://github.com/'
const npmBase = 'https://www.npmjs.com/package/'
const openInEditor = useOpenInEditor()
</script>

<template>
Expand All @@ -50,7 +52,7 @@ const npmBase = 'https://www.npmjs.com/package/'
v-else-if="mod.entryPath"
role="button"
hover="underline text-primary"
@click="rpc.openInEditor(mod.entryPath!)"
@click="openInEditor(mod.entryPath!)"
>
{{ data.name }}
</button>
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/client/components/RoutesTable.vue
Expand Up @@ -13,6 +13,7 @@ defineEmits<{
(e: 'navigate', path: string): void
}>()
const openInEditor = useOpenInEditor()
const sorted = computed(() => {
return [...props.pages].sort((a, b) => a.path.localeCompare(b.path))
})
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/client/components/ServerRouteDetails.vue
Expand Up @@ -45,6 +45,8 @@ const fetchTime = ref(0)
const fetching = ref(false)
const started = ref(false)
const openInEditor = useOpenInEditor()
const parsedRoute = computed(() => props.route.route?.split(/((?:\*\*)?:[\w_]+)/g))
const paramNames = computed(() => parsedRoute.value?.filter(i => i.startsWith(':') || i.startsWith('**:')) || [])
Expand Down
20 changes: 18 additions & 2 deletions packages/devtools/client/composables/editor.ts
@@ -1,7 +1,23 @@
import { useClipboard } from '@vueuse/core'

export async function openInEditor(filepath: string) {
await rpc.openInEditor(filepath)
export function useOpenInEditor() {
const config = useServerConfig()
const virtualFiles = useVirtualFiles()
const router = useRouter()

return async (filepath: string) => {
const buildDir = config.value?.buildDir
const path = (buildDir && filepath.startsWith(buildDir))
? `#build${filepath.slice(buildDir.length).replace(/\.\w+$/, '')}`
: filepath

const vfs = virtualFiles.value?.entries.find(i => i.path === path || i.id === path)
|| virtualFiles.value?.entries.find(i => i.path === filepath || i.id === filepath)
if (vfs)
router.push(`/modules/virtual-files?id=${encodeURIComponent(vfs.id)}`)
else
await rpc.openInEditor(filepath)
}
}

export function useCopy() {
Expand Down
22 changes: 22 additions & 0 deletions packages/devtools/client/composables/state.ts
Expand Up @@ -176,3 +176,25 @@ interface RestartDialog {
export function useRestartDialogs() {
return useState<RestartDialog[]>('devtools:restart-dialogs', () => [])
}

export interface VfsData {
rootDir: string
entries: {
id: string
path: string
}[]
}

export interface VfsFile {
id: string
content: string
}

export function useVirtualFiles() {
const { data } = useFetch<VfsData>('/_vfs.json', {
key: 'vfs-list',
baseURL: '/',
responseType: 'json',
})
return data
}
4 changes: 2 additions & 2 deletions packages/devtools/client/pages/modules/plugins.vue
Expand Up @@ -12,7 +12,7 @@ const client = useClient()
const plugins = computed((): PluginInfoWithMetic[] => {
const plugins = config.value?.plugins || []
const metics = client.value.getClientPluginMetrics()
const metics = client.value?.getClientPluginMetrics() || []
return plugins.map((plugin) => {
const p = typeof plugin === 'string' ? { src: plugin } : plugin
Expand All @@ -24,7 +24,7 @@ const plugins = computed((): PluginInfoWithMetic[] => {
})
const totalTime = computed(() => {
const metics = client.value.getClientPluginMetrics()
const metics = client.value?.getClientPluginMetrics() || []
const minStart = Math.min(...metics.map(m => m.start))
const maxEnd = Math.max(...metics.map(m => m.end))
return maxEnd - minStart
Expand Down
20 changes: 2 additions & 18 deletions packages/devtools/client/pages/modules/virtual-files.vue
@@ -1,5 +1,6 @@
<script setup lang="ts">
import Fuse from 'fuse.js'
import type { VfsFile } from '~/composables/state'
definePageMeta({
icon: 'i-carbon-border-none',
Expand All @@ -8,26 +9,9 @@ definePageMeta({
category: 'advanced',
})
interface VfsData {
rootDir: string
entries: {
id: string
path: string
}[]
}
interface VfsFile {
id: string
content: string
}
const searchString = ref('')
const { data } = await useFetch<VfsData>('/_vfs.json', {
key: 'vfs-list',
baseURL: '/',
responseType: 'json',
})
const data = useVirtualFiles()
const fileId = computed(() => useRoute().query?.id as string | undefined)
Expand Down

0 comments on commit ff27b92

Please sign in to comment.