0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix(assets): Don't delete original images if multiple images were generated with the same name and hash

This commit is contained in:
Princesseuh 2024-07-11 16:09:52 +02:00
parent 88e2b43305
commit 09ebbc136e
No known key found for this signature in database
GPG key ID: 105BBD6D57F2B0C0
3 changed files with 17 additions and 1 deletions

View file

@ -115,6 +115,13 @@ export async function generateImagesForPath(
transformsAndPath.originalSrcPath &&
!globalThis.astroAsset.referencedImages?.has(transformsAndPath.originalSrcPath)
) {
// If multiple images imports resulted in the same output file, don't delete the original file as
// we cannot know if the other references to it are used or not.
const creationCount = globalThis.astroAsset.originalAssets?.[basename(originalFilePath)];
if (creationCount && creationCount > 1) {
return;
}
try {
if (transformsAndPath.originalSrcPath) {
env.logger.debug(

View file

@ -25,6 +25,7 @@ declare global {
| undefined;
staticImages?: AssetsGlobalStaticImagesList;
referencedImages?: Set<string>;
originalAssets?: Record<string, number>
};
}

View file

@ -1,4 +1,4 @@
import { extname } from 'node:path';
import { basename, extname } from 'node:path';
import MagicString from 'magic-string';
import type * as vite from 'vite';
import { normalizePath } from 'vite';
@ -99,6 +99,7 @@ export default function assets({
globalThis.astroAsset = {
referencedImages: new Set(),
originalAssets: {}
};
return [
@ -173,6 +174,13 @@ export default function assets({
const [full, hash, postfix = ''] = match;
const file = this.getFileName(hash);
const fileBasename = basename(file);
if (globalThis.astroAsset.originalAssets) {
globalThis.astroAsset.originalAssets[fileBasename] ??= 0;
globalThis.astroAsset.originalAssets[fileBasename]++;
}
const fileExtension = extname(file);
const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix);
const prefix = pf ? appendForwardSlash(pf) : resolvedConfig.base;