@@ -48,6 +48,13 @@ export function transformMiddleware(
48
48
server : ViteDevServer ,
49
49
) : Connect . NextHandleFunction {
50
50
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
51
+
52
+ // check if public dir is inside root dir
53
+ const { root } = server . config
54
+ const publicDir = normalizePath ( server . config . publicDir )
55
+ const publicDirInRoot = publicDir . startsWith ( withTrailingSlash ( root ) )
56
+ const publicPath = `${ publicDir . slice ( root . length ) } /`
57
+
51
58
return async function viteTransformMiddleware ( req , res , next ) {
52
59
if ( req . method !== 'GET' || knownIgnoreList . has ( req . url ! ) ) {
53
60
return next ( )
@@ -123,43 +130,8 @@ export function transformMiddleware(
123
130
}
124
131
}
125
132
126
- // check if public dir is inside root dir
127
- const publicDir = normalizePath ( server . config . publicDir )
128
- const rootDir = normalizePath ( server . config . root )
129
- if ( publicDir . startsWith ( withTrailingSlash ( rootDir ) ) ) {
130
- const publicPath = `${ publicDir . slice ( rootDir . length ) } /`
131
- // warn explicit public paths
132
- if ( url . startsWith ( withTrailingSlash ( publicPath ) ) ) {
133
- let warning : string
134
-
135
- if ( isImportRequest ( url ) ) {
136
- const rawUrl = removeImportQuery ( url )
137
- if ( urlRE . test ( url ) ) {
138
- warning =
139
- `Assets in the public directory are served at the root path.\n` +
140
- `Instead of ${ colors . cyan ( rawUrl ) } , use ${ colors . cyan (
141
- rawUrl . replace ( publicPath , '/' ) ,
142
- ) } .`
143
- } else {
144
- warning =
145
- 'Assets in public directory cannot be imported from JavaScript.\n' +
146
- `If you intend to import that asset, put the file in the src directory, and use ${ colors . cyan (
147
- rawUrl . replace ( publicPath , '/src/' ) ,
148
- ) } instead of ${ colors . cyan ( rawUrl ) } .\n` +
149
- `If you intend to use the URL of that asset, use ${ colors . cyan (
150
- injectQuery ( rawUrl . replace ( publicPath , '/' ) , 'url' ) ,
151
- ) } .`
152
- }
153
- } else {
154
- warning =
155
- `Files in the public directory are served at the root path.\n` +
156
- `Instead of ${ colors . cyan ( url ) } , use ${ colors . cyan (
157
- url . replace ( publicPath , '/' ) ,
158
- ) } .`
159
- }
160
-
161
- server . config . logger . warn ( colors . yellow ( warning ) )
162
- }
133
+ if ( publicDirInRoot && url . startsWith ( publicPath ) ) {
134
+ warnAboutExplicitPublicPathInUrl ( url )
163
135
}
164
136
165
137
if (
@@ -265,4 +237,36 @@ export function transformMiddleware(
265
237
266
238
next ( )
267
239
}
240
+
241
+ function warnAboutExplicitPublicPathInUrl ( url : string ) {
242
+ let warning : string
243
+
244
+ if ( isImportRequest ( url ) ) {
245
+ const rawUrl = removeImportQuery ( url )
246
+ if ( urlRE . test ( url ) ) {
247
+ warning =
248
+ `Assets in the public directory are served at the root path.\n` +
249
+ `Instead of ${ colors . cyan ( rawUrl ) } , use ${ colors . cyan (
250
+ rawUrl . replace ( publicPath , '/' ) ,
251
+ ) } .`
252
+ } else {
253
+ warning =
254
+ 'Assets in public directory cannot be imported from JavaScript.\n' +
255
+ `If you intend to import that asset, put the file in the src directory, and use ${ colors . cyan (
256
+ rawUrl . replace ( publicPath , '/src/' ) ,
257
+ ) } instead of ${ colors . cyan ( rawUrl ) } .\n` +
258
+ `If you intend to use the URL of that asset, use ${ colors . cyan (
259
+ injectQuery ( rawUrl . replace ( publicPath , '/' ) , 'url' ) ,
260
+ ) } .`
261
+ }
262
+ } else {
263
+ warning =
264
+ `Files in the public directory are served at the root path.\n` +
265
+ `Instead of ${ colors . cyan ( url ) } , use ${ colors . cyan (
266
+ url . replace ( publicPath , '/' ) ,
267
+ ) } .`
268
+ }
269
+
270
+ server . config . logger . warn ( colors . yellow ( warning ) )
271
+ }
268
272
}
0 commit comments