removing a few node dependencies
This commit is contained in:
parent
d3c8185234
commit
175b8e51dc
4 changed files with 27 additions and 28 deletions
|
@ -6,10 +6,31 @@ import OS from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import type { SSRImageService, TransformOptions } from '../loaders/index.js';
|
import type { SSRImageService, TransformOptions } from '../loaders/index.js';
|
||||||
import { loadLocalImage, loadRemoteImage } from '../utils/images.js';
|
|
||||||
import { debug, info, LoggerLevel, warn } from '../utils/logger.js';
|
import { debug, info, LoggerLevel, warn } from '../utils/logger.js';
|
||||||
import { isRemoteImage } from '../utils/paths.js';
|
import { isRemoteImage } from '../utils/paths.js';
|
||||||
|
|
||||||
|
async function loadLocalImage(src: string | URL) {
|
||||||
|
try {
|
||||||
|
return await fs.readFile(src);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadRemoteImage(src: string) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(src);
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Buffer.from(await res.arrayBuffer());
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getTimeStat(timeStart: number, timeEnd: number) {
|
function getTimeStat(timeStart: number, timeEnd: number) {
|
||||||
const buildTime = timeEnd - timeStart;
|
const buildTime = timeEnd - timeStart;
|
||||||
return buildTime < 750 ? `${Math.round(buildTime)}ms` : `${(buildTime / 1000).toFixed(2)}s`;
|
return buildTime < 750 ? `${Math.round(buildTime)}ms` : `${(buildTime / 1000).toFixed(2)}s`;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/// <reference types="astro/astro-jsx" />
|
/// <reference types="astro/astro-jsx" />
|
||||||
import mime from 'mime';
|
import mime from 'mime';
|
||||||
import { extname } from 'node:path';
|
|
||||||
import { OutputFormat, parseAspectRatio, TransformOptions } from '../loaders/index.js';
|
import { OutputFormat, parseAspectRatio, TransformOptions } from '../loaders/index.js';
|
||||||
import { ImageMetadata } from '../vite-plugin-astro-image.js';
|
import { ImageMetadata } from '../vite-plugin-astro-image.js';
|
||||||
import { getImage } from './get-image.js';
|
import { getImage } from './get-image.js';
|
||||||
|
import { extname } from '../utils/paths.js';
|
||||||
|
|
||||||
export interface GetPictureParams {
|
export interface GetPictureParams {
|
||||||
src: string | ImageMetadata | Promise<{ default: ImageMetadata }>;
|
src: string | ImageMetadata | Promise<{ default: ImageMetadata }>;
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
import fs from 'node:fs/promises';
|
|
||||||
|
|
||||||
export async function loadLocalImage(src: string | URL) {
|
|
||||||
try {
|
|
||||||
return await fs.readFile(src);
|
|
||||||
} catch {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function loadRemoteImage(src: string) {
|
|
||||||
try {
|
|
||||||
const res = await fetch(src);
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Buffer.from(await res.arrayBuffer());
|
|
||||||
} catch {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,14 +10,15 @@ function removeQueryString(src: string) {
|
||||||
return index > 0 ? src.substring(0, index) : src;
|
return index > 0 ? src.substring(0, index) : src;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extname(src: string, format?: OutputFormat) {
|
export function extname(src: string) {
|
||||||
const index = src.lastIndexOf('.');
|
const base = basename(src);
|
||||||
|
const index = base.lastIndexOf('.');
|
||||||
|
|
||||||
if (index <= 0) {
|
if (index <= 0) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
return src.substring(index);
|
return src.substring(src.length - (base.length - index));
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeExtname(src: string) {
|
function removeExtname(src: string) {
|
||||||
|
|
Loading…
Reference in a new issue