Move removing code from assets to core

This commit is contained in:
Maksim Karelov 2023-09-01 20:08:59 +07:00
parent a41cf54251
commit 99232793c2
2 changed files with 27 additions and 19 deletions

View file

@ -29,7 +29,6 @@ export async function generateImage(
): Promise<GenerationData | undefined> {
const config = pipeline.getConfig();
const logger = pipeline.getLogger();
const ssr = isServerLikeOutput(config);
let useCache = true;
const assetsCacheDir = new URL('assets/', config.cacheDir);
@ -45,7 +44,7 @@ export async function generateImage(
}
let serverRoot: URL, clientRoot: URL;
if (ssr) {
if (isServerLikeOutput(config)) {
serverRoot = config.build.server;
clientRoot = config.build.client;
} else {
@ -58,23 +57,6 @@ export async function generateImage(
const finalFileURL = new URL('.' + filepath, clientRoot);
const finalFolderURL = new URL('./', finalFileURL);
// The original filepath or URL from the image transform
const originalImagePath = isLocalImage
? (options.src as ImageMetadata).src
: (options.src as string);
if (!ssr && config.image.removeOriginals) {
const originalFileURL = new URL('.' + originalImagePath, clientRoot);
try {
await fs.promises.unlink(originalFileURL);
} catch (e) {
logger.warn(
'astro:assets',
`An error was encountered while removing asset original. Error: ${e}`
);
}
}
// For remote images, instead of saving the image directly, we save a JSON file with the image data and expiration date from the server
const cacheFile = basename(filepath) + (isLocalImage ? '' : '.json');
const cachedFileURL = new URL(cacheFile, assetsCacheDir);
@ -108,6 +90,11 @@ export async function generateImage(
// If the cache file doesn't exist, just move on, and we'll generate it
}
// The original filepath or URL from the image transform
const originalImagePath = isLocalImage
? (options.src as ImageMetadata).src
: (options.src as string);
let imageData;
let resultData: { data: Buffer | undefined; expires: number | undefined } = {
data: undefined,

View file

@ -9,6 +9,7 @@ import type {
AstroSettings,
ComponentInstance,
GetStaticPathsItem,
ImageMetadata,
ImageTransform,
MiddlewareEndpointHandler,
RouteData,
@ -21,6 +22,7 @@ import {
generateImage as generateImageInternal,
getStaticImageList,
} from '../../assets/build/generate.js';
import { isESMImportedImage } from '../../assets/internal.js';
import { hasPrerenderedPages, type BuildInternals } from '../../core/build/internal.js';
import {
isRelativePath,
@ -196,6 +198,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
}
}
const originalImages = new Set<string>();
const staticImageList = getStaticImageList();
if (staticImageList.size)
@ -210,8 +213,26 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
count,
staticImageList.size
);
// We don't need to remove remote images
const imageSrc = imageData[1].options.src;
const isLocalImage = isESMImportedImage(imageSrc);
if (opts.settings.config.image.removeOriginals && !ssr && isLocalImage) {
originalImages.add(imageSrc.src);
}
}
for (const originalImagePath of originalImages) {
try {
const targetPath = new URL('.' + originalImagePath, outFolder);
await fs.promises.unlink(targetPath);
} catch (e) {
logger.warn(
'astro:assets',
`An error was encountered while removing original image. Error: ${e}`
);
}
delete globalThis?.astroAsset?.addStaticImage;
await runHookBuildGenerated({