@@ -2,7 +2,6 @@ import chalk from 'chalk'
2
2
import { spawn } from 'child_process'
3
3
import pick from 'lodash.pick'
4
4
import npmRunPath from 'npm-run-path'
5
- import path from 'path'
6
5
import type { ConfigEnv , Plugin , ResolvedConfig } from 'vite'
7
6
import { Checker } from './Checker.js'
8
7
import {
@@ -59,6 +58,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
59
58
let checkers : ServeAndBuildChecker [ ] = [ ]
60
59
let isProduction = true
61
60
let skipRuntime = false
61
+ let devBase = '/'
62
62
63
63
let viteMode : ConfigEnv [ 'command' ] | undefined
64
64
let resolvedConfig : ResolvedConfig | undefined
@@ -87,6 +87,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
87
87
} ,
88
88
configResolved ( config ) {
89
89
resolvedConfig = config
90
+ devBase = config . base
90
91
isProduction = config . isProduction
91
92
skipRuntime ||= isProduction || config . command === 'build'
92
93
} ,
@@ -107,37 +108,11 @@ export function checker(userConfig: UserPluginConfig): Plugin {
107
108
} ,
108
109
load ( id ) {
109
110
if ( id === RUNTIME_CLIENT_RUNTIME_PATH ) {
110
- if ( ! resolvedConfig ) return
111
-
112
- const devBase = resolvedConfig . base
113
-
114
- // #region
115
- // copied from https://github.com/vitejs/vite/blob/d76db0cae645beaecd970d95b4819158c5dd568a/packages/vite/src/client/client.ts#LL25
116
- const hmrConfig = isObject ( resolvedConfig . server . hmr ) ? resolvedConfig . server . hmr : { }
117
- const host = hmrConfig . host || null
118
- const protocol = hmrConfig . protocol || null
119
- // hmr.clientPort -> hmr.port
120
- // -> (24678 if middleware mode) -> new URL(import.meta.url).port
121
- let port = hmrConfig ?. clientPort || hmrConfig ?. port || null
122
- if ( resolvedConfig . server . middlewareMode ) {
123
- port ||= 24678
124
- }
125
-
126
- let hmrBase = devBase
127
- if ( hmrConfig ?. path ) {
128
- hmrBase = path . posix . join ( hmrBase , hmrConfig . path )
129
- }
130
-
131
111
return runtimeCode
132
- . replace ( / _ _ H M R _ P R O T O C O L _ _ / g, JSON . stringify ( protocol ) )
133
- . replace ( / _ _ H M R _ H O S T N A M E _ _ / g, JSON . stringify ( host ) )
134
- . replace ( / _ _ H M R _ P O R T _ _ / g, JSON . stringify ( port ) )
135
- . replace ( / _ _ H M R _ B A S E _ _ / g, JSON . stringify ( hmrBase ) )
136
- // #endregion
137
112
}
138
113
139
114
if ( id === RUNTIME_CLIENT_ENTRY_PATH ) {
140
- return composePreambleCode ( resolvedConfig ! . base , overlayConfig )
115
+ return composePreambleCode ( devBase , overlayConfig )
141
116
}
142
117
143
118
return
@@ -179,7 +154,6 @@ export function checker(userConfig: UserPluginConfig): Plugin {
179
154
} ) ( )
180
155
} ,
181
156
configureServer ( server ) {
182
- let connectedTimes = 0
183
157
let latestOverlayErrors : OverlayErrorAction [ 'payload' ] [ ] = new Array ( checkers . length )
184
158
// for dev mode (2/2)
185
159
// Get the server instance and keep reference in a closure
@@ -190,7 +164,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
190
164
if ( action . type === ACTION_TYPES . overlayError ) {
191
165
latestOverlayErrors [ index ] = action . payload
192
166
if ( action . payload ) {
193
- server . ws . send ( action . payload )
167
+ server . ws . send ( 'vite-plugin-checker' , action . payload )
194
168
}
195
169
} else if ( action . type === ACTION_TYPES . console ) {
196
170
Checker . log ( action )
@@ -204,15 +178,11 @@ export function checker(userConfig: UserPluginConfig): Plugin {
204
178
// may update the overlay before full-reload fired. So we make sure the overlay
205
179
// will be displayed again after full-reload.
206
180
server . ws . on ( 'connection' , ( ) => {
207
- connectedTimes ++
208
- // if connectedCount !== 1, means Vite is doing a full-reload, so we don't need to send overlay again
209
- if ( connectedTimes > 1 ) {
210
- server . ws . send ( {
211
- type : 'custom' ,
212
- event : WS_CHECKER_RECONNECT_EVENT ,
213
- data : latestOverlayErrors . filter ( Boolean ) ,
214
- } )
215
- }
181
+ server . ws . send ( 'vite-plugin-checker' , {
182
+ type : 'custom' ,
183
+ event : WS_CHECKER_RECONNECT_EVENT ,
184
+ data : latestOverlayErrors . filter ( Boolean ) ,
185
+ } )
216
186
} )
217
187
} else {
218
188
setTimeout ( ( ) => {
@@ -266,8 +236,4 @@ function spawnChecker(
266
236
} )
267
237
}
268
238
269
- function isObject ( value : unknown ) : value is Record < string , any > {
270
- return Object . prototype . toString . call ( value ) === '[object Object]'
271
- }
272
-
273
239
export default checker
0 commit comments