Skip to content

Commit b3e0055

Browse files
committed
fix: respect server.origin when provided
1 parent 154ca0f commit b3e0055

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

.changeset/grumpy-pumas-tie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vite-plugin-checker': patch
3+
---
4+
5+
Should respect `server.origin` when it's provided.

packages/vite-plugin-checker/src/client/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import fs from 'fs'
22
import { createRequire } from 'module'
3+
import type { SharedConfig } from '../types'
34
const _require = createRequire(import.meta.url)
45

56
export const RUNTIME_CLIENT_RUNTIME_PATH = '/@vite-plugin-checker-runtime'
67
export const RUNTIME_CLIENT_ENTRY_PATH = '/@vite-plugin-checker-runtime-entry'
78

89
export const wrapVirtualPrefix = (id: `/${string}`): `virtual:${string}` =>
910
`virtual:${id.slice('/'.length)}`
10-
export const composePreambleCode = (base = '/', config: Record<string, any>) => `
11-
import { inject } from "${base}${RUNTIME_CLIENT_RUNTIME_PATH.slice(1)}";
11+
export const composePreambleCode = ({
12+
baseWithOrigin = '/',
13+
overlayConfig,
14+
}: {
15+
baseWithOrigin: string
16+
overlayConfig: SharedConfig['overlay']
17+
}) => `
18+
import { inject } from "${baseWithOrigin}${RUNTIME_CLIENT_RUNTIME_PATH.slice(1)}";
1219
inject({
13-
overlayConfig: ${JSON.stringify(config)},
14-
base: "${base}",
20+
overlayConfig: ${JSON.stringify(overlayConfig)},
21+
base: "${baseWithOrigin}",
1522
});
1623
`
1724

packages/vite-plugin-checker/src/main.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
type SharedConfig,
2525
type UserPluginConfig,
2626
} from './types.js'
27-
2827
import type { ConfigEnv, Plugin, Logger } from 'vite'
28+
2929
const sharedConfigKeys: (keyof SharedConfig)[] = ['enableBuild', 'overlay']
3030
const buildInCheckerKeys: BuildInCheckerNames[] = [
3131
'typescript',
@@ -63,7 +63,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
6363
let initializeCounter = 0
6464
let checkers: ServeAndBuildChecker[] = []
6565
let isProduction = false
66-
let devBase = '/'
66+
let baseWithOrigin: string
6767
let viteMode: ConfigEnv['command'] | undefined
6868
let buildWatch = false
6969
let logger: Logger | null = null
@@ -99,7 +99,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
9999
},
100100
configResolved(config) {
101101
logger = config.logger
102-
devBase = config.base
102+
baseWithOrigin = config.server.origin ? config.server.origin + config.base : config.base
103103
isProduction ||= config.isProduction || config.command === 'build'
104104
buildWatch = !!config.build.watch
105105
},
@@ -126,7 +126,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
126126
}
127127

128128
if (id === wrapVirtualPrefix(RUNTIME_CLIENT_ENTRY_PATH)) {
129-
return composePreambleCode(devBase, overlayConfig)
129+
return composePreambleCode({ baseWithOrigin, overlayConfig })
130130
}
131131

132132
return
@@ -139,7 +139,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
139139
{
140140
tag: 'script',
141141
attrs: { type: 'module' },
142-
children: composePreambleCode(devBase, overlayConfig),
142+
children: composePreambleCode({ baseWithOrigin, overlayConfig }),
143143
},
144144
]
145145
},

0 commit comments

Comments
 (0)