Skip to content

Commit

Permalink
[@astrojs/image] Build all optimized images to dist/assets (#4678)
Browse files Browse the repository at this point in the history
* fix: build all optimized images to dist/assets

* chore: add changeset
  • Loading branch information
Tony Sullivan committed Sep 8, 2022
1 parent ce48931 commit 4c05c65
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-gifts-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Updates the integration to build all optimized images to `dist/assets` during SSG builds
4 changes: 2 additions & 2 deletions packages/integrations/image/src/build/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export async function ssgBuild({ loader, staticImages, config, outDir, logLevel
let outputFile: string;

if (isRemoteImage(src)) {
const outputFileURL = new URL(path.join('./', path.basename(filename)), outDir);
const outputFileURL = new URL(path.join('./assets', path.basename(filename)), outDir);
outputFile = fileURLToPath(outputFileURL);
} else {
const outputFileURL = new URL(path.join('./', filename), outDir);
const outputFileURL = new URL(path.join('./assets', filename), outDir);
outputFile = fileURLToPath(outputFileURL);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
// Doing this here makes sure that base is ignored when building
// staticImages to /dist, but the rendered HTML will include the
// base prefix for `src`.
return prependForwardSlash(joinPaths(_config.base, filename));
return prependForwardSlash(joinPaths(_config.base, 'assets', filename));
}

// Helpers for building static images should only be available for SSG
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/image/src/loaders/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ class SharpService implements SSRImageService {
}
}

const service = new SharpService();
const service: SSRImageService = new SharpService();

export default service;
28 changes: 14 additions & 14 deletions packages/integrations/image/test/image-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,43 +213,43 @@ describe('SSG images - build', function () {
{
title: 'Local images',
id: '#social-jpg',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Filename with spaces',
id: '#spaces',
regex: /^\/introducing astro.\w{8}_\w{4,10}.webp/,
regex: /^\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
size: { width: 768, height: 414, type: 'webp' },
},
{
title: 'Inline imports',
id: '#inline',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Remote images',
id: '#google',
regex: /^\/googlelogo_color_272x92dp_\w{4,10}.webp/,
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
size: { width: 544, height: 184, type: 'webp' },
},
{
title: 'Remote without file extension',
id: '#ipsum',
regex: /^\/300_\w{4,10}/,
regex: /^\/assets\/300_\w{4,10}/,
size: { width: 200, height: 300, type: 'jpg' },
},
{
title: 'Public images',
id: '#hero',
regex: /^\/hero_\w{4,10}.webp/,
regex: /^\/assets\/hero_\w{4,10}.webp/,
size: { width: 768, height: 414, type: 'webp' },
},
{
title: 'Remote images',
id: '#bg-color',
regex: /^\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
size: { width: 544, height: 184, type: 'jpg' },
},
].forEach(({ title, id, regex, size }) => {
Expand Down Expand Up @@ -289,43 +289,43 @@ describe('SSG images with subpath - build', function () {
{
title: 'Local images',
id: '#social-jpg',
regex: /^\/docs\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Filename with spaces',
id: '#spaces',
regex: /^\/docs\/introducing astro.\w{8}_\w{4,10}.webp/,
regex: /^\/docs\/assets\/introducing astro.\w{8}_\w{4,10}.webp/,
size: { width: 768, height: 414, type: 'webp' },
},
{
title: 'Inline imports',
id: '#inline',
regex: /^\/docs\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Remote images',
id: '#google',
regex: /^\/docs\/googlelogo_color_272x92dp_\w{4,10}.webp/,
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
size: { width: 544, height: 184, type: 'webp' },
},
{
title: 'Remote without file extension',
id: '#ipsum',
regex: /^\/docs\/300_\w{4,10}/,
regex: /^\/docs\/assets\/300_\w{4,10}/,
size: { width: 200, height: 300, type: 'jpg' },
},
{
title: 'Public images',
id: '#hero',
regex: /^\/docs\/hero_\w{4,10}.webp/,
regex: /^\/docs\/assets\/hero_\w{4,10}.webp/,
size: { width: 768, height: 414, type: 'webp' },
},
{
title: 'Remote images',
id: '#bg-color',
regex: /^\/docs\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
size: { width: 544, height: 184, type: 'jpg' },
},
].forEach(({ title, id, regex, size }) => {
Expand Down
20 changes: 10 additions & 10 deletions packages/integrations/image/test/picture-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,35 @@ describe('SSG pictures - build', function () {
{
title: 'Local images',
id: '#social-jpg',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
alt: 'Social image',
},
{
title: 'Inline images',
id: '#inline',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
alt: 'Inline social image',
},
{
title: 'Remote images',
id: '#google',
regex: /^\/googlelogo_color_272x92dp_\w{4,10}.png/,
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.png/,
size: { width: 544, height: 184, type: 'png' },
alt: 'Google logo',
},
{
title: 'Remote without file extension',
id: '#ipsum',
regex: /^\/300_\w{4,10}/,
regex: /^\/assets\/300_\w{4,10}/,
size: { width: 200, height: 300, type: 'jpg' },
alt: 'ipsum',
},
{
title: 'Public images',
id: '#hero',
regex: /^\/hero_\w{4,10}.jpg/,
regex: /^\/assets\/hero_\w{4,10}.jpg/,
size: { width: 768, height: 414, type: 'jpg' },
alt: 'Hero image',
},
Expand Down Expand Up @@ -290,35 +290,35 @@ describe('SSG pictures with subpath - build', function () {
{
title: 'Local images',
id: '#social-jpg',
regex: /^\/docs\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
alt: 'Social image',
},
{
title: 'Inline images',
id: '#inline',
regex: /^\/docs\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/docs\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
alt: 'Inline social image',
},
{
title: 'Remote images',
id: '#google',
regex: /^\/docs\/googlelogo_color_272x92dp_\w{4,10}.png/,
regex: /^\/docs\/assets\/googlelogo_color_272x92dp_\w{4,10}.png/,
size: { width: 544, height: 184, type: 'png' },
alt: 'Google logo',
},
{
title: 'Remote without file extension',
id: '#ipsum',
regex: /^\/docs\/300_\w{4,10}/,
regex: /^\/docs\/assets\/300_\w{4,10}/,
size: { width: 200, height: 300, type: 'jpg' },
alt: 'ipsum',
},
{
title: 'Public images',
id: '#hero',
regex: /^\/docs\/hero_\w{4,10}.jpg/,
regex: /^\/docs\/assets\/hero_\w{4,10}.jpg/,
size: { width: 768, height: 414, type: 'jpg' },
alt: 'Hero image',
},
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/image/test/rotation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Image rotation', function () {
it('Landscape images', () => {
for (let i = 0; i < 9; i++) {
const image = $(`#landscape-${i}`);
const regex = new RegExp(`\^/Landscape_${i}.\\w{8}_\\w{4,10}.jpg`);
const regex = new RegExp(`\^/assets\/Landscape_${i}.\\w{8}_\\w{4,10}.jpg`);

expect(image.attr('src')).to.match(regex);
expect(image.attr('width')).to.equal('1800');
Expand All @@ -49,7 +49,7 @@ describe('Image rotation', function () {
it('Portait images', () => {
for (let i = 0; i < 9; i++) {
const image = $(`#portrait-${i}`);
const regex = new RegExp(`\^/Portrait_${i}.\\w{8}_\\w{4,10}.jpg`);
const regex = new RegExp(`\^/assets\/Portrait_${i}.\\w{8}_\\w{4,10}.jpg`);

expect(image.attr('src')).to.match(regex);
expect(image.attr('width')).to.equal('1200');
Expand Down
10 changes: 5 additions & 5 deletions packages/integrations/image/test/with-mdx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ describe('Images in MDX - build', function () {
{
title: 'Local images',
id: '#social-jpg',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Inline imports',
id: '#inline',
regex: /^\/social.\w{8}_\w{4,10}.jpg/,
regex: /^\/assets\/social.\w{8}_\w{4,10}.jpg/,
size: { width: 506, height: 253, type: 'jpg' },
},
{
title: 'Remote images',
id: '#google',
regex: /^\/googlelogo_color_272x92dp_\w{4,10}.webp/,
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.webp/,
size: { width: 544, height: 184, type: 'webp' },
},
{
title: 'Public images',
id: '#hero',
regex: /^\/hero_\w{4,10}.webp/,
regex: /^\/assets\/hero_\w{4,10}.webp/,
size: { width: 768, height: 414, type: 'webp' },
},
{
title: 'Background color',
id: '#bg-color',
regex: /^\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
regex: /^\/assets\/googlelogo_color_272x92dp_\w{4,10}.jpeg/,
size: { width: 544, height: 184, type: 'jpg' },
},
].forEach(({ title, id, regex, size }) => {
Expand Down

0 comments on commit 4c05c65

Please sign in to comment.