Skip to content

Commit 738b781

Browse files
authored
fix: open-in-editor base url, deprecated openInEditorHost option (#86)
1 parent ff5178f commit 738b781

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ interface VitePluginInspectorOptions {
134134
appendTo?: string | RegExp
135135

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

142143
/**

packages/core/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ interface VitePluginInspectorOptions {
134134
appendTo?: string | RegExp
135135

136136
/**
137-
* Customize openInEditor host (e.g. http://localhost:3000)
138-
* @default false
139-
*/
137+
* Customize openInEditor host (e.g. http://localhost:3000)
138+
* @default false
139+
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
140+
*
141+
*/
140142
openInEditorHost?: string | false
141143
}
142144
```

packages/core/src/Overlay.vue

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<script>
22
import inspectorOptions from 'virtual:vue-inspector-options'
3-
const isClient = typeof window !== 'undefined'
4-
const importMetaUrl = isClient ? new URL(import.meta.url) : {}
5-
const protocol = inspectorOptions.serverOptions?.https ? 'https:' : importMetaUrl?.protocol
6-
const hostOpts = inspectorOptions.serverOptions?.host
7-
const host = (hostOpts && hostOpts !== true) ? hostOpts : importMetaUrl?.hostname
8-
const port = (hostOpts && hostOpts !== true) ? inspectorOptions.serverOptions?.port : importMetaUrl?.port
9-
const baseUrl = isClient ? (inspectorOptions.openInEditorHost || `${protocol}//${host}:${port}`) : ''
3+
4+
const base = inspectorOptions.base
105
116
const KEY_DATA = 'data-v-inspector'
127
const KEY_IGNORE = 'data-v-inspector-ignore'
@@ -173,7 +168,11 @@ export default {
173168
e.stopImmediatePropagation()
174169
const { file, line, column } = params
175170
this.overlayVisible = false
176-
this.openInEditor(baseUrl, file, line, column)
171+
const url = new URL(
172+
`${base}__open-in-editor?file=${encodeURIComponent(`${file}:${line}:${column}`)}`,
173+
import.meta.url,
174+
)
175+
this.openInEditor(url)
177176
},
178177
updateLinkParams(e) {
179178
const { targetNode, params } = this.getTargetNode(e)
@@ -215,9 +214,11 @@ export default {
215214
/**
216215
* Vite built-in support
217216
* https://github.com/vitejs/vite/blob/d59e1acc2efc0307488364e9f2fad528ec57f204/packages/vite/src/node/server/index.ts#L569-L570
218-
* */
217+
*/
218+
219+
const _url = baseUrl instanceof URL ? baseUrl : `${baseUrl}/__open-in-editor?file=${file}:${line}:${column}`
219220
const promise = fetch(
220-
`${baseUrl}/__open-in-editor?file=${file}:${line}:${column}`,
221+
_url,
221222
{
222223
mode: 'no-cors',
223224
},

packages/core/src/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
33
import fs from 'node:fs'
44
import { bold, dim, green, yellow } from 'kolorist'
55
import { normalizePath } from 'vite'
6-
import type { PluginOption, ResolvedConfig, ServerOptions } from 'vite'
6+
import type { PluginOption, ResolvedConfig } from 'vite'
77
import MagicString from 'magic-string'
88
import { compileSFCTemplate } from './compiler'
99
import { idToFile, parseVueRequest } from './utils'
@@ -75,6 +75,7 @@ export interface VitePluginInspectorOptions {
7575
/**
7676
* Customize openInEditor host (e.g. http://localhost:3000)
7777
* @default false
78+
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
7879
*/
7980
openInEditorHost?: string | false
8081

@@ -122,7 +123,6 @@ export const DEFAULT_INSPECTOR_OPTIONS: VitePluginInspectorOptions = {
122123
toggleButtonVisibility: 'active',
123124
toggleButtonPos: 'top-right',
124125
appendTo: '',
125-
openInEditorHost: false,
126126
lazyLoad: false,
127127
} as const
128128

@@ -132,7 +132,6 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
132132
...DEFAULT_INSPECTOR_OPTIONS,
133133
...options,
134134
}
135-
let serverOptions: ServerOptions | undefined
136135
let config: ResolvedConfig
137136

138137
const {
@@ -161,7 +160,7 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
161160

162161
async load(id) {
163162
if (id === 'virtual:vue-inspector-options') {
164-
return `export default ${JSON.stringify({ ...normalizedOptions, serverOptions })}`
163+
return `export default ${JSON.stringify({ ...normalizedOptions, base: config.base })}`
165164
}
166165
else if (id.startsWith(inspectorPath)) {
167166
const { query } = parseVueRequest(id)
@@ -220,7 +219,6 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
220219
},
221220
configResolved(resolvedConfig) {
222221
config = resolvedConfig
223-
serverOptions = resolvedConfig.server
224222
},
225223
},
226224
{

packages/playground/vue3/vite.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default defineConfig({
1010
VueJsx(),
1111
Inspector({
1212
enabled: true,
13-
openInEditorHost: 'http://localhost:5173',
1413
toggleButtonVisibility: 'always',
1514
}),
1615
Inspect(),

packages/unplugin/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ interface VitePluginInspectorOptions {
134134
appendTo?: string | RegExp
135135

136136
/**
137-
* Customize openInEditor host (e.g. http://localhost:3000)
138-
* @default false
139-
*/
137+
* Customize openInEditor host (e.g. http://localhost:3000)
138+
* @default false
139+
* @deprecated This option is deprecated and removed in 5.0. The plugin now automatically detects the correct host.
140+
*
141+
*/
140142
openInEditorHost?: string | false
141143
}
142144
```

0 commit comments

Comments
 (0)