Skip to content

Commit

Permalink
refactor(vite): load devtools resource (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 committed Jul 23, 2024
1 parent a44978f commit b45bf94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function removeUrlQuery(url: string): string {
return url.replace(/\?.*$/, '')
}
25 changes: 21 additions & 4 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'node:fs'
import { normalizePath } from 'vite'
import type { PluginOption, ResolvedConfig, ViteDevServer } from 'vite'
import sirv from 'sirv'
Expand All @@ -11,6 +12,7 @@ import { bold, cyan, dim, green, yellow } from 'kolorist'
import type { VitePluginInspectorOptions } from 'vite-plugin-vue-inspector'
import { DIR_CLIENT } from './dir'
import { getRpcFunctions } from './rpc'
import { removeUrlQuery } from './utils'

function getVueDevtoolsPath() {
const pluginPath = normalizePath(path.dirname(fileURLToPath(import.meta.url)))
Expand All @@ -26,6 +28,8 @@ function normalizeComboKeyPrint(toggleComboKey: string) {
return toggleComboKey.split('-').map(key => toggleComboKeysMap[key] || key[0].toUpperCase() + key.slice(1)).join(dim('+'))
}

const devtoolsNextResourceSymbol = '?__vue-devtools-next-resource'

export interface VitePluginVueDevToolsOptions {
/**
* append an import to the module id ending with `appendTo` instead of adding a script into body
Expand Down Expand Up @@ -118,6 +122,10 @@ export default function VitePluginVueDevTools(options?: VitePluginVueDevToolsOpt
console.log(` ${green('➜')} ${bold('Vue DevTools')}: ${green(`Press ${yellow(keys)} in App to toggle the Vue DevTools`)}\n`)
}
}

const devtoolsOptionsImportee = 'virtual:vue-devtools-options'
const resolvedDevtoolsOptions = `\0${devtoolsOptionsImportee}`

const plugin = <PluginOption>{
name: 'vite-plugin-vue-devtools',
enforce: 'pre',
Expand All @@ -129,17 +137,26 @@ export default function VitePluginVueDevTools(options?: VitePluginVueDevToolsOpt
configureServer(server)
},
async resolveId(importee: string) {
if (importee.startsWith('virtual:vue-devtools-options')) {
return importee
if (importee === devtoolsOptionsImportee) {
return resolvedDevtoolsOptions
}
// Why use query instead of vite virtual module on devtools resource?
// Devtools resource will import `@vue/devtools-core` and other packages, which vite cannot analysis correctly on virtual module.
// So we should use absolute path + `query` to mark the resource as devtools resource.
else if (importee.startsWith('virtual:vue-devtools-path:')) {
const resolved = importee.replace('virtual:vue-devtools-path:', `${vueDevtoolsPath}/`)
return resolved
return `${resolved}${devtoolsNextResourceSymbol}`
}
},
async load(id) {
if (id === 'virtual:vue-devtools-options')
if (id === resolvedDevtoolsOptions) {
return `export default ${JSON.stringify({ base: config.base, componentInspector: pluginOptions.componentInspector })}`
}
else if (id.endsWith(devtoolsNextResourceSymbol)) {
const filename = removeUrlQuery(id)
// read file ourselves to avoid getting shut out by vite's fs.allow check
return await fs.promises.readFile(filename, 'utf-8')
}
},
transform(code, id, options) {
if (options?.ssr)
Expand Down

0 comments on commit b45bf94

Please sign in to comment.