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:
parent
88e2b43305
commit
09ebbc136e
3 changed files with 17 additions and 1 deletions
|
@ -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(
|
||||
|
|
|
@ -25,6 +25,7 @@ declare global {
|
|||
| undefined;
|
||||
staticImages?: AssetsGlobalStaticImagesList;
|
||||
referencedImages?: Set<string>;
|
||||
originalAssets?: Record<string, number>
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue