From 0d0460d31184c5019df96163045131c95819d5ca Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 13:58:06 +0530 Subject: [PATCH 01/10] Change engine --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 909cbe813cf1..a32206247db6 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -216,6 +216,6 @@ }, "engines": { "node": ">=18.14.1", - "npm": ">=6.14.0" + "pnpm": ">=8.6.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a45afb8b2b3a..538817c5705a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18263,25 +18263,21 @@ packages: file:packages/astro/test/fixtures/css-assets/packages/font-awesome: resolution: {directory: packages/astro/test/fixtures/css-assets/packages/font-awesome, type: directory} name: '@test/astro-font-awesome-package' - version: 0.0.1 dev: false file:packages/astro/test/fixtures/multiple-renderers/renderers/one: resolution: {directory: packages/astro/test/fixtures/multiple-renderers/renderers/one, type: directory} name: '@test/astro-renderer-one' - version: 1.0.0 dev: false file:packages/astro/test/fixtures/multiple-renderers/renderers/two: resolution: {directory: packages/astro/test/fixtures/multiple-renderers/renderers/two, type: directory} name: '@test/astro-renderer-two' - version: 1.0.0 dev: false file:packages/astro/test/fixtures/solid-component/deps/solid-jsx-component: resolution: {directory: packages/astro/test/fixtures/solid-component/deps/solid-jsx-component, type: directory} name: '@test/solid-jsx-component' - version: 0.0.0 dependencies: solid-js: 1.7.6 dev: false From 10a2fff07613111deb182bd3965fd53b2054a43a Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 14:15:25 +0530 Subject: [PATCH 02/10] Add Test for Sharp --- packages/astro/test/astro-sharp.test.js | 20 +++++++++++++++++++ .../fixtures/astro-sharp/astro.config.mjs | 4 ++++ .../test/fixtures/astro-sharp/package.json | 8 ++++++++ .../astro-sharp/src/pages/index.astro | 15 ++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 packages/astro/test/astro-sharp.test.js create mode 100644 packages/astro/test/fixtures/astro-sharp/astro.config.mjs create mode 100644 packages/astro/test/fixtures/astro-sharp/package.json create mode 100644 packages/astro/test/fixtures/astro-sharp/src/pages/index.astro diff --git a/packages/astro/test/astro-sharp.test.js b/packages/astro/test/astro-sharp.test.js new file mode 100644 index 000000000000..e726071ac775 --- /dev/null +++ b/packages/astro/test/astro-sharp.test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('Sharp Default Setup', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/astro-sharp', + }); + await fixture.build(); + }); + + it('Shows static content', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + expect($('#opt-image').attr('src')).to.include('_astro'); + }); +}); diff --git a/packages/astro/test/fixtures/astro-sharp/astro.config.mjs b/packages/astro/test/fixtures/astro-sharp/astro.config.mjs new file mode 100644 index 000000000000..ad7965b1a917 --- /dev/null +++ b/packages/astro/test/fixtures/astro-sharp/astro.config.mjs @@ -0,0 +1,4 @@ +import { defineConfig } from 'astro/config'; + +// https://astro.build/config +export default defineConfig(); diff --git a/packages/astro/test/fixtures/astro-sharp/package.json b/packages/astro/test/fixtures/astro-sharp/package.json new file mode 100644 index 000000000000..70e66fe5388f --- /dev/null +++ b/packages/astro/test/fixtures/astro-sharp/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/astro-sharp", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro b/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro new file mode 100644 index 000000000000..f8b2f0e82cf5 --- /dev/null +++ b/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro @@ -0,0 +1,15 @@ +--- +import { Image } from 'astro:assets'; + +let title = 'My Page' +--- + + + + {title} + + +

{title}

+ A description of my image. + + From 2b4c564d7c70edaca62ba360906f98e9361ec9d3 Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 14:17:16 +0530 Subject: [PATCH 03/10] add workspace --- pnpm-lock.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 538817c5705a..fc1a65e1a3ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2143,6 +2143,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/astro-sharp: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/astro-sitemap-rss: dependencies: astro: From 7940bae3b746b05e16bda0dbede4fceee49a141a Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 14:28:53 +0530 Subject: [PATCH 04/10] enable assets --- packages/astro/test/fixtures/astro-sharp/astro.config.mjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/astro/test/fixtures/astro-sharp/astro.config.mjs b/packages/astro/test/fixtures/astro-sharp/astro.config.mjs index ad7965b1a917..8788ab31f696 100644 --- a/packages/astro/test/fixtures/astro-sharp/astro.config.mjs +++ b/packages/astro/test/fixtures/astro-sharp/astro.config.mjs @@ -1,4 +1,8 @@ import { defineConfig } from 'astro/config'; // https://astro.build/config -export default defineConfig(); +export default defineConfig({ + experimental: { + assets: true + }, +}); From 29c8919ba55ba96fd86f7bd99d6ddc87d43a24a6 Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 15:03:57 +0530 Subject: [PATCH 05/10] fix tests --- packages/astro/package.json | 3 +++ packages/astro/test/astro-sharp.test.js | 2 +- .../test/fixtures/astro-sharp/src/assets/img.svg | 13 +++++++++++++ .../test/fixtures/astro-sharp/src/pages/index.astro | 6 ++++-- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 packages/astro/test/fixtures/astro-sharp/src/assets/img.svg diff --git a/packages/astro/package.json b/packages/astro/package.json index a32206247db6..b05b3eff2653 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -214,6 +214,9 @@ "srcset-parse": "^1.1.0", "unified": "^10.1.2" }, + "peerDependencies": { + "sharp": "^0.32.1" + }, "engines": { "node": ">=18.14.1", "pnpm": ">=8.6.2" diff --git a/packages/astro/test/astro-sharp.test.js b/packages/astro/test/astro-sharp.test.js index e726071ac775..3a58b71ad872 100644 --- a/packages/astro/test/astro-sharp.test.js +++ b/packages/astro/test/astro-sharp.test.js @@ -12,7 +12,7 @@ describe('Sharp Default Setup', () => { await fixture.build(); }); - it('Shows static content', async () => { + it('Serve optimized image', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); expect($('#opt-image').attr('src')).to.include('_astro'); diff --git a/packages/astro/test/fixtures/astro-sharp/src/assets/img.svg b/packages/astro/test/fixtures/astro-sharp/src/assets/img.svg new file mode 100644 index 000000000000..daf1ffb25443 --- /dev/null +++ b/packages/astro/test/fixtures/astro-sharp/src/assets/img.svg @@ -0,0 +1,13 @@ + diff --git a/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro b/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro index f8b2f0e82cf5..43fa925181e8 100644 --- a/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-sharp/src/pages/index.astro @@ -1,5 +1,7 @@ --- import { Image } from 'astro:assets'; +// @ts-ignore +import myImage from "../assets/img.svg"; let title = 'My Page' --- @@ -9,7 +11,7 @@ let title = 'My Page' {title} -

{title}

- A description of my image. +

{title}

+ descriptive text From 2a5ceba51f4d6a1f46e455958ba57e7e7fa7370d Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 15:18:02 +0530 Subject: [PATCH 06/10] fix condition --- packages/astro/test/astro-sharp.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/test/astro-sharp.test.js b/packages/astro/test/astro-sharp.test.js index 3a58b71ad872..a266c7b0c4ac 100644 --- a/packages/astro/test/astro-sharp.test.js +++ b/packages/astro/test/astro-sharp.test.js @@ -15,6 +15,6 @@ describe('Sharp Default Setup', () => { it('Serve optimized image', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); - expect($('#opt-image').attr('src')).to.include('_astro'); + expect($('img').attr('src')).to.include('_astro'); }); }); From 61d5d99263e7a5033b5b8af50bf540987616dacd Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 17:57:24 +0530 Subject: [PATCH 07/10] re-configure import --- packages/astro/src/assets/services/sharp.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/packages/astro/src/assets/services/sharp.ts b/packages/astro/src/assets/services/sharp.ts index 7f70b926891c..918e59c5493e 100644 --- a/packages/astro/src/assets/services/sharp.ts +++ b/packages/astro/src/assets/services/sharp.ts @@ -1,3 +1,4 @@ +import sharp from 'sharp'; import type { FormatEnum } from 'sharp'; import type { ImageOutputFormat, ImageQualityPreset } from '../types.js'; import { @@ -7,8 +8,6 @@ import { type LocalImageService, } from './service.js'; -let sharp: typeof import('sharp'); - const qualityTable: Record = { low: 25, mid: 50, @@ -16,25 +15,12 @@ const qualityTable: Record = { max: 100, }; -async function loadSharp() { - let sharpImport: typeof import('sharp'); - try { - sharpImport = (await import('sharp')).default; - } catch (e) { - throw new Error('Could not find Sharp. Please install Sharp manually into your project.'); - } - - return sharpImport; -} - const sharpService: LocalImageService = { validateOptions: baseService.validateOptions, getURL: baseService.getURL, parseURL: baseService.parseURL, getHTMLAttributes: baseService.getHTMLAttributes, async transform(inputBuffer, transformOptions) { - if (!sharp) sharp = await loadSharp(); - const transform: BaseServiceTransform = transformOptions as BaseServiceTransform; // Return SVGs as-is From 96f13bdce0485927c3e8ee37ea364115d2e9c8a6 Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 17:58:15 +0530 Subject: [PATCH 08/10] remove peerdep --- packages/astro/package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index b05b3eff2653..a32206247db6 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -214,9 +214,6 @@ "srcset-parse": "^1.1.0", "unified": "^10.1.2" }, - "peerDependencies": { - "sharp": "^0.32.1" - }, "engines": { "node": ">=18.14.1", "pnpm": ">=8.6.2" From 843bdb835603a0696abce2d2a80bbc74a022d9ea Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 18:10:11 +0530 Subject: [PATCH 09/10] remove pnpm --- packages/astro/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index a32206247db6..1ccc93c59514 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -215,7 +215,6 @@ "unified": "^10.1.2" }, "engines": { - "node": ">=18.14.1", - "pnpm": ">=8.6.2" + "node": ">=18.14.1" } } From 7e4454d3fc1d03d3adeb947c2c33339b5262ec3a Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Sun, 13 Aug 2023 23:24:56 +0530 Subject: [PATCH 10/10] removing sharp explicit import (and it works just as expected!) --- packages/astro/src/assets/services/sharp.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/astro/src/assets/services/sharp.ts b/packages/astro/src/assets/services/sharp.ts index 918e59c5493e..1796d70ed178 100644 --- a/packages/astro/src/assets/services/sharp.ts +++ b/packages/astro/src/assets/services/sharp.ts @@ -1,4 +1,3 @@ -import sharp from 'sharp'; import type { FormatEnum } from 'sharp'; import type { ImageOutputFormat, ImageQualityPreset } from '../types.js'; import {