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:
parent
dc8bcb6ae7
commit
4d2f9450a3
4 changed files with 43 additions and 5 deletions
5
.changeset/thirty-geckos-bathe.md
Normal file
5
.changeset/thirty-geckos-bathe.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add ability to remove assets originals
|
|
@ -791,6 +791,24 @@ export interface AstroUserConfig {
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
assetsPrefix?: string;
|
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
|
* @docs
|
||||||
* @name build.serverEntry
|
* @name build.serverEntry
|
||||||
|
|
|
@ -57,6 +57,23 @@ export async function generateImage(
|
||||||
const finalFileURL = new URL('.' + filepath, clientRoot);
|
const finalFileURL = new URL('.' + filepath, clientRoot);
|
||||||
const finalFolderURL = new URL('./', finalFileURL);
|
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
|
// 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 cacheFile = basename(filepath) + (isLocalImage ? '' : '.json');
|
||||||
const cachedFileURL = new URL(cacheFile, assetsCacheDir);
|
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
|
// 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 imageData;
|
||||||
let resultData: { data: Buffer | undefined; expires: number | undefined } = {
|
let resultData: { data: Buffer | undefined; expires: number | undefined } = {
|
||||||
data: undefined,
|
data: undefined,
|
||||||
|
|
|
@ -23,6 +23,7 @@ const ASTRO_CONFIG_DEFAULTS = {
|
||||||
client: './dist/client/',
|
client: './dist/client/',
|
||||||
server: './dist/server/',
|
server: './dist/server/',
|
||||||
assets: '_astro',
|
assets: '_astro',
|
||||||
|
assetsRemoveOriginals: false,
|
||||||
serverEntry: 'entry.mjs',
|
serverEntry: 'entry.mjs',
|
||||||
redirects: true,
|
redirects: true,
|
||||||
inlineStylesheets: 'auto',
|
inlineStylesheets: 'auto',
|
||||||
|
@ -119,6 +120,7 @@ export const AstroConfigSchema = z.object({
|
||||||
.transform((val) => new URL(val)),
|
.transform((val) => new URL(val)),
|
||||||
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
||||||
assetsPrefix: z.string().optional(),
|
assetsPrefix: z.string().optional(),
|
||||||
|
assetsRemoveOriginals: z.boolean().default(ASTRO_CONFIG_DEFAULTS.build.assetsRemoveOriginals),
|
||||||
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
||||||
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
||||||
inlineStylesheets: z
|
inlineStylesheets: z
|
||||||
|
@ -341,6 +343,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
|
||||||
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
|
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
|
||||||
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
|
||||||
assetsPrefix: z.string().optional(),
|
assetsPrefix: z.string().optional(),
|
||||||
|
assetsRemoveOriginals: z.boolean().default(ASTRO_CONFIG_DEFAULTS.build.assetsRemoveOriginals),
|
||||||
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
|
||||||
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects),
|
||||||
inlineStylesheets: z
|
inlineStylesheets: z
|
||||||
|
|
Loading…
Reference in a new issue