Skip to content

Commit

Permalink
feat(plugin): 添加客户端支持并优化版本信息传输
Browse files Browse the repository at this point in the history
  • Loading branch information
FliPPeDround committed Nov 21, 2024
1 parent b671be1 commit 493fc58
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/client/src/pages/overview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { version } from './../../package.json'
import UniIcon from '/icon/uni_icon.png'
const { initState } = useInitState()
const pages = await trpc.getPages.query()
const modules = await trpc.getComponent.query()
Expand Down
23 changes: 15 additions & 8 deletions packages/client/src/stores/init.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { decompressURLQuery } from '@uni-helper/devtools-shared'
// import { decompressURLQuery } from '@uni-helper/devtools-shared'
import type { InitState } from '~/types'

export const useInitState = createGlobalState(
() => {
const initState = ref<InitState>()
const initState = ref<InitState | object>({})
function init() {
const params = new URLSearchParams(window.location.search)
const data = params.get('data')
if (!data)
return
initState.value = decompressURLQuery(data)
console.log('initState', initState.value)
// const params = new URLSearchParams(window.location.search)
// const data = params.get('data')
// if (!data)
// return
// initState.value = decompressURLQuery(data)
// console.log('initState', initState.value)
trpc.onVersion.subscribe(undefined, {
onData: (data) => {
initState.value!.vueVersion = data.vueVersion
initState.value!.uniCompileVersion = data.uniVersion
console.log('initState', initState.value)

Check warning on line 18 in packages/client/src/stores/init.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
},
})
}

return {
Expand Down
16 changes: 11 additions & 5 deletions packages/plugin/inspect/UniDevTools.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<script setup>
import { ref, version } from 'vue'
import { getCurrentInstance, onMounted, ref } from 'vue'
import { basename } from '@uni-helper/devtools-shared'
import { getActivePinia } from 'pinia'
import { getCurrentPage } from './initMPClient'
onMounted(() => {
getCurrentPage()
})
console.log('====================================================')

Check warning on line 10 in packages/plugin/inspect/UniDevTools.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const x = ref(0)
const y = ref(0)
Expand All @@ -26,9 +32,9 @@ function handleTouchEnd() {
}
function getPiniaState() {
const state = JSON.parse(JSON.stringify(getActivePinia().state.value))
// const state = JSON.parse(JSON.stringify(getActivePinia().state.value))
// 获取所有 stores 的信息
return state
// return state
}
/** 递归获取组件名称和地址 */
Expand All @@ -55,6 +61,7 @@ function extractComponentInfo(component) {
}
function handleTap() {
console.log(getCurrentInstance())

Check warning on line 64 in packages/plugin/inspect/UniDevTools.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const pages = getCurrentPages()
const piniaState = getPiniaState()
const currentPage = pages[pages.length - 1]
Expand All @@ -63,7 +70,6 @@ function handleTap() {
const data = {
currentPage: currentPage.route,
vueVersion: version,
uniPlatform,
uniCompileVersion,
uniRuntimeVersion,
Expand Down
32 changes: 32 additions & 0 deletions packages/plugin/inspect/initMPClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { onMounted, version } from 'vue'
import { trpc } from './trpc'

export function initMPClient() {
sendVersion()
getCurrentPage()
}

function sendVersion() {
const { uniRuntimeVersion } = uni.getSystemInfoSync()

const vueVersion = version
const uniVersion = uniRuntimeVersion

trpc.sendVersion.subscribe({
vueVersion,
uniVersion,
}, {
onComplete: () => {},
})
}

export function getCurrentPage() {
onMounted(() => {
// const i = getCurrentInstance()

// const pages = getCurrentPages()
// const i = pages[pages.length - 1]

// console.log('instance', i)
})
}
1 change: 1 addition & 0 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@uni-helper/trpc-client": "workspace:*",
"@uni-helper/uni-env": "^0.1.1",
"@vue/compiler-sfc": "^3.4.27",
"@webviewjs/webview": "^0.1.3",
"body-parser": "^1.20.2",
"detect-port": "^1.6.1",
"error-stack-parser-es": "^0.1.4",
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin/src/devtoolServer/rpc/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export async function getImageMeta(filepath: string) {
if (_imageMetaCache.has(filepath))
return _imageMetaCache.get(filepath)
try {
const meta = imageMeta(await fs.readFile(filepath)) as ImageMeta
const fileBuffer = await fs.readFile(filepath)
const uint8Array = new Uint8Array(fileBuffer.buffer, fileBuffer.byteOffset, fileBuffer.byteLength)
const meta = imageMeta(uint8Array) as ImageMeta
_imageMetaCache.set(filepath, meta)
return meta
}
Expand Down
12 changes: 9 additions & 3 deletions packages/plugin/src/devtoolServer/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { parseStack } from 'error-stack-parser-es/lite'
import { extractPathByStack, sourceFile } from '../../utils/sourceFile'
import type { ConsoleInfo, ModuleInfo, Options } from './../../types'
import { getPagesInfo } from './../../logic'
import { publicProcedure, router } from './../trpc'
import { mergeRouters, publicProcedure, router } from './../trpc'
import { DIR_INSPECT_LIST } from './../../dir'
import { getImageMeta, getStaticAssets, getTextAssetContent } from './assets'
import { openInEditor } from './openInEditor'
import openInBrowser from './openInBrowser'
import { versionRouter } from './version'

export default function (
config: ResolvedConfig,
Expand All @@ -20,14 +21,14 @@ export default function (
) {
const { query, input, subscription } = publicProcedure

return router({
const routes = router({
getComponent: query(() => {
const json = readJsonSync(DIR_INSPECT_LIST)
return json.modules as ModuleInfo[]
}),
getPages: query(() => {
const [_, pages] = getPagesInfo(options?.pageJsonPath)

console.log(pages)
return pages
}),
staticAssets: query(() => {
Expand Down Expand Up @@ -79,4 +80,9 @@ export default function (
})
}),
})

return mergeRouters(
routes,
versionRouter(eventEmitter),
)
}
37 changes: 37 additions & 0 deletions packages/plugin/src/devtoolServer/rpc/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { EventEmitter } from 'node:stream'
import { z } from 'zod'
import { observable } from '@trpc/server/observable'
import { publicProcedure, router } from './../trpc'

interface Version {
vueVersion: string
uniVersion: string
}

export function versionRouter(eventEmitter: EventEmitter) {
const { input, subscription } = publicProcedure

return router({
sendVersion: input(
z.object({
vueVersion: z.string(),
uniVersion: z.string(),
}),
).subscription(({ input }) => {
eventEmitter.emit('version', input)
}),
onVersion: subscription(() => {
return observable<Version>((emit) => {
const versionHandler = (data: Version) => {
emit.next(data)
}

eventEmitter.on('version', versionHandler)

return () => {
eventEmitter.off('version', versionHandler)
}
})
}),
})
}
1 change: 1 addition & 0 deletions packages/plugin/src/devtoolServer/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ const t = initTRPC.create()

export const router = t.router
export const publicProcedure = t.procedure
export const mergeRouters = t.mergeRouters
4 changes: 4 additions & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Options } from './types'
import type createRouter from './devtoolServer/rpc/index'
import { pluginByEnv } from './logic/pluginByEnv'
import { injectDevtoolInfo } from './inspects/injectDevtoolInfo'
import { open } from './webview'

export * from './types'
export type AppRouter = ReturnType<typeof createRouter>
Expand All @@ -19,6 +20,9 @@ export default function UniDevToolsPlugin(options?: Partial<Options>): Plugin[]
return _plugin
}
const port = options?.port || 5015
if (options?.client) {
open(port)
}
const inspect = loadInspectPlugin()
const [pagesPath, pages] = getPagesInfo(options?.pageJsonPath)
const rootPath = pagesPath.replace('pages.json', '')
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin/src/inspects/injectDevtoolInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ export function injectDevtoolInfo(code: string, id: string) {
const script = descriptor.script
const inspectInfo = /* js */`
;const __uni_devtoolInfo = {
name: ${basename(id, '.vue')},
filename: ${id},
name: "${basename(id, '.vue')}",
filename: "${id}",
}
`

if (script) {
ms.appendRight(script.loc.end.offset, inspectInfo)
}
else {
const lang = descriptor.scriptSetup?.lang
const langContet = lang === 'ts' ? 'lang="ts"' : ''
const inspectScript = /* js */`
<script>${inspectInfo}</script>
<script ${langContet}>${inspectInfo}</script>
`
ms.append(inspectScript)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin/src/logic/importDevtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export async function importDevtools(code: string, id: string) {
const ms = new MagicString(code)
const importer = [
`import UniDevTools from '@uni-helper/devtools/inspect/UniDevTools.vue';`,
`import {proxyConsole} from '@uni-helper/devtools/inspect/proxyConsole.js';`,
// `import {proxyConsole} from '@uni-helper/devtools/inspect/proxyConsole.js';`,
`import {initMPClient} from '@uni-helper/devtools/inspect/initMPClient.js';`,
]
const injectFunc = [
`proxyConsole();`,
// `proxyConsole();`,
`initMPClient();`,
]
const component = `app.component('uni-dev-tools', UniDevTools);`
ms.prepend(`\n${injectFunc.join('\n')}\n`)
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export interface Options {
* vue-devtools Opthins
*/
vueDevtoolsOptions: VitePluginVueDevToolsOptions
/**
* 客户端
* @default false
*/
client: boolean
}

export interface ModuleInfo {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin/src/webview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Application } from '@webviewjs/webview'

export function open(port: number) {
const app = new Application()
const window = app.createBrowserWindow()
console.log(`http://localhost:${port}`)
window.createWebview({
url: `http://localhost:${port}`,
})
app.run()
}
8 changes: 1 addition & 7 deletions playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { createSSRApp } from 'vue'
import App from './App.vue'
import 'uno.css'
import * as Pinia from 'pinia';
console.log(1)
console.log('string')
console.log(true)
console.log(1000n)
console.log(Symbol('123'))
console.log(undefined)
console.log(null)

export function createApp() {
const app = createSSRApp(App)
app.use(Pinia.createPinia());
Expand Down
Loading

0 comments on commit 493fc58

Please sign in to comment.