[ci] format
This commit is contained in:
parent
ef9345767b
commit
cb1db133ba
10 changed files with 36 additions and 44 deletions
|
@ -2,9 +2,9 @@ import fs from 'fs/promises';
|
|||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { OUTPUT_DIR } from '../constants.js';
|
||||
import { ensureDir } from '../utils/paths.js';
|
||||
import { isRemoteImage, loadRemoteImage, loadLocalImage } from '../utils/images.js';
|
||||
import type { SSRImageService, TransformOptions } from '../types.js';
|
||||
import { isRemoteImage, loadLocalImage, loadRemoteImage } from '../utils/images.js';
|
||||
import { ensureDir } from '../utils/paths.js';
|
||||
|
||||
export interface SSGBuildParams {
|
||||
loader: SSRImageService;
|
||||
|
@ -13,12 +13,7 @@ export interface SSGBuildParams {
|
|||
outDir: URL;
|
||||
}
|
||||
|
||||
export async function ssgBuild({
|
||||
loader,
|
||||
staticImages,
|
||||
srcDir,
|
||||
outDir,
|
||||
}: SSGBuildParams) {
|
||||
export async function ssgBuild({ loader, staticImages, srcDir, outDir }: SSGBuildParams) {
|
||||
const inputFiles = new Set<string>();
|
||||
|
||||
// process transforms one original image file at a time
|
||||
|
@ -51,10 +46,7 @@ export async function ssgBuild({
|
|||
let outputFile: string;
|
||||
|
||||
if (isRemoteImage(src)) {
|
||||
const outputFileURL = new URL(
|
||||
path.join('./', OUTPUT_DIR, path.basename(filename)),
|
||||
outDir
|
||||
);
|
||||
const outputFileURL = new URL(path.join('./', OUTPUT_DIR, path.basename(filename)), outDir);
|
||||
outputFile = fileURLToPath(outputFileURL);
|
||||
} else {
|
||||
const outputFileURL = new URL(path.join('./', OUTPUT_DIR, filename), outDir);
|
||||
|
|
|
@ -6,10 +6,9 @@ import { ensureDir } from '../utils/paths.js';
|
|||
|
||||
async function globImages(dir: URL) {
|
||||
const srcPath = fileURLToPath(dir);
|
||||
return await glob(
|
||||
`${srcPath}/**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}`,
|
||||
{ absolute: true }
|
||||
);
|
||||
return await glob(`${srcPath}/**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}`, {
|
||||
absolute: true,
|
||||
});
|
||||
}
|
||||
|
||||
export interface SSRBuildParams {
|
||||
|
|
|
@ -2,8 +2,8 @@ import type { AstroConfig, AstroIntegration } from 'astro';
|
|||
import { ssgBuild } from './build/ssg.js';
|
||||
import { ssrBuild } from './build/ssr.js';
|
||||
import { PKG_NAME, ROUTE_PATTERN } from './constants.js';
|
||||
import { filenameFormat, propsToFilename } from './utils/paths.js';
|
||||
import { IntegrationOptions, TransformOptions } from './types.js';
|
||||
import { filenameFormat, propsToFilename } from './utils/paths.js';
|
||||
import { createPlugin } from './vite-plugin-astro-image.js';
|
||||
|
||||
export default function integration(options: IntegrationOptions = {}): AstroIntegration {
|
||||
|
|
|
@ -101,9 +101,7 @@ async function resolveTransform(input: GetImageTransform): Promise<TransformOpti
|
|||
* @param transform @type {TransformOptions} The transformations requested for the optimized image.
|
||||
* @returns @type {ImageAttributes} The HTML attributes to be included on the built `<img />` element.
|
||||
*/
|
||||
export async function getImage(
|
||||
transform: GetImageTransform
|
||||
): Promise<ImageAttributes> {
|
||||
export async function getImage(transform: GetImageTransform): Promise<ImageAttributes> {
|
||||
if (!transform.src) {
|
||||
throw new Error('[@astrojs/image] `src` is required');
|
||||
}
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
import { lookup } from 'mrmime';
|
||||
import { extname } from 'path';
|
||||
import { getImage } from './get-image.js';
|
||||
import {
|
||||
ImageAttributes,
|
||||
ImageMetadata,
|
||||
OutputFormat,
|
||||
TransformOptions,
|
||||
} from '../types.js';
|
||||
import { ImageAttributes, ImageMetadata, OutputFormat, TransformOptions } from '../types.js';
|
||||
import { parseAspectRatio } from '../utils/images.js';
|
||||
import { getImage } from './get-image.js';
|
||||
|
||||
export interface GetPictureParams {
|
||||
src: string | ImageMetadata | Promise<{ default: ImageMetadata }>;
|
||||
|
@ -45,7 +40,7 @@ async function resolveFormats({ src, formats }: GetPictureParams) {
|
|||
|
||||
export async function getPicture(params: GetPictureParams): Promise<GetPictureResult> {
|
||||
const { src, widths } = params;
|
||||
|
||||
|
||||
if (!src) {
|
||||
throw new Error('[@astrojs/image] `src` is required');
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { OUTPUT_DIR } from '../constants.js';
|
||||
import type { TransformOptions } from '../types.js';
|
||||
import { isRemoteImage } from './images.js';
|
||||
import { shorthash } from './shorthash.js';
|
||||
import type { TransformOptions } from '../types.js';
|
||||
|
||||
export function ensureDir(dir: string) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
|
@ -32,9 +32,5 @@ export function propsToFilename({ src, width, height, format }: TransformOptions
|
|||
export function filenameFormat(transform: TransformOptions) {
|
||||
return isRemoteImage(transform.src)
|
||||
? path.join(OUTPUT_DIR, path.basename(propsToFilename(transform)))
|
||||
: path.join(
|
||||
OUTPUT_DIR,
|
||||
path.dirname(transform.src),
|
||||
path.basename(propsToFilename(transform))
|
||||
);
|
||||
: path.join(OUTPUT_DIR, path.dirname(transform.src), path.basename(propsToFilename(transform)));
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ import type { PluginContext } from 'rollup';
|
|||
import slash from 'slash';
|
||||
import { pathToFileURL } from 'url';
|
||||
import type { Plugin, ResolvedConfig } from 'vite';
|
||||
import { metadata } from './utils/metadata.js';
|
||||
import type { IntegrationOptions } from './types.js';
|
||||
import { metadata } from './utils/metadata.js';
|
||||
|
||||
export function createPlugin(config: AstroConfig, options: Required<IntegrationOptions>): Plugin {
|
||||
const filter = (id: string) =>
|
||||
|
@ -60,6 +60,6 @@ export function createPlugin(config: AstroConfig, options: Required<IntegrationO
|
|||
};
|
||||
|
||||
return `export default ${JSON.stringify(output)}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -71,7 +71,9 @@ describe('SSR images - build', function () {
|
|||
});
|
||||
|
||||
it('includes the original images', () => {
|
||||
['/assets/social.jpg', '/assets/social.png', '/assets/blog/introducing-astro.jpg'].map(verifyImage);
|
||||
['/assets/social.jpg', '/assets/social.png', '/assets/blog/introducing-astro.jpg'].map(
|
||||
verifyImage
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -87,7 +87,9 @@ describe('SSR pictures - build', function () {
|
|||
});
|
||||
|
||||
it('includes the original images', () => {
|
||||
['/assets/social.jpg', '/assets/social.png', '/assets/blog/introducing-astro.jpg'].map(verifyImage);
|
||||
['/assets/social.jpg', '/assets/social.png', '/assets/blog/introducing-astro.jpg'].map(
|
||||
verifyImage
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -33,16 +33,20 @@ describe('Image rotation', function () {
|
|||
it('includes <img> attributes', () => {
|
||||
for (let i = 0; i < 9; i++) {
|
||||
const image = $(`#landscape-${i}`);
|
||||
|
||||
|
||||
expect(image.attr('src')).to.equal(`/_image/assets/Landscape_${i}_1800x1200.jpg`);
|
||||
expect(image.attr('width')).to.equal('1800');
|
||||
expect(image.attr('height')).to.equal('1200');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it('built the optimized image', () => {
|
||||
for (let i = 0; i < 9; i++) {
|
||||
verifyImage(`/_image/assets/Landscape_${i}_1800x1200.jpg`, { width: 1800, height: 1200, type: 'jpg' });
|
||||
verifyImage(`/_image/assets/Landscape_${i}_1800x1200.jpg`, {
|
||||
width: 1800,
|
||||
height: 1200,
|
||||
type: 'jpg',
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -51,16 +55,20 @@ describe('Image rotation', function () {
|
|||
it('includes <img> attributes', () => {
|
||||
for (let i = 0; i < 9; i++) {
|
||||
const image = $(`#portrait-${i}`);
|
||||
|
||||
|
||||
expect(image.attr('src')).to.equal(`/_image/assets/Portrait_${i}_1200x1800.jpg`);
|
||||
expect(image.attr('width')).to.equal('1200');
|
||||
expect(image.attr('height')).to.equal('1800');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it('built the optimized image', () => {
|
||||
for (let i = 0; i < 9; i++) {
|
||||
verifyImage(`/_image/assets/Portrait_${i}_1200x1800.jpg`, { width: 1200, height: 1800, type: 'jpg' });
|
||||
verifyImage(`/_image/assets/Portrait_${i}_1200x1800.jpg`, {
|
||||
width: 1200,
|
||||
height: 1800,
|
||||
type: 'jpg',
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue