@@ -131,31 +131,23 @@ async function doTransform(
131
131
url = removeTimestampQuery ( url )
132
132
133
133
const { config, pluginContainer } = server
134
- const prettyUrl = debugCache ? prettifyUrl ( url , config . root ) : ''
135
134
const ssr = ! ! options . ssr
136
135
137
136
if ( ssr && isDepsOptimizerEnabled ( config , true ) ) {
138
137
await initDevSsrDepsOptimizer ( config , server )
139
138
}
140
139
141
- const module = await server . moduleGraph . getModuleByUrl ( url , ssr )
142
-
143
- // tries to handle soft invalidation of the module if available,
144
- // returns a boolean true is successful, or false if no handling is needed
145
- const softInvalidatedTransformResult =
146
- module &&
147
- ( await handleModuleSoftInvalidation ( module , ssr , timestamp , server ) )
148
- if ( softInvalidatedTransformResult ) {
149
- debugCache ?.( `[memory-hmr] ${ prettyUrl } ` )
150
- return softInvalidatedTransformResult
151
- }
152
-
153
- // check if we have a fresh cache
154
- const cached =
155
- module && ( ssr ? module . ssrTransformResult : module . transformResult )
156
- if ( cached ) {
157
- debugCache ?.( `[memory] ${ prettyUrl } ` )
158
- return cached
140
+ let module = await server . moduleGraph . getModuleByUrl ( url , ssr )
141
+ if ( module ) {
142
+ // try use cache from url
143
+ const cached = await getCachedTransformResult (
144
+ url ,
145
+ module ,
146
+ server ,
147
+ ssr ,
148
+ timestamp ,
149
+ )
150
+ if ( cached ) return cached
159
151
}
160
152
161
153
const resolved = module
@@ -165,6 +157,21 @@ async function doTransform(
165
157
// resolve
166
158
const id = module ?. id ?? resolved ?. id ?? url
167
159
160
+ module ??= server . moduleGraph . getModuleById ( id )
161
+ if ( module ) {
162
+ // if a different url maps to an existing loaded id, make sure we relate this url to the id
163
+ await server . moduleGraph . _ensureEntryFromUrl ( url , ssr , undefined , resolved )
164
+ // try use cache from id
165
+ const cached = await getCachedTransformResult (
166
+ url ,
167
+ module ,
168
+ server ,
169
+ ssr ,
170
+ timestamp ,
171
+ )
172
+ if ( cached ) return cached
173
+ }
174
+
168
175
const result = loadAndTransform (
169
176
id ,
170
177
url ,
@@ -180,6 +187,34 @@ async function doTransform(
180
187
return result
181
188
}
182
189
190
+ async function getCachedTransformResult (
191
+ url : string ,
192
+ module : ModuleNode ,
193
+ server : ViteDevServer ,
194
+ ssr : boolean ,
195
+ timestamp : number ,
196
+ ) {
197
+ const prettyUrl = debugCache ? prettifyUrl ( url , server . config . root ) : ''
198
+
199
+ // tries to handle soft invalidation of the module if available,
200
+ // returns a boolean true is successful, or false if no handling is needed
201
+ const softInvalidatedTransformResult =
202
+ module &&
203
+ ( await handleModuleSoftInvalidation ( module , ssr , timestamp , server ) )
204
+ if ( softInvalidatedTransformResult ) {
205
+ debugCache ?.( `[memory-hmr] ${ prettyUrl } ` )
206
+ return softInvalidatedTransformResult
207
+ }
208
+
209
+ // check if we have a fresh cache
210
+ const cached =
211
+ module && ( ssr ? module . ssrTransformResult : module . transformResult )
212
+ if ( cached ) {
213
+ debugCache ?.( `[memory] ${ prettyUrl } ` )
214
+ return cached
215
+ }
216
+ }
217
+
183
218
async function loadAndTransform (
184
219
id : string ,
185
220
url : string ,
0 commit comments