@@ -117,32 +117,34 @@ export default function assets({ settings }: { settings: AstroSettings }): vite.
117
117
} ,
118
118
load ( id ) {
119
119
if ( id === resolvedVirtualModuleId ) {
120
- return /* ts */ `
121
- export { getConfiguredImageService, isLocalService } from "astro/assets";
122
- import { getImage as getImageInternal } from "astro/assets";
123
- export { default as Image } from "astro/components/${ imageComponentPrefix } Image.astro";
124
- export { default as Picture } from "astro/components/${ imageComponentPrefix } Picture.astro";
125
- export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";
126
-
127
- export const imageConfig = ${ JSON . stringify ( { ...settings . config . image , experimentalResponsiveImages : settings . config . experimental . responsiveImages } ) } ;
128
- // This is used by the @astrojs/node integration to locate images.
129
- // It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
130
- // new URL("dist/...") is interpreted by the bundler as a signal to include that directory
131
- // in the Lambda bundle, which would bloat the bundle with images.
132
- // To prevent this, we mark the URL construction as pure,
133
- // so that it's tree-shaken away for all platforms that don't need it.
134
- export const outDir = /* #__PURE__ */ new URL(${ JSON . stringify (
135
- new URL (
136
- settings . buildOutput === 'server'
137
- ? settings . config . build . client
138
- : settings . config . outDir ,
139
- ) ,
140
- ) } );
141
- export const assetsDir = /* #__PURE__ */ new URL(${ JSON . stringify (
142
- settings . config . build . assets ,
143
- ) } , outDir);
144
- export const getImage = async (options) => await getImageInternal(options, imageConfig);
145
- ` ;
120
+ return {
121
+ code : `
122
+ export { getConfiguredImageService, isLocalService } from "astro/assets";
123
+ import { getImage as getImageInternal } from "astro/assets";
124
+ export { default as Image } from "astro/components/${ imageComponentPrefix } Image.astro";
125
+ export { default as Picture } from "astro/components/${ imageComponentPrefix } Picture.astro";
126
+ export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";
127
+
128
+ export const imageConfig = ${ JSON . stringify ( { ...settings . config . image , experimentalResponsiveImages : settings . config . experimental . responsiveImages } ) } ;
129
+ // This is used by the @astrojs/node integration to locate images.
130
+ // It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
131
+ // new URL("dist/...") is interpreted by the bundler as a signal to include that directory
132
+ // in the Lambda bundle, which would bloat the bundle with images.
133
+ // To prevent this, we mark the URL construction as pure,
134
+ // so that it's tree-shaken away for all platforms that don't need it.
135
+ export const outDir = /* #__PURE__ */ new URL(${ JSON . stringify (
136
+ new URL (
137
+ settings . buildOutput === 'server'
138
+ ? settings . config . build . client
139
+ : settings . config . outDir ,
140
+ ) ,
141
+ ) } );
142
+ export const assetsDir = /* #__PURE__ */ new URL(${ JSON . stringify (
143
+ settings . config . build . assets ,
144
+ ) } , outDir);
145
+ export const getImage = async (options) => await getImageInternal(options, imageConfig);
146
+ ` ,
147
+ } ;
146
148
}
147
149
} ,
148
150
buildStart ( ) {
@@ -151,7 +153,7 @@ export default function assets({ settings }: { settings: AstroSettings }): vite.
151
153
} ,
152
154
// In build, rewrite paths to ESM imported images in code to their final location
153
155
async renderChunk ( code ) {
154
- const assetUrlRE = / _ _ A S T R O _ A S S E T _ I M A G E _ _ ( [ \w $ ] { 8 } ) _ _ (?: _ ( .* ?) _ _ ) ? / g;
156
+ const assetUrlRE = / _ _ A S T R O _ A S S E T _ I M A G E _ _ ( [ \w $ ] + ) _ _ (?: _ ( .* ?) _ _ ) ? / g;
155
157
156
158
let match ;
157
159
let s ;
@@ -205,7 +207,7 @@ export default function assets({ settings }: { settings: AstroSettings }): vite.
205
207
return ;
206
208
}
207
209
208
- const emitFile = shouldEmitFile ? this . emitFile : undefined ;
210
+ const emitFile = shouldEmitFile ? this . emitFile . bind ( this ) : undefined ;
209
211
const imageMetadata = await emitESMImage (
210
212
id ,
211
213
this . meta . watchMode ,
@@ -223,20 +225,24 @@ export default function assets({ settings }: { settings: AstroSettings }): vite.
223
225
if ( settings . config . experimental . svg && / \. s v g $ / . test ( id ) ) {
224
226
const { contents, ...metadata } = imageMetadata ;
225
227
// We know that the contents are present, as we only emit this property for SVG files
226
- return makeSvgComponent ( metadata , contents ! ) ;
228
+ return { code : makeSvgComponent ( metadata , contents ! ) } ;
227
229
}
228
230
229
231
// We can only reliably determine if an image is used on the server, as we need to track its usage throughout the entire build.
230
232
// Since you cannot use image optimization on the client anyway, it's safe to assume that if the user imported
231
233
// an image on the client, it should be present in the final build.
232
234
if ( options ?. ssr ) {
233
- return `export default ${ getProxyCode (
234
- imageMetadata ,
235
- settings . buildOutput === 'server' ,
236
- ) } `;
235
+ return {
236
+ code : `export default ${ getProxyCode (
237
+ imageMetadata ,
238
+ settings . buildOutput === 'server' ,
239
+ ) } `,
240
+ } ;
237
241
} else {
238
242
globalThis . astroAsset . referencedImages . add ( imageMetadata . fsPath ) ;
239
- return `export default ${ JSON . stringify ( imageMetadata ) } ` ;
243
+ return {
244
+ code : `export default ${ JSON . stringify ( imageMetadata ) } ` ,
245
+ } ;
240
246
}
241
247
}
242
248
} ,
0 commit comments