Repository: withastro/astro
File: packages/astro/src/assets/vite-plugin-assets.ts (TypeScript)

Code before:
	let shouldEmitFile = false;
	let isBuild = false;

	globalThis.astroAsset = {
		referencedImages: new Set(),
	};

	const imageComponentPrefix = settings.config.experimental.responsiveImages ? 'Responsive' : '';
	return [
		// Expose the components and different utilities from `astro:assets`
		{
			name: 'astro:assets',
			config(_, env) {
				isBuild = env.command === 'build';
			},
			async resolveId(id) {
				if (id === VIRTUAL_SERVICE_ID) {
					return await this.resolve(settings.config.image.service.entrypoint);
				}
				if (id === VIRTUAL_MODULE_ID) {
					return resolvedVirtualModuleId;
				}
			},
			load(id) {
				if (id === resolvedVirtualModuleId) {
					return {
						code: /* ts */ `
							export { getConfiguredImageService, isLocalService } from "astro/assets";
							import { getImage as getImageInternal } from "astro/assets";
							export { default as Image } from "astro/components/${imageComponentPrefix}Image.astro";
							export { default as Picture } from "astro/components/${imageComponentPrefix}Picture.astro";
							export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";

							export const imageConfig = ${JSON.stringify({ ...settings.config.image, experimentalResponsiveImages: settings.config.experimental.responsiveImages })};
							// This is used by the @astrojs/node integration to locate images.
							// It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
							// new URL("dist/...") is interpreted by the bundler as a signal to include that directory
							// in the Lambda bundle, which would bloat the bundle with images.
							// To prevent this, we mark the URL construction as pure,
							// so that it's tree-shaken away for all platforms that don't need it.
							export const outDir = /* #__PURE__ */ new URL(${JSON.stringify(
								new URL(
									settings.buildOutput === 'server'
										? settings.config.build.client
										: settings.config.outDir,
								),
							)});
							export const assetsDir = /* #__PURE__ */ new URL(${JSON.stringify(
								settings.config.build.assets,
							)}, outDir);
							export const getImage = async (options) => await getImageInternal(options, imageConfig);

Review comment (suggestion):
Makes it an object that returns `code` (rollup/rolldown compatible shape) and `moduleType` (only rolldown I think)

Code after:
	let shouldEmitFile = false;
	let isBuild = false;

	globalThis.astroAsset = {
		referencedImages: new Set(),
	};

	const imageComponentPrefix = settings.config.experimental.responsiveImages ? 'Responsive' : '';
	return [
		// Expose the components and different utilities from `astro:assets`
		{
			name: 'astro:assets',
			config(_, env) {
				isBuild = env.command === 'build';
			},
			async resolveId(id) {
				if (id === VIRTUAL_SERVICE_ID) {
					return await this.resolve(settings.config.image.service.entrypoint);
				}
				if (id === VIRTUAL_MODULE_ID) {
					return resolvedVirtualModuleId;
				}
			},
			load(id) {
				if (id === resolvedVirtualModuleId) {
					return {
						code: `
							export { getConfiguredImageService, isLocalService } from "astro/assets";
							import { getImage as getImageInternal } from "astro/assets";
							export { default as Image } from "astro/components/${imageComponentPrefix}Image.astro";
							export { default as Picture } from "astro/components/${imageComponentPrefix}Picture.astro";
							export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";

							export const imageConfig = ${JSON.stringify({ ...settings.config.image, experimentalResponsiveImages: settings.config.experimental.responsiveImages })};
							// This is used by the @astrojs/node integration to locate images.
							// It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
							// new URL("dist/...") is interpreted by the bundler as a signal to include that directory
							// in the Lambda bundle, which would bloat the bundle with images.
							// To prevent this, we mark the URL construction as pure,
							// so that it's tree-shaken away for all platforms that don't need it.
							export const outDir = /* #__PURE__ */ new URL(${JSON.stringify(
								new URL(
									settings.buildOutput === 'server'
										? settings.config.build.client
										: settings.config.outDir,
								),
							)});
							export const assetsDir = /* #__PURE__ */ new URL(${JSON.stringify(
								settings.config.build.assets,
							)}, outDir);
							export const getImage = async (options) => await getImageInternal(options, imageConfig);