Fix review comments

- Add SSR check
- Fix changeset type
- Rename config option from `build.assetsRemoveOriginals` to `image.removeOriginals`
- Docs (by @Princesseuh)
This commit is contained in:
Maksim Karelov 2023-09-01 18:42:44 +07:00
parent 4d2f9450a3
commit a41cf54251
4 changed files with 26 additions and 24 deletions

View file

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

View file

@ -791,24 +791,6 @@ 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
@ -1076,6 +1058,26 @@ export interface AstroUserConfig {
*/
remotePatterns?: Partial<RemotePattern>[];
/**
* @docs
* @name image.removeOriginals
* @type {boolean}
* @default `false`
* @version 3.1.0
* @description
* Specifies if original files for optimized assets should be removed from the final build in SSG.
* Note that this option is **potentially unsafe**, as we cannot reliably tell if your website uses the original files outside of the optimization pipeline.
* With this option enabled, **ALL** original files won't be included in the final output.
*
* ```js
* {
* image: {
* removeOriginals: true
* }
* }
* ```
*/
removeOriginals?: boolean;
};
/**

View file

@ -29,6 +29,7 @@ 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);
@ -44,7 +45,7 @@ export async function generateImage(
}
let serverRoot: URL, clientRoot: URL;
if (isServerLikeOutput(config)) {
if (ssr) {
serverRoot = config.build.server;
clientRoot = config.build.client;
} else {
@ -62,7 +63,7 @@ export async function generateImage(
? (options.src as ImageMetadata).src
: (options.src as string);
if (config.build.assetsRemoveOriginals) {
if (!ssr && config.image.removeOriginals) {
const originalFileURL = new URL('.' + originalImagePath, clientRoot);
try {
await fs.promises.unlink(originalFileURL);

View file

@ -23,7 +23,6 @@ const ASTRO_CONFIG_DEFAULTS = {
client: './dist/client/',
server: './dist/server/',
assets: '_astro',
assetsRemoveOriginals: false,
serverEntry: 'entry.mjs',
redirects: true,
inlineStylesheets: 'auto',
@ -32,6 +31,7 @@ const ASTRO_CONFIG_DEFAULTS = {
},
image: {
service: { entrypoint: 'astro/assets/services/sharp', config: {} },
removeOriginals: false,
},
compressHTML: true,
server: {
@ -120,7 +120,6 @@ 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
@ -221,6 +220,7 @@ export const AstroConfigSchema = z.object({
})
)
.default([]),
removeOriginals: z.boolean().default(ASTRO_CONFIG_DEFAULTS.image.removeOriginals),
})
.default(ASTRO_CONFIG_DEFAULTS.image),
markdown: z
@ -343,7 +343,6 @@ 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