diff --git a/.changeset/new-crews-return.md b/.changeset/new-crews-return.md new file mode 100644 index 000000000..f608b9ae1 --- /dev/null +++ b/.changeset/new-crews-return.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Adds support for image srcset to the build diff --git a/packages/astro/src/build.ts b/packages/astro/src/build.ts index 97b616b1f..4fd22f1bf 100644 --- a/packages/astro/src/build.ts +++ b/packages/astro/src/build.ts @@ -265,7 +265,7 @@ export function findDeps(html: string, { astroConfig, srcPath }: { astroConfig: const $ = cheerio.load(html); - $('script').each((i, el) => { + $('script').each((_i, el) => { const src = $(el).attr('src'); if (src) { if (isRemote(src)) return; @@ -283,7 +283,7 @@ export function findDeps(html: string, { astroConfig, srcPath }: { astroConfig: } }); - $('link[href]').each((i, el) => { + $('link[href]').each((_i, el) => { const href = $(el).attr('href'); if (href && !isRemote(href) && ($(el).attr('rel') === 'stylesheet' || $(el).attr('type') === 'text/css' || href.endsWith('.css'))) { const dist = getDistPath(href, { astroConfig, srcPath }); @@ -291,13 +291,22 @@ export function findDeps(html: string, { astroConfig, srcPath }: { astroConfig: } }); - $('img[src]').each((i, el) => { + $('img[src]').each((_i, el) => { const src = $(el).attr('src'); if (src && !isRemote(src)) { pageDeps.images.add(getDistPath(src, { astroConfig, srcPath })); } }); + $('img[srcset]').each((_i, el) => { + const srcset = $(el).attr('srcset') || ''; + const sources = srcset.split(','); + const srces = sources.map(s => s.trim().split(' ')[0]); + for(const src of srces) { + pageDeps.images.add(getDistPath(src, { astroConfig, srcPath })); + } + }); + // important: preserve the scan order of deps! order matters on pages return pageDeps; diff --git a/packages/astro/test/astro-assets.test.js b/packages/astro/test/astro-assets.test.js new file mode 100644 index 000000000..c82d34aa6 --- /dev/null +++ b/packages/astro/test/astro-assets.test.js @@ -0,0 +1,25 @@ +import { suite } from 'uvu'; +import * as assert from 'uvu/assert'; +import { setup, setupBuild } from './helpers.js'; + +const Assets = suite('Assets'); + +setup(Assets, './fixtures/astro-assets'); +setupBuild(Assets, './fixtures/astro-assets'); + +Assets('srcset is copied in the build', async ({ build, readFile }) => { + await build().catch((err) => { + assert.ok(!err, 'Error during the build'); + }); + + let oneX = await readFile('/_astro/src/images/twitter.png'); + assert.ok(oneX, 'built the base image'); + + let twoX = await readFile('/_astro/src/images/twitter@2x.png'); + assert.ok(twoX, 'built the 2x image'); + + let threeX = await readFile('/_astro/src/images/twitter@3x.png'); + assert.ok(threeX, 'build the 3x image'); +}); + +Assets.run(); \ No newline at end of file diff --git a/packages/astro/test/fixtures/astro-assets/snowpack.config.json b/packages/astro/test/fixtures/astro-assets/snowpack.config.json new file mode 100644 index 000000000..8f034781d --- /dev/null +++ b/packages/astro/test/fixtures/astro-assets/snowpack.config.json @@ -0,0 +1,3 @@ +{ + "workspaceRoot": "../../../../../" +} diff --git a/packages/astro/test/fixtures/astro-assets/src/images/twitter.png b/packages/astro/test/fixtures/astro-assets/src/images/twitter.png new file mode 100644 index 000000000..ad4cae1e9 Binary files /dev/null and b/packages/astro/test/fixtures/astro-assets/src/images/twitter.png differ diff --git a/packages/astro/test/fixtures/astro-assets/src/images/twitter@2x.png b/packages/astro/test/fixtures/astro-assets/src/images/twitter@2x.png new file mode 100644 index 000000000..ad89e72c3 Binary files /dev/null and b/packages/astro/test/fixtures/astro-assets/src/images/twitter@2x.png differ diff --git a/packages/astro/test/fixtures/astro-assets/src/images/twitter@3x.png b/packages/astro/test/fixtures/astro-assets/src/images/twitter@3x.png new file mode 100644 index 000000000..2fe263f99 Binary files /dev/null and b/packages/astro/test/fixtures/astro-assets/src/images/twitter@3x.png differ diff --git a/packages/astro/test/fixtures/astro-assets/src/pages/index.astro b/packages/astro/test/fixtures/astro-assets/src/pages/index.astro new file mode 100644 index 000000000..4ce1d07ef --- /dev/null +++ b/packages/astro/test/fixtures/astro-assets/src/pages/index.astro @@ -0,0 +1,10 @@ + +This Site + + +

Icons

+ + + \ No newline at end of file