Compare commits
No commits in common. "4cb2af8e9d54ffed5db48c6a56eeb87676ae6e6e" and "ea61d9fc984475b7995ab197c23758b9332fce1a" have entirely different histories.
4cb2af8e9d
...
ea61d9fc98
7 changed files with 30 additions and 39 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -28,6 +28,3 @@ pnpm-debug.log*
|
||||||
/playwright-report/
|
/playwright-report/
|
||||||
/blob-report/
|
/blob-report/
|
||||||
/playwright/.cache/
|
/playwright/.cache/
|
||||||
|
|
||||||
# Custom
|
|
||||||
src/assets/
|
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,18 @@ import Layout from '../layouts/Layout.astro'
|
||||||
import Button from '../templates/components/Button/Button.tsx'
|
import Button from '../templates/components/Button/Button.tsx'
|
||||||
import Image from '../templates/components/Image/Image'
|
import Image from '../templates/components/Image/Image'
|
||||||
import Page from '../templates/components/Page/Page'
|
import Page from '../templates/components/Page/Page'
|
||||||
import sampleA from '../templates/images/sample.avif'
|
|
||||||
import sampleW from '../templates/images/sample.avif'
|
|
||||||
import OptimizeImage from '../templates/components/Optimizer/OptimizeImage'
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Home">
|
<Layout title="Home">
|
||||||
<Page alignment="column">
|
<Page alignment='column'>
|
||||||
<h1>Built-in Components</h1>
|
<h1>Built-in Components</h1>
|
||||||
<Button edges="rounded" label="Back to Home" to="/" />
|
<Button edges='rounded' label="Back to Home" to="/" />
|
||||||
|
|
||||||
<section class="image">
|
<section class="image">
|
||||||
<!-- <OptimizeImage src="sample.png" size={400} /> -->
|
<Image src="sample.png" alt="Example Image" size={400} />
|
||||||
<Image avif={sampleA.src} webp={sampleW.src} alt="Example Image" size={400} />
|
<Image src="sample.png" alt="Example Image" size={400} />
|
||||||
<Image avif={sampleA.src} webp={sampleW.src} alt="Example Image" size={400} />
|
<Image src="sample.png" alt="Example Image" size={400} />
|
||||||
<Image avif={sampleA.src} webp={sampleW.src} alt="Example Image" size={400} />
|
<Image src="sample.png" alt="Example Image" size={400} />
|
||||||
<Image avif={sampleA.src} webp={sampleW.src} alt="Example Image" size={400} />
|
|
||||||
</section>
|
</section>
|
||||||
</Page>
|
</Page>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,35 @@
|
||||||
|
import sharp from 'sharp'
|
||||||
|
import fs from 'fs'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
avif: string
|
src: string
|
||||||
webp: string
|
|
||||||
size?: number
|
size?: number
|
||||||
alt?: string
|
alt?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const convertImage = async (props: Props) => {
|
||||||
|
const avifOutputPath = `src/assets/optimized/${props.src.split('.').slice(0, -1).join('.')}.avif`
|
||||||
|
const webpOutputPath = `src/assets/optimized/${props.src.split('.').slice(0, -1).join('.')}.webp`
|
||||||
|
|
||||||
|
if (!fs.existsSync(webpOutputPath) || !fs.existsSync(avifOutputPath)) {
|
||||||
|
const avifBuffer = await sharp(`src/assets/images/${props.src}`).avif({ quality: 60 }).resize(props.size).toBuffer()
|
||||||
|
await sharp(avifBuffer).toFile(avifOutputPath)
|
||||||
|
|
||||||
|
const webpBuffer = await sharp(`src/assets/images/${props.src}`).webp({ quality: 75 }).resize(props.size).toBuffer()
|
||||||
|
await sharp(webpBuffer).toFile(webpOutputPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default (props: Props) => {
|
export default (props: Props) => {
|
||||||
|
const imageSrc = `src/assets/optimized/${props.src.split('.').slice(0, -1).join('.')}.webp`
|
||||||
|
convertImage(props)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<picture>
|
<picture>
|
||||||
<source srcset={props.avif} type="image/avif" />
|
<source srcset={imageSrc.replace(/\.webp$/, '.avif')} type="image/avif" />
|
||||||
<source srcset={props.webp} type="image/webp" />
|
<source srcset={imageSrc} type="image/webp" />
|
||||||
<img width={props.size} height="auto" decoding="async" loading="lazy" alt={props.alt} />
|
<img src={imageSrc} width={props.size} height="auto" decoding="async" loading="lazy" alt={props.alt} />
|
||||||
</picture>
|
</picture>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
import sharp from 'sharp'
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
src: string
|
|
||||||
size?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
const convertImage = async (props: Props) => {
|
|
||||||
const avifOutputPath = `src/templates/images/${props.src.split('.').slice(0, -1).join('.')}.avif`
|
|
||||||
const webpOutputPath = `src/templates/images/${props.src.split('.').slice(0, -1).join('.')}.webp`
|
|
||||||
|
|
||||||
const avifBuffer = await sharp(`src/assets/images/${props.src}`).avif({ quality: 60 }).resize(props.size).toBuffer()
|
|
||||||
await sharp(avifBuffer).toFile(avifOutputPath)
|
|
||||||
|
|
||||||
const webpBuffer = await sharp(`src/assets/images/${props.src}`).webp({ quality: 75 }).resize(props.size).toBuffer()
|
|
||||||
await sharp(webpBuffer).toFile(webpOutputPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default (props: Props) => {
|
|
||||||
convertImage(props)
|
|
||||||
}
|
|
||||||
|
|
@ -2,6 +2,7 @@ import sharp from 'sharp'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
size?: number
|
size?: number
|
||||||
|
alt?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const convertLogo = async (props: Props) => {
|
const convertLogo = async (props: Props) => {
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.9 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.9 KiB |
Loading…
Add table
Add a link
Reference in a new issue