mirror of
https://github.com/withastro/astro.git
synced 2025-03-24 23:21:57 -05:00
[ci] format
This commit is contained in:
parent
9892989619
commit
849fefd8b0
4 changed files with 29 additions and 17 deletions
|
@ -1,19 +1,19 @@
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { debug, error, warn } from '../utils/logger.js';
|
|
||||||
import type { LoggerLevel } from '../utils/logger.js';
|
import type { LoggerLevel } from '../utils/logger.js';
|
||||||
|
import { debug, warn } from '../utils/logger.js';
|
||||||
|
|
||||||
const CACHE_FILE = `cache.json`;
|
const CACHE_FILE = `cache.json`;
|
||||||
|
|
||||||
interface Cache {
|
interface Cache {
|
||||||
[filename: string]: { expires: number }
|
[filename: string]: { expires: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ImageCache {
|
export class ImageCache {
|
||||||
#cacheDir: URL;
|
#cacheDir: URL;
|
||||||
#cacheFile: URL;
|
#cacheFile: URL;
|
||||||
#cache: Cache = { }
|
#cache: Cache = {};
|
||||||
#logLevel: LoggerLevel;
|
#logLevel: LoggerLevel;
|
||||||
|
|
||||||
constructor(dir: URL, logLevel: LoggerLevel) {
|
constructor(dir: URL, logLevel: LoggerLevel) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { doWork } from '@altano/tiny-async-pool';
|
import { doWork } from '@altano/tiny-async-pool';
|
||||||
import type { AstroConfig } from 'astro';
|
import type { AstroConfig } from 'astro';
|
||||||
import { bgGreen, black, cyan, dim, green } from 'kleur/colors';
|
|
||||||
import CachePolicy from 'http-cache-semantics';
|
import CachePolicy from 'http-cache-semantics';
|
||||||
|
import { bgGreen, black, cyan, dim, green } from 'kleur/colors';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import OS from 'node:os';
|
import OS from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
@ -19,11 +19,11 @@ async function loadLocalImage(src: string | URL) {
|
||||||
// we can safely cache local images here.
|
// we can safely cache local images here.
|
||||||
const timeToLive = new Date();
|
const timeToLive = new Date();
|
||||||
timeToLive.setFullYear(timeToLive.getFullYear() + 1);
|
timeToLive.setFullYear(timeToLive.getFullYear() + 1);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data,
|
data,
|
||||||
expires: timeToLive.getTime(),
|
expires: timeToLive.getTime(),
|
||||||
}
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -32,23 +32,23 @@ async function loadLocalImage(src: string | URL) {
|
||||||
function webToCachePolicyRequest({ url, method, headers: _headers }: Request): CachePolicy.Request {
|
function webToCachePolicyRequest({ url, method, headers: _headers }: Request): CachePolicy.Request {
|
||||||
const headers: CachePolicy.Headers = {};
|
const headers: CachePolicy.Headers = {};
|
||||||
for (const [key, value] of _headers) {
|
for (const [key, value] of _headers) {
|
||||||
headers[key] = value;
|
headers[key] = value;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function webToCachePolicyResponse({ status, headers: _headers }: Response): CachePolicy.Response {
|
function webToCachePolicyResponse({ status, headers: _headers }: Response): CachePolicy.Response {
|
||||||
const headers: CachePolicy.Headers = {};
|
const headers: CachePolicy.Headers = {};
|
||||||
for (const [key, value] of _headers) {
|
for (const [key, value] of _headers) {
|
||||||
headers[key] = value;
|
headers[key] = value;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
status,
|
status,
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +88,14 @@ export interface SSGBuildParams {
|
||||||
cacheDir?: URL;
|
cacheDir?: URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function ssgBuild({ loader, staticImages, config, outDir, logLevel, cacheDir }: SSGBuildParams) {
|
export async function ssgBuild({
|
||||||
|
loader,
|
||||||
|
staticImages,
|
||||||
|
config,
|
||||||
|
outDir,
|
||||||
|
logLevel,
|
||||||
|
cacheDir,
|
||||||
|
}: SSGBuildParams) {
|
||||||
let cache: ImageCache | undefined = undefined;
|
let cache: ImageCache | undefined = undefined;
|
||||||
|
|
||||||
if (cacheDir) {
|
if (cacheDir) {
|
||||||
|
@ -174,7 +181,7 @@ export async function ssgBuild({ loader, staticImages, config, outDir, logLevel,
|
||||||
if (cache?.has(pathRelative)) {
|
if (cache?.has(pathRelative)) {
|
||||||
data = await cache.get(pathRelative);
|
data = await cache.get(pathRelative);
|
||||||
}
|
}
|
||||||
|
|
||||||
// a valid cache file wasn't found, transform the image and cache it
|
// a valid cache file wasn't found, transform the image and cache it
|
||||||
if (!data) {
|
if (!data) {
|
||||||
const transformed = await loader.transform(inputBuffer, transform);
|
const transformed = await loader.transform(inputBuffer, transform);
|
||||||
|
|
|
@ -129,7 +129,9 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loader && 'transform' in loader && staticImages.size > 0) {
|
if (loader && 'transform' in loader && staticImages.size > 0) {
|
||||||
const cacheDir = !!resolvedOptions.cacheDir ? new URL(resolvedOptions.cacheDir, _config.root) : undefined;
|
const cacheDir = !!resolvedOptions.cacheDir
|
||||||
|
? new URL(resolvedOptions.cacheDir, _config.root)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
await ssgBuild({
|
await ssgBuild({
|
||||||
loader,
|
loader,
|
||||||
|
|
|
@ -263,7 +263,10 @@ describe('SSG images - build', function () {
|
||||||
|
|
||||||
verifyImage(image.attr('src'), size);
|
verifyImage(image.attr('src'), size);
|
||||||
|
|
||||||
const url = new URL('./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'), import.meta.url);
|
const url = new URL(
|
||||||
|
'./fixtures/basic-image/node_modules/.astro/image' + image.attr('src'),
|
||||||
|
import.meta.url
|
||||||
|
);
|
||||||
expect(await fs.stat(url), 'transformed image was cached').to.not.be.undefined;
|
expect(await fs.stat(url), 'transformed image was cached').to.not.be.undefined;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue