@@ -114,6 +114,10 @@ function shouldPreTransform(url: string, config: ResolvedConfig) {
114
114
115
115
const wordCharRE = / \w /
116
116
117
+ function isBareRelative ( url : string ) {
118
+ return wordCharRE . test ( url [ 0 ] ) && ! url . includes ( ':' )
119
+ }
120
+
117
121
const isSrcSet = ( attr : Token . Attribute ) =>
118
122
attr . name === 'srcset' && attr . prefix === undefined
119
123
const processNodeUrl = (
@@ -143,20 +147,30 @@ const processNodeUrl = (
143
147
// rewrite `./index.js` -> `localhost:5173/a/index.js`.
144
148
// rewrite `../index.js` -> `localhost:5173/index.js`.
145
149
// rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
146
- ( ( url [ 0 ] === '.' || ( wordCharRE . test ( url [ 0 ] ) && ! url . includes ( ':' ) ) ) &&
150
+ ( ( url [ 0 ] === '.' || isBareRelative ( url ) ) &&
147
151
originalUrl &&
148
152
originalUrl !== '/' &&
149
153
htmlPath === '/index.html' )
150
154
) {
151
- const devBase = config . base
152
- const fullUrl = path . posix . join ( devBase , url )
153
- if ( server && shouldPreTransform ( url , config ) ) {
154
- preTransformRequest ( server , fullUrl , devBase )
155
+ url = path . posix . join ( config . base , url )
156
+ }
157
+
158
+ if ( server && shouldPreTransform ( url , config ) ) {
159
+ let preTransformUrl : string | undefined
160
+ if ( url [ 0 ] === '/' ) {
161
+ preTransformUrl = url
162
+ } else if ( url [ 0 ] === '.' || isBareRelative ( url ) ) {
163
+ preTransformUrl = path . posix . join (
164
+ config . base ,
165
+ path . posix . dirname ( htmlPath ) ,
166
+ url ,
167
+ )
168
+ }
169
+ if ( preTransformUrl ) {
170
+ preTransformRequest ( server , preTransformUrl , config . base )
155
171
}
156
- return fullUrl
157
- } else {
158
- return url
159
172
}
173
+ return url
160
174
}
161
175
162
176
const processedUrl = useSrcSetReplacer
0 commit comments