Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sull authored and astrobot-houston committed Jul 8, 2022
1 parent 89d7675 commit d2f6834
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
29 changes: 18 additions & 11 deletions packages/integrations/image/src/get-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import slash from 'slash';
import { ROUTE_PATTERN } from './constants.js';
import { ImageAttributes, ImageMetadata, ImageService, isSSRService, OutputFormat, TransformOptions } from './types.js';
import {
ImageAttributes,
ImageMetadata,
ImageService,
isSSRService,
OutputFormat,
TransformOptions,
} from './types.js';
import { parseAspectRatio } from './utils.js';

export interface GetImageTransform extends Omit<TransformOptions, 'src'> {
Expand All @@ -18,7 +25,9 @@ function resolveSize(transform: TransformOptions): TransformOptions {
}

if (!transform.aspectRatio) {
throw new Error(`"aspectRatio" must be included if only "${transform.width ? "width": "height"}" is provided`)
throw new Error(
`"aspectRatio" must be included if only "${transform.width ? 'width' : 'height'}" is provided`
);
}

let aspectRatio: number;
Expand All @@ -32,18 +41,18 @@ function resolveSize(transform: TransformOptions): TransformOptions {
}

if (transform.width) {
// only width was provided, calculate height
// only width was provided, calculate height
return {
...transform,
width: transform.width,
height: Math.round(transform.width / aspectRatio)
height: Math.round(transform.width / aspectRatio),
} as TransformOptions;
} else if (transform.height) {
// only height was provided, calculate width
// only height was provided, calculate width
return {
...transform,
width: Math.round(transform.height * aspectRatio),
height: transform.height
height: transform.height,
};
}

Expand All @@ -57,9 +66,7 @@ async function resolveTransform(input: GetImageTransform): Promise<TransformOpti
}

// resolve the metadata promise, usually when the ESM import is inlined
const metadata = 'then' in input.src
? (await input.src).default
: input.src;
const metadata = 'then' in input.src ? (await input.src).default : input.src;

let { width, height, aspectRatio, format = metadata.format, ...rest } = input;

Expand All @@ -84,7 +91,7 @@ async function resolveTransform(input: GetImageTransform): Promise<TransformOpti
height,
aspectRatio,
format: format as OutputFormat,
}
};
}

/**
Expand All @@ -94,7 +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(
export async function getImage(
loader: ImageService,
transform: GetImageTransform
): Promise<ImageAttributes> {
Expand Down
35 changes: 24 additions & 11 deletions packages/integrations/image/src/get-picture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { lookup } from 'mrmime';
import { extname } from 'path';
import { getImage } from './get-image.js';
import { ImageAttributes, ImageMetadata, ImageService, OutputFormat, TransformOptions } from './types.js';
import {
ImageAttributes,
ImageMetadata,
ImageService,
OutputFormat,
TransformOptions,
} from './types.js';
import { parseAspectRatio } from './utils.js';

export interface GetPictureParams {
Expand All @@ -14,7 +20,7 @@ export interface GetPictureParams {

export interface GetPictureResult {
image: ImageAttributes;
sources: { type: string; srcset: string; }[];
sources: { type: string; srcset: string }[];
}

async function resolveAspectRatio({ src, aspectRatio }: GetPictureParams) {
Expand Down Expand Up @@ -49,14 +55,21 @@ export async function getPicture(params: GetPictureParams): Promise<GetPictureRe
}

async function getSource(format: OutputFormat) {
const imgs = await Promise.all(widths.map(async (width) => {
const img = await getImage(loader, { src, format, width, height: Math.round(width / aspectRatio!) });
return `${img.src} ${width}w`;
}))
const imgs = await Promise.all(
widths.map(async (width) => {
const img = await getImage(loader, {
src,
format,
width,
height: Math.round(width / aspectRatio!),
});
return `${img.src} ${width}w`;
})
);

return {
type: lookup(format) || format,
srcset: imgs.join(',')
srcset: imgs.join(','),
};
}

Expand All @@ -67,13 +80,13 @@ export async function getPicture(params: GetPictureParams): Promise<GetPictureRe
src,
width: Math.max(...widths),
aspectRatio,
format: allFormats[allFormats.length - 1]
format: allFormats[allFormats.length - 1],
});

const sources = await Promise.all(allFormats.map(format => getSource(format)));
const sources = await Promise.all(allFormats.map((format) => getSource(format)));

return {
sources,
image
}
image,
};
}
4 changes: 2 additions & 2 deletions packages/integrations/image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { OUTPUT_DIR, PKG_NAME, ROUTE_PATTERN } from './constants.js';
export * from './get-image.js';
export * from './get-picture.js';
import { IntegrationOptions, TransformOptions } from './types.js';
import {
ensureDir,
Expand All @@ -14,6 +12,8 @@ import {
propsToFilename,
} from './utils.js';
import { createPlugin } from './vite-plugin-astro-image.js';
export * from './get-image.js';
export * from './get-picture.js';

const createIntegration = (options: IntegrationOptions = {}): AstroIntegration => {
const resolvedOptions = {
Expand Down
3 changes: 2 additions & 1 deletion packages/integrations/image/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export interface HostedImageService<T extends TransformOptions = TransformOption
getImageAttributes(transform: T): Promise<ImageAttributes>;
}

export interface SSRImageService<T extends TransformOptions = TransformOptions> extends HostedImageService<T> {
export interface SSRImageService<T extends TransformOptions = TransformOptions>
extends HostedImageService<T> {
/**
* Gets tthe HTML attributes needed for the server rendered `<img />` element.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/image/test/image-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('SSG images', function () {
});

describe('Inline imports', () => {
it ('includes src, width, and height attributes', () => {
it('includes src, width, and height attributes', () => {
const image = $('#inline');

expect(image.attr('src')).to.equal('/_image/assets/social_506x253.jpg');
Expand Down
12 changes: 6 additions & 6 deletions packages/integrations/image/test/picture-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('SSG pictures', function () {
describe('Local images', () => {
it('includes sources', () => {
const sources = $('#social-jpg source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand All @@ -66,7 +66,7 @@ describe('SSG pictures', function () {
describe('Inline imports', () => {
it('includes sources', () => {
const sources = $('#inline source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand All @@ -93,7 +93,7 @@ describe('SSG pictures', function () {
describe('Remote images', () => {
it('includes sources', () => {
const sources = $('#google source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('SSG pictures', function () {
describe('Local images', () => {
it('includes sources', () => {
const sources = $('#social-jpg source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('SSG pictures', function () {
describe('Local images with inline imports', () => {
it('includes sources', () => {
const sources = $('#inline source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('SSG pictures', function () {
describe('Remote images', () => {
it('includes sources', () => {
const sources = $('#google source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down
12 changes: 6 additions & 6 deletions packages/integrations/image/test/picture-ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('SSR pictures - build', function () {
const $ = cheerio.load(html);

const sources = $('#social-jpg source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('SSR pictures - build', function () {
const $ = cheerio.load(html);

const sources = $('#inline source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('SSR pictures - build', function () {
const $ = cheerio.load(html);

const sources = $('#google source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('SSR images - dev', function () {
describe('Local images', () => {
it('includes sources', () => {
const sources = $('#social-jpg source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('SSR images - dev', function () {
describe('Inline imports', () => {
it('includes sources', () => {
const sources = $('#inline source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand All @@ -251,7 +251,7 @@ describe('SSR images - dev', function () {
describe('Remote images', () => {
it('includes sources', () => {
const sources = $('#google source');

expect(sources.length).to.equal(3);

// TODO: better coverage to verify source props
Expand Down

0 comments on commit d2f6834

Please sign in to comment.