Skip to content

Commit 8aaaa85

Browse files
oorestisimesidharthachatterjee
authored andcommitted
feat(gatsby-plugin-sharp): Add tracedSVG option in fluid and fixed processors (#11981)
* Add tracedSVG option in fluid and fixed processors Fix: #11912 * Add unit tests
1 parent 7d3bcad commit 8aaaa85

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap

+33
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Object {
2424
"srcSet": "/static/1234/56b42/test.png 200w,
2525
/static/1234/a3030/test.png 281w",
2626
"srcSetType": "image/png",
27+
"tracedSVG": undefined,
2728
}
2829
`;
2930

@@ -41,6 +42,7 @@ Object {
4142
"srcSet": "/static/1234/4df82/test.png 200w,
4243
/static/1234/67b29/test.png 281w",
4344
"srcSetType": "image/png",
45+
"tracedSVG": undefined,
4446
}
4547
`;
4648

@@ -57,6 +59,7 @@ Object {
5759
"src": "/static/1234/4ce1a/test.png",
5860
"srcSet": "/static/1234/4ce1a/test.png 1w",
5961
"srcSetType": "image/png",
62+
"tracedSVG": undefined,
6063
}
6164
`;
6265

@@ -70,3 +73,33 @@ Object {
7073
"width": 746,
7174
}
7275
`;
76+
77+
exports[`gatsby-plugin-sharp tracedSVG runs on demand 1`] = `
78+
Object {
79+
"aspectRatio": 1,
80+
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGElEQVQ4y2P4TwFgGNU8qnlU86jmgdUMAOBEq42KgAyMAAAAAElFTkSuQmCC",
81+
"height": 1,
82+
"originalName": undefined,
83+
"src": "/static/1234/05eb0/test.png",
84+
"srcSet": "/static/1234/05eb0/test.png 1x",
85+
"tracedSVG": "data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100' version='1'/%3e",
86+
"width": 1,
87+
}
88+
`;
89+
90+
exports[`gatsby-plugin-sharp tracedSVG runs on demand 2`] = `
91+
Object {
92+
"aspectRatio": 1,
93+
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGElEQVQ4y2P4TwFgGNU8qnlU86jmgdUMAOBEq42KgAyMAAAAAElFTkSuQmCC",
94+
"density": 72,
95+
"originalImg": "/static/1234/c1fac/test.png",
96+
"originalName": undefined,
97+
"presentationHeight": 1,
98+
"presentationWidth": 1,
99+
"sizes": "(max-width: 1px) 100vw, 1px",
100+
"src": "/static/1234/c1fac/test.png",
101+
"srcSet": "/static/1234/c1fac/test.png 1w",
102+
"srcSetType": "image/png",
103+
"tracedSVG": "data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100' version='1'/%3e",
104+
}
105+
`;

packages/gatsby-plugin-sharp/src/__tests__/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,53 @@ describe(`gatsby-plugin-sharp`, () => {
319319
expect(result).toMatchSnapshot()
320320
})
321321
})
322+
323+
describe(`tracedSVG`, () => {
324+
it(`doesn't always run`, async () => {
325+
const args = {
326+
maxWidth: 100,
327+
width: 100,
328+
tracedSVG: { color: `#FF0000` },
329+
}
330+
331+
let result = await fixed({
332+
file,
333+
args,
334+
})
335+
336+
expect(result.tracedSVG).toBeUndefined()
337+
338+
result = await fluid({
339+
file,
340+
args,
341+
})
342+
343+
expect(result.tracedSVG).toBeUndefined()
344+
})
345+
346+
it(`runs on demand`, async () => {
347+
const args = {
348+
maxWidth: 100,
349+
width: 100,
350+
generateTracedSVG: true,
351+
tracedSVG: { color: `#FF0000` },
352+
}
353+
354+
let result = await fixed({
355+
file,
356+
args,
357+
})
358+
359+
expect(result).toMatchSnapshot()
360+
361+
result = await fluid({
362+
file,
363+
args,
364+
})
365+
366+
expect(result).toMatchSnapshot()
367+
})
368+
})
322369
})
323370

324371
function getFileObject(absolutePath, name = `test`) {

packages/gatsby-plugin-sharp/src/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,18 @@ async function base64(arg) {
557557
return await memoizedBase64(arg)
558558
}
559559

560+
async function getTracedSVG(options, file) {
561+
if (options.generateTracedSVG && options.tracedSVG) {
562+
const tracedSVG = await traceSVG({
563+
file,
564+
args: options.tracedSVG,
565+
fileArgs: options,
566+
})
567+
return tracedSVG
568+
}
569+
return undefined
570+
}
571+
560572
async function fluid({ file, args = {}, reporter, cache }) {
561573
const options = healOptions(pluginOptions, args, {})
562574
// Account for images with a high pixel density. We assume that these types of
@@ -686,6 +698,8 @@ async function fluid({ file, args = {}, reporter, cache }) {
686698
// Get base64 version
687699
const base64Image = await base64({ file, args: base64Args, reporter, cache })
688700

701+
const tracedSVG = await getTracedSVG(options, file)
702+
689703
// Construct src and srcSet strings.
690704
const originalImg = _.maxBy(images, image => image.width).src
691705
const fallbackSrc = _.minBy(images, image =>
@@ -729,6 +743,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
729743
density,
730744
presentationWidth,
731745
presentationHeight,
746+
tracedSVG,
732747
}
733748
}
734749

@@ -796,6 +811,8 @@ async function fixed({ file, args = {}, reporter, cache }) {
796811
// Get base64 version
797812
const base64Image = await base64({ file, args: base64Args, reporter, cache })
798813

814+
const tracedSVG = await getTracedSVG(options, file)
815+
799816
const fallbackSrc = images[0].src
800817
const srcSet = images
801818
.map((image, i) => {
@@ -829,6 +846,7 @@ async function fixed({ file, args = {}, reporter, cache }) {
829846
src: fallbackSrc,
830847
srcSet,
831848
originalName: originalName,
849+
tracedSVG,
832850
}
833851
}
834852

0 commit comments

Comments
 (0)