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 &&
|
transformsAndPath.originalSrcPath &&
|
||||||
!globalThis.astroAsset.referencedImages?.has(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 {
|
try {
|
||||||
if (transformsAndPath.originalSrcPath) {
|
if (transformsAndPath.originalSrcPath) {
|
||||||
env.logger.debug(
|
env.logger.debug(
|
||||||
|
|
|
@ -25,6 +25,7 @@ declare global {
|
||||||
| undefined;
|
| undefined;
|
||||||
staticImages?: AssetsGlobalStaticImagesList;
|
staticImages?: AssetsGlobalStaticImagesList;
|
||||||
referencedImages?: Set<string>;
|
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 MagicString from 'magic-string';
|
||||||
import type * as vite from 'vite';
|
import type * as vite from 'vite';
|
||||||
import { normalizePath } from 'vite';
|
import { normalizePath } from 'vite';
|
||||||
|
@ -99,6 +99,7 @@ export default function assets({
|
||||||
|
|
||||||
globalThis.astroAsset = {
|
globalThis.astroAsset = {
|
||||||
referencedImages: new Set(),
|
referencedImages: new Set(),
|
||||||
|
originalAssets: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -173,6 +174,13 @@ export default function assets({
|
||||||
const [full, hash, postfix = ''] = match;
|
const [full, hash, postfix = ''] = match;
|
||||||
|
|
||||||
const file = this.getFileName(hash);
|
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 fileExtension = extname(file);
|
||||||
const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix);
|
const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix);
|
||||||
const prefix = pf ? appendForwardSlash(pf) : resolvedConfig.base;
|
const prefix = pf ? appendForwardSlash(pf) : resolvedConfig.base;
|
||||||
|
|
Loading…
Reference in a new issue