Add ability to remove assets originals

Related to #8143

Setting `assetsRemoveOriginals: true` will remove original images from `dist/_astro` folder
This commit is contained in:
Maksim Karelov 2023-09-01 17:54:23 +07:00
parent dc8bcb6ae7
commit 4d2f9450a3
4 changed files with 43 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Add ability to remove assets originals

View file

@ -791,6 +791,24 @@ export interface AstroUserConfig {
* ```
*/
assetsPrefix?: string;
/**
* @docs
* @name build.assetsRemoveOriginals
* @type {boolean}
* @default `false`
* @version 3.0.6
* @description
* Specifies necessity for removing assets originals.
*
* ```js
* {
* build: {
* assetsRemoveOriginals: true
* }
* }
* ```
*/
assetsRemoveOriginals?: boolean;
/**
* @docs
* @name build.serverEntry

View file

@ -57,6 +57,23 @@ 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 (config.build.assetsRemoveOriginals) {
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);
@ -90,11 +107,6 @@ 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

@ -23,6 +23,7 @@ const ASTRO_CONFIG_DEFAULTS = {
client: './dist/client/',
server: './dist/server/',
assets: '_astro',
assetsRemoveOriginals: false,
serverEntry: 'entry.mjs',
redirects: true,
inlineStylesheets: 'auto',
@ -119,6 +120,7 @@ export const AstroConfigSchema = z.object({
.transform((val) => new URL(val)),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z.string().optional(),
assetsRemoveOriginals: z.boolean().default(ASTRO_CONFIG_DEFAULTS.build.assetsRemoveOriginals),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
inlineStylesheets: z
@ -341,6 +343,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z.string().optional(),
assetsRemoveOriginals: z.boolean().default(ASTRO_CONFIG_DEFAULTS.build.assetsRemoveOriginals),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
inlineStylesheets: z