Skip to content

Commit

Permalink
fix: open-in-editor base url, deprecated openInEditorHost option (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz authored Apr 14, 2024
1 parent ff5178f commit 738b781
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ interface VitePluginInspectorOptions {
appendTo?: string | RegExp

/**
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
*/
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
*/
openInEditorHost?: string | false

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ interface VitePluginInspectorOptions {
appendTo?: string | RegExp

/**
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
*/
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
*
*/
openInEditorHost?: string | false
}
```
Expand Down
21 changes: 11 additions & 10 deletions packages/core/src/Overlay.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<script>
import inspectorOptions from 'virtual:vue-inspector-options'
const isClient = typeof window !== 'undefined'
const importMetaUrl = isClient ? new URL(import.meta.url) : {}
const protocol = inspectorOptions.serverOptions?.https ? 'https:' : importMetaUrl?.protocol
const hostOpts = inspectorOptions.serverOptions?.host
const host = (hostOpts && hostOpts !== true) ? hostOpts : importMetaUrl?.hostname
const port = (hostOpts && hostOpts !== true) ? inspectorOptions.serverOptions?.port : importMetaUrl?.port
const baseUrl = isClient ? (inspectorOptions.openInEditorHost || `${protocol}//${host}:${port}`) : ''
const base = inspectorOptions.base
const KEY_DATA = 'data-v-inspector'
const KEY_IGNORE = 'data-v-inspector-ignore'
Expand Down Expand Up @@ -173,7 +168,11 @@ export default {
e.stopImmediatePropagation()
const { file, line, column } = params
this.overlayVisible = false
this.openInEditor(baseUrl, file, line, column)
const url = new URL(
`${base}__open-in-editor?file=${encodeURIComponent(`${file}:${line}:${column}`)}`,
import.meta.url,
)
this.openInEditor(url)
},
updateLinkParams(e) {
const { targetNode, params } = this.getTargetNode(e)
Expand Down Expand Up @@ -215,9 +214,11 @@ export default {
/**
* Vite built-in support
* https://github.com/vitejs/vite/blob/d59e1acc2efc0307488364e9f2fad528ec57f204/packages/vite/src/node/server/index.ts#L569-L570
* */
*/
const _url = baseUrl instanceof URL ? baseUrl : `${baseUrl}/__open-in-editor?file=${file}:${line}:${column}`
const promise = fetch(
`${baseUrl}/__open-in-editor?file=${file}:${line}:${column}`,
_url,
{
mode: 'no-cors',
},
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
import { bold, dim, green, yellow } from 'kolorist'
import { normalizePath } from 'vite'
import type { PluginOption, ResolvedConfig, ServerOptions } from 'vite'
import type { PluginOption, ResolvedConfig } from 'vite'
import MagicString from 'magic-string'
import { compileSFCTemplate } from './compiler'
import { idToFile, parseVueRequest } from './utils'
Expand Down Expand Up @@ -75,6 +75,7 @@ export interface VitePluginInspectorOptions {
/**
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
*/
openInEditorHost?: string | false

Expand Down Expand Up @@ -122,7 +123,6 @@ export const DEFAULT_INSPECTOR_OPTIONS: VitePluginInspectorOptions = {
toggleButtonVisibility: 'active',
toggleButtonPos: 'top-right',
appendTo: '',
openInEditorHost: false,
lazyLoad: false,
} as const

Expand All @@ -132,7 +132,6 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
...DEFAULT_INSPECTOR_OPTIONS,
...options,
}
let serverOptions: ServerOptions | undefined
let config: ResolvedConfig

const {
Expand Down Expand Up @@ -161,7 +160,7 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE

async load(id) {
if (id === 'virtual:vue-inspector-options') {
return `export default ${JSON.stringify({ ...normalizedOptions, serverOptions })}`
return `export default ${JSON.stringify({ ...normalizedOptions, base: config.base })}`
}
else if (id.startsWith(inspectorPath)) {
const { query } = parseVueRequest(id)
Expand Down Expand Up @@ -220,7 +219,6 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
},
configResolved(resolvedConfig) {
config = resolvedConfig
serverOptions = resolvedConfig.server
},
},
{
Expand Down
1 change: 0 additions & 1 deletion packages/playground/vue3/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default defineConfig({
VueJsx(),
Inspector({
enabled: true,
openInEditorHost: 'http://localhost:5173',
toggleButtonVisibility: 'always',
}),
Inspect(),
Expand Down
8 changes: 5 additions & 3 deletions packages/unplugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ interface VitePluginInspectorOptions {
appendTo?: string | RegExp

/**
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
*/
* Customize openInEditor host (e.g. http://localhost:3000)
* @default false
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
*
*/
openInEditorHost?: string | false
}
```
Expand Down

0 comments on commit 738b781

Please sign in to comment.