[@astrojs/image] Build all optimized images to dist/assets (#4678)
* fix: build all optimized images to dist/assets * chore: add changeset
This commit is contained in:
parent
ce48931aad
commit
4c05c65a3d
8 changed files with 40 additions and 35 deletions
5
.changeset/proud-gifts-explain.md
Normal file
5
.changeset/proud-gifts-explain.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/image': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Updates the integration to build all optimized images to `dist/assets` during SSG builds
|
|
@ -83,10 +83,10 @@ export async function ssgBuild({ loader, staticImages, config, outDir, logLevel
|
||||||
let outputFile: string;
|
let outputFile: string;
|
||||||
|
|
||||||
if (isRemoteImage(src)) {
|
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);
|
outputFile = fileURLToPath(outputFileURL);
|
||||||
} else {
|
} else {
|
||||||
const outputFileURL = new URL(path.join('./', filename), outDir);
|
const outputFileURL = new URL(path.join('./assets', filename), outDir);
|
||||||
outputFile = fileURLToPath(outputFileURL);
|
outputFile = fileURLToPath(outputFileURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
|
||||||
// Doing this here makes sure that base is ignored when building
|
// Doing this here makes sure that base is ignored when building
|
||||||
// staticImages to /dist, but the rendered HTML will include the
|
// staticImages to /dist, but the rendered HTML will include the
|
||||||
// base prefix for `src`.
|
// 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
|
// Helpers for building static images should only be available for SSG
|
||||||
|
|
|
@ -116,6 +116,6 @@ class SharpService implements SSRImageService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const service = new SharpService();
|
const service: SSRImageService = new SharpService();
|
||||||
|
|
||||||
export default service;
|
export default service;
|
||||||
|
|
|
@ -213,43 +213,43 @@ describe('SSG images - build', function () {
|
||||||
{
|
{
|
||||||
title: 'Local images',
|
title: 'Local images',
|
||||||
id: '#social-jpg',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Filename with spaces',
|
title: 'Filename with spaces',
|
||||||
id: '#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' },
|
size: { width: 768, height: 414, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Inline imports',
|
title: 'Inline imports',
|
||||||
id: '#inline',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#google',
|
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' },
|
size: { width: 544, height: 184, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote without file extension',
|
title: 'Remote without file extension',
|
||||||
id: '#ipsum',
|
id: '#ipsum',
|
||||||
regex: /^\/300_\w{4,10}/,
|
regex: /^\/assets\/300_\w{4,10}/,
|
||||||
size: { width: 200, height: 300, type: 'jpg' },
|
size: { width: 200, height: 300, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Public images',
|
title: 'Public images',
|
||||||
id: '#hero',
|
id: '#hero',
|
||||||
regex: /^\/hero_\w{4,10}.webp/,
|
regex: /^\/assets\/hero_\w{4,10}.webp/,
|
||||||
size: { width: 768, height: 414, type: 'webp' },
|
size: { width: 768, height: 414, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#bg-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' },
|
size: { width: 544, height: 184, type: 'jpg' },
|
||||||
},
|
},
|
||||||
].forEach(({ title, id, regex, size }) => {
|
].forEach(({ title, id, regex, size }) => {
|
||||||
|
@ -289,43 +289,43 @@ describe('SSG images with subpath - build', function () {
|
||||||
{
|
{
|
||||||
title: 'Local images',
|
title: 'Local images',
|
||||||
id: '#social-jpg',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Filename with spaces',
|
title: 'Filename with spaces',
|
||||||
id: '#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' },
|
size: { width: 768, height: 414, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Inline imports',
|
title: 'Inline imports',
|
||||||
id: '#inline',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#google',
|
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' },
|
size: { width: 544, height: 184, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote without file extension',
|
title: 'Remote without file extension',
|
||||||
id: '#ipsum',
|
id: '#ipsum',
|
||||||
regex: /^\/docs\/300_\w{4,10}/,
|
regex: /^\/docs\/assets\/300_\w{4,10}/,
|
||||||
size: { width: 200, height: 300, type: 'jpg' },
|
size: { width: 200, height: 300, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Public images',
|
title: 'Public images',
|
||||||
id: '#hero',
|
id: '#hero',
|
||||||
regex: /^\/docs\/hero_\w{4,10}.webp/,
|
regex: /^\/docs\/assets\/hero_\w{4,10}.webp/,
|
||||||
size: { width: 768, height: 414, type: 'webp' },
|
size: { width: 768, height: 414, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#bg-color',
|
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' },
|
size: { width: 544, height: 184, type: 'jpg' },
|
||||||
},
|
},
|
||||||
].forEach(({ title, id, regex, size }) => {
|
].forEach(({ title, id, regex, size }) => {
|
||||||
|
|
|
@ -195,35 +195,35 @@ describe('SSG pictures - build', function () {
|
||||||
{
|
{
|
||||||
title: 'Local images',
|
title: 'Local images',
|
||||||
id: '#social-jpg',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
alt: 'Social image',
|
alt: 'Social image',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Inline images',
|
title: 'Inline images',
|
||||||
id: '#inline',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
alt: 'Inline social image',
|
alt: 'Inline social image',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#google',
|
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' },
|
size: { width: 544, height: 184, type: 'png' },
|
||||||
alt: 'Google logo',
|
alt: 'Google logo',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote without file extension',
|
title: 'Remote without file extension',
|
||||||
id: '#ipsum',
|
id: '#ipsum',
|
||||||
regex: /^\/300_\w{4,10}/,
|
regex: /^\/assets\/300_\w{4,10}/,
|
||||||
size: { width: 200, height: 300, type: 'jpg' },
|
size: { width: 200, height: 300, type: 'jpg' },
|
||||||
alt: 'ipsum',
|
alt: 'ipsum',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Public images',
|
title: 'Public images',
|
||||||
id: '#hero',
|
id: '#hero',
|
||||||
regex: /^\/hero_\w{4,10}.jpg/,
|
regex: /^\/assets\/hero_\w{4,10}.jpg/,
|
||||||
size: { width: 768, height: 414, type: 'jpg' },
|
size: { width: 768, height: 414, type: 'jpg' },
|
||||||
alt: 'Hero image',
|
alt: 'Hero image',
|
||||||
},
|
},
|
||||||
|
@ -290,35 +290,35 @@ describe('SSG pictures with subpath - build', function () {
|
||||||
{
|
{
|
||||||
title: 'Local images',
|
title: 'Local images',
|
||||||
id: '#social-jpg',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
alt: 'Social image',
|
alt: 'Social image',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Inline images',
|
title: 'Inline images',
|
||||||
id: '#inline',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
alt: 'Inline social image',
|
alt: 'Inline social image',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#google',
|
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' },
|
size: { width: 544, height: 184, type: 'png' },
|
||||||
alt: 'Google logo',
|
alt: 'Google logo',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote without file extension',
|
title: 'Remote without file extension',
|
||||||
id: '#ipsum',
|
id: '#ipsum',
|
||||||
regex: /^\/docs\/300_\w{4,10}/,
|
regex: /^\/docs\/assets\/300_\w{4,10}/,
|
||||||
size: { width: 200, height: 300, type: 'jpg' },
|
size: { width: 200, height: 300, type: 'jpg' },
|
||||||
alt: 'ipsum',
|
alt: 'ipsum',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Public images',
|
title: 'Public images',
|
||||||
id: '#hero',
|
id: '#hero',
|
||||||
regex: /^\/docs\/hero_\w{4,10}.jpg/,
|
regex: /^\/docs\/assets\/hero_\w{4,10}.jpg/,
|
||||||
size: { width: 768, height: 414, type: 'jpg' },
|
size: { width: 768, height: 414, type: 'jpg' },
|
||||||
alt: 'Hero image',
|
alt: 'Hero image',
|
||||||
},
|
},
|
||||||
|
|
|
@ -32,7 +32,7 @@ describe('Image rotation', function () {
|
||||||
it('Landscape images', () => {
|
it('Landscape images', () => {
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 9; i++) {
|
||||||
const image = $(`#landscape-${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('src')).to.match(regex);
|
||||||
expect(image.attr('width')).to.equal('1800');
|
expect(image.attr('width')).to.equal('1800');
|
||||||
|
@ -49,7 +49,7 @@ describe('Image rotation', function () {
|
||||||
it('Portait images', () => {
|
it('Portait images', () => {
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 9; i++) {
|
||||||
const image = $(`#portrait-${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('src')).to.match(regex);
|
||||||
expect(image.attr('width')).to.equal('1200');
|
expect(image.attr('width')).to.equal('1200');
|
||||||
|
|
|
@ -28,31 +28,31 @@ describe('Images in MDX - build', function () {
|
||||||
{
|
{
|
||||||
title: 'Local images',
|
title: 'Local images',
|
||||||
id: '#social-jpg',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Inline imports',
|
title: 'Inline imports',
|
||||||
id: '#inline',
|
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' },
|
size: { width: 506, height: 253, type: 'jpg' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Remote images',
|
title: 'Remote images',
|
||||||
id: '#google',
|
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' },
|
size: { width: 544, height: 184, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Public images',
|
title: 'Public images',
|
||||||
id: '#hero',
|
id: '#hero',
|
||||||
regex: /^\/hero_\w{4,10}.webp/,
|
regex: /^\/assets\/hero_\w{4,10}.webp/,
|
||||||
size: { width: 768, height: 414, type: 'webp' },
|
size: { width: 768, height: 414, type: 'webp' },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Background color',
|
title: 'Background color',
|
||||||
id: '#bg-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' },
|
size: { width: 544, height: 184, type: 'jpg' },
|
||||||
},
|
},
|
||||||
].forEach(({ title, id, regex, size }) => {
|
].forEach(({ title, id, regex, size }) => {
|
||||||
|
|
Loading…
Reference in a new issue