@@ -19,7 +19,8 @@ import { hashTransform, propsToFilename } from './utils/transformToPath.js';
19
19
20
20
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID ;
21
21
22
- const assetRegex = new RegExp ( `\\.(${ VALID_INPUT_FORMATS . join ( '|' ) } )$` , 'i' ) ;
22
+ const assetRegex = new RegExp ( `\\.(${ VALID_INPUT_FORMATS . join ( '|' ) } )` , 'i' ) ;
23
+ const assetRegexEnds = new RegExp ( `\\.(${ VALID_INPUT_FORMATS . join ( '|' ) } )$` , 'i' ) ;
23
24
24
25
export default function assets ( {
25
26
settings,
@@ -81,7 +82,7 @@ export default function assets({
81
82
return ;
82
83
}
83
84
84
- globalThis . astroAsset . addStaticImage = ( options , hashProperties ) => {
85
+ globalThis . astroAsset . addStaticImage = ( options , hashProperties , originalPath ) => {
85
86
if ( ! globalThis . astroAsset . staticImages ) {
86
87
globalThis . astroAsset . staticImages = new Map <
87
88
string ,
@@ -97,11 +98,6 @@ export default function assets({
97
98
isESMImportedImage ( options . src ) ? options . src . src : options . src
98
99
) . replace ( settings . config . build . assetsPrefix || '' , '' ) ;
99
100
100
- // This, however, is the real original path, in `src` and all.
101
- const originalSrcPath = isESMImportedImage ( options . src )
102
- ? options . src . fsPath
103
- : options . src ;
104
-
105
101
const hash = hashTransform (
106
102
options ,
107
103
settings . config . image . service . entrypoint ,
@@ -120,7 +116,7 @@ export default function assets({
120
116
121
117
if ( ! transformsForPath ) {
122
118
globalThis . astroAsset . staticImages . set ( finalOriginalImagePath , {
123
- originalSrcPath : originalSrcPath ,
119
+ originalSrcPath : originalPath ,
124
120
transforms : new Map ( ) ,
125
121
} ) ;
126
122
transformsForPath = globalThis . astroAsset . staticImages . get ( finalOriginalImagePath ) ! ;
@@ -178,15 +174,25 @@ export default function assets({
178
174
resolvedConfig = viteConfig ;
179
175
} ,
180
176
async load ( id , options ) {
181
- // If our import has any query params, we'll let Vite handle it
182
- // See https://github.com/withastro/astro/issues/8333
183
- if ( id !== removeQueryString ( id ) ) {
184
- return ;
185
- }
186
177
if ( assetRegex . test ( id ) ) {
187
- const meta = await emitESMImage ( id , this . meta . watchMode , this . emitFile ) ;
178
+ if ( ! globalThis . astroAsset . referencedImages )
179
+ globalThis . astroAsset . referencedImages = new Set ( ) ;
180
+
181
+ if ( id !== removeQueryString ( id ) ) {
182
+ // If our import has any query params, we'll let Vite handle it, nonetheless we'll make sure to not delete it
183
+ // See https://github.com/withastro/astro/issues/8333
184
+ globalThis . astroAsset . referencedImages . add ( removeQueryString ( id ) ) ;
185
+ return ;
186
+ }
188
187
189
- if ( ! meta ) {
188
+ // If the requested ID doesn't end with a valid image extension, we'll let Vite handle it
189
+ if ( ! assetRegexEnds . test ( id ) ) {
190
+ return ;
191
+ }
192
+
193
+ const imageMetadata = await emitESMImage ( id , this . meta . watchMode , this . emitFile ) ;
194
+
195
+ if ( ! imageMetadata ) {
190
196
throw new AstroError ( {
191
197
...AstroErrorData . ImageNotFound ,
192
198
message : AstroErrorData . ImageNotFound . message ( id ) ,
@@ -197,13 +203,13 @@ export default function assets({
197
203
// Since you cannot use image optimization on the client anyway, it's safe to assume that if the user imported
198
204
// an image on the client, it should be present in the final build.
199
205
if ( options ?. ssr ) {
200
- return `export default ${ getProxyCode ( meta , isServerLikeOutput ( settings . config ) ) } ` ;
206
+ return `export default ${ getProxyCode (
207
+ imageMetadata ,
208
+ isServerLikeOutput ( settings . config )
209
+ ) } `;
201
210
} else {
202
- if ( ! globalThis . astroAsset . referencedImages )
203
- globalThis . astroAsset . referencedImages = new Set ( ) ;
204
-
205
- globalThis . astroAsset . referencedImages . add ( meta . fsPath ) ;
206
- return `export default ${ JSON . stringify ( meta ) } ` ;
211
+ globalThis . astroAsset . referencedImages . add ( imageMetadata . fsPath ) ;
212
+ return `export default ${ JSON . stringify ( imageMetadata ) } ` ;
207
213
}
208
214
}
209
215
} ,
0 commit comments