-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
869 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Snapshot report for `tests/svgs.js` | ||
|
||
The actual snapshot is saved in `svgs.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Optimizes SVG image, generates AVIF, WebP, SVG, JPEG formats, and puts them in <picture> | ||
|
||
> Snapshot 1 | ||
'<html><head></head><body><picture><source type="image/avif" srcset="/images/Ghostscript_Tiger-150w.avif 150w, /images/Ghostscript_Tiger-300w.avif 300w, /images/Ghostscript_Tiger-450w.avif 450w, /images/Ghostscript_Tiger-600w.avif 600w, /images/Ghostscript_Tiger-750w.avif 750w, /images/Ghostscript_Tiger-900w.avif 900w, /images/Ghostscript_Tiger-1050w.avif 1050w, /images/Ghostscript_Tiger-1200w.avif 1200w, /images/Ghostscript_Tiger-1350w.avif 1350w" sizes="100vw"><source type="image/webp" srcset="/images/Ghostscript_Tiger-150w.webp 150w, /images/Ghostscript_Tiger-300w.webp 300w, /images/Ghostscript_Tiger-450w.webp 450w, /images/Ghostscript_Tiger-600w.webp 600w, /images/Ghostscript_Tiger-900w.svg 900w" sizes="100vw"><source type="image/svg+xml" srcset="/images/Ghostscript_Tiger-900w.svg 900w" sizes="100vw"><source type="image/jpeg" srcset="/images/Ghostscript_Tiger-150w.jpeg 150w, /images/Ghostscript_Tiger-300w.jpeg 300w, /images/Ghostscript_Tiger-450w.jpeg 450w, /images/Ghostscript_Tiger-900w.svg 900w" sizes="100vw"><img alt="Ghostscript Tiger" loading="lazy" decoding="async" src="/images/Ghostscript_Tiger-150w.jpeg" width="900" height="900"></picture></body></html>' | ||
|
||
## Optimizes SVG image to other formats when `svgShortCircuit: false` | ||
|
||
> Snapshot 1 | ||
'<html><head></head><body><picture><source type="image/svg+xml" srcset="/images/Ghostscript_Tiger-900w.svg 900w" sizes="100vw"><source type="image/avif" srcset="/images/Ghostscript_Tiger-150w.avif 150w, /images/Ghostscript_Tiger-300w.avif 300w, /images/Ghostscript_Tiger-450w.avif 450w, /images/Ghostscript_Tiger-600w.avif 600w, /images/Ghostscript_Tiger-750w.avif 750w, /images/Ghostscript_Tiger-900w.avif 900w, /images/Ghostscript_Tiger-1050w.avif 1050w, /images/Ghostscript_Tiger-1200w.avif 1200w, /images/Ghostscript_Tiger-1350w.avif 1350w" sizes="100vw"><source type="image/webp" srcset="/images/Ghostscript_Tiger-150w.webp 150w, /images/Ghostscript_Tiger-300w.webp 300w, /images/Ghostscript_Tiger-450w.webp 450w, /images/Ghostscript_Tiger-600w.webp 600w, /images/Ghostscript_Tiger-750w.webp 750w, /images/Ghostscript_Tiger-900w.webp 900w, /images/Ghostscript_Tiger-1050w.webp 1050w, /images/Ghostscript_Tiger-1200w.webp 1200w, /images/Ghostscript_Tiger-1350w.webp 1350w" sizes="100vw"><source type="image/jpeg" srcset="/images/Ghostscript_Tiger-150w.jpeg 150w, /images/Ghostscript_Tiger-300w.jpeg 300w, /images/Ghostscript_Tiger-450w.jpeg 450w, /images/Ghostscript_Tiger-600w.jpeg 600w, /images/Ghostscript_Tiger-750w.jpeg 750w, /images/Ghostscript_Tiger-900w.jpeg 900w, /images/Ghostscript_Tiger-1050w.jpeg 1050w, /images/Ghostscript_Tiger-1200w.jpeg 1200w, /images/Ghostscript_Tiger-1350w.jpeg 1350w" sizes="100vw"><img alt="Ghostscript Tiger" loading="lazy" decoding="async" src="/images/Ghostscript_Tiger-150w.jpeg" width="1350" height="1350"></picture></body></html>' | ||
|
||
## Process SVG image and skip to other formats when `svgShortCircuit: true` | ||
|
||
> Snapshot 1 | ||
'<html><head></head><body><img alt="Ghostscript Tiger" loading="lazy" decoding="async" src="/images/Ghostscript_Tiger-900w.svg" width="900" height="900"></body></html>' |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const path = require("path"); | ||
const test = require("ava"); | ||
const { rimraf } = require("rimraf"); | ||
const imageSize = require("image-size"); | ||
const { existsSync } = require("node:fs"); | ||
|
||
const img2picture = require("../lib/img2picture.js"); | ||
const { filenameFormat } = require("./utils.js"); | ||
|
||
const sourcePath = path.join("tests/fixtures"); | ||
const outputBase = path.join("tests/output/svgs"); | ||
|
||
test.before("Cleanup Output Images", async () => rimraf(outputBase)); | ||
test.after.always("Cleanup Output Images", async () => rimraf(outputBase)); | ||
|
||
const baseOptions = { | ||
eleventyInputDir: sourcePath, | ||
imagesOutputDir: outputBase, | ||
urlPath: "/images/", | ||
filenameFormat, | ||
formats: ["avif", "webp", "svg", "jpeg"], | ||
}; | ||
|
||
test("Optimizes SVG image, generates AVIF, WebP, SVG, JPEG formats, and puts them in <picture>", async (t) => { | ||
const input = | ||
'<img src="/images/Ghostscript_Tiger.svg" alt="Ghostscript Tiger">'; | ||
const outputPath = "file.html"; | ||
const imagesOutputDir = path.join(outputBase, "default"); | ||
const transformer = img2picture({ | ||
...baseOptions, | ||
imagesOutputDir, | ||
}); | ||
const result = await transformer(input, outputPath); | ||
t.snapshot(result); | ||
|
||
const svgFilePath = path.join(imagesOutputDir, "Ghostscript_Tiger-900w.svg"); | ||
t.true(existsSync(svgFilePath)); | ||
|
||
const { width: smallestImgWidth } = imageSize( | ||
path.join(imagesOutputDir, "Ghostscript_Tiger-150w.jpeg"), | ||
); | ||
const { width: largestImgWidth } = imageSize( | ||
path.join(imagesOutputDir, "Ghostscript_Tiger-1350w.jpeg"), | ||
); | ||
|
||
t.is(smallestImgWidth, 150); | ||
t.is(largestImgWidth, 1350); | ||
}); | ||
|
||
test("Optimizes SVG image to other formats when `svgShortCircuit: false`", async (t) => { | ||
const input = | ||
'<img src="/images/Ghostscript_Tiger.svg" alt="Ghostscript Tiger">'; | ||
const outputPath = "file.html"; | ||
|
||
const transformer = img2picture({ | ||
...baseOptions, | ||
svgShortCircuit: false, | ||
imagesOutputDir: path.join(outputBase, "svgShortCircuit-false"), | ||
}); | ||
const result = await transformer(input, outputPath); | ||
t.snapshot(result); | ||
}); | ||
|
||
test("Process SVG image and skip to other formats when `svgShortCircuit: true`", async (t) => { | ||
const input = | ||
'<img src="/images/Ghostscript_Tiger.svg" alt="Ghostscript Tiger">'; | ||
const outputPath = "file.html"; | ||
|
||
const transformer = img2picture({ | ||
...baseOptions, | ||
svgShortCircuit: true, | ||
imagesOutputDir: path.join(outputBase, "svgShortCircuit-true"), | ||
}); | ||
const result = await transformer(input, outputPath); | ||
t.snapshot(result); | ||
}); |