Skip to content

Commit a246011

Browse files
authored
feat(gatsby-plugin-utils): enable tracedsvg (#35328)
1 parent dfa5991 commit a246011

File tree

19 files changed

+406
-317
lines changed

19 files changed

+406
-317
lines changed

e2e-tests/development-runtime/cypress/integration/remote-file/gatsby-plugin-image.js

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ describe(`remote-file`, () => {
123123
expect($el.prop("tagName")).to.be.equal("IMG")
124124
expect($el.prop("src")).to.contain("data:image/jpg;base64")
125125
})
126+
cy.get(".constrained_traced [data-placeholder-image]")
127+
.first()
128+
.should($el => {
129+
expect($el.prop("tagName")).to.be.equal("IMG")
130+
expect($el.prop("src")).to.contain("data:image/svg+xml,%3csvg")
131+
})
126132
cy.get(".full [data-placeholder-image]")
127133
.first()
128134
.should($el => {

e2e-tests/development-runtime/src/pages/remote-file.js

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const RemoteFile = ({ data }) => {
3232
image={node.constrained}
3333
alt=""
3434
/>
35+
<GatsbyImage
36+
className="constrained_traced"
37+
image={node.constrained_traced}
38+
alt=""
39+
/>
3540
<GatsbyImage className="full" image={node.full} alt="" />
3641
</div>
3742
</div>
@@ -64,6 +69,11 @@ export const pageQuery = graphql`
6469
width: 300
6570
placeholder: BLURRED
6671
)
72+
constrained_traced: gatsbyImage(
73+
layout: CONSTRAINED
74+
width: 300
75+
placeholder: TRACED_SVG
76+
)
6777
full: gatsbyImage(layout: FULL_WIDTH, width: 500, placeholder: NONE)
6878
}
6979
}

e2e-tests/production-runtime/cypress/integration/remote-file.js

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ describe(`remote-file`, () => {
119119
expect($el.prop("tagName")).to.be.equal("IMG")
120120
expect($el.prop("src")).to.contain("data:image/jpg;base64")
121121
})
122+
cy.get(".constrained_traced [data-placeholder-image]")
123+
.first()
124+
.should($el => {
125+
expect($el.prop("tagName")).to.be.equal("IMG")
126+
expect($el.prop("src")).to.contain("data:image/svg+xml,%3csvg")
127+
})
122128
cy.get(".full [data-placeholder-image]")
123129
.first()
124130
.should($el => {

e2e-tests/production-runtime/src/pages/remote-file.js

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const RemoteFile = ({ data }) => {
2929
image={node.constrained}
3030
alt=""
3131
/>
32+
<GatsbyImage
33+
className="constrained_traced"
34+
image={node.constrained_traced}
35+
alt=""
36+
/>
3237
<GatsbyImage className="full" image={node.full} alt="" />
3338
</div>
3439
</div>
@@ -61,6 +66,11 @@ export const pageQuery = graphql`
6166
width: 300
6267
placeholder: BLURRED
6368
)
69+
constrained_traced: gatsbyImage(
70+
layout: CONSTRAINED
71+
width: 300
72+
placeholder: TRACED_SVG
73+
)
6474
full: gatsbyImage(layout: FULL_WIDTH, width: 500, placeholder: NONE)
6575
}
6676
}

packages/gatsby-plugin-sharp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.15.4",
11+
"@gatsbyjs/potrace": "^2.2.0",
1112
"async": "^3.2.3",
1213
"bluebird": "^3.7.2",
1314
"debug": "^4.3.4",
@@ -19,7 +20,6 @@
1920
"got": "^11.8.3",
2021
"lodash": "^4.17.21",
2122
"mini-svg-data-uri": "^1.4.4",
22-
"potrace": "^2.1.8",
2323
"probe-image-size": "^7.2.3",
2424
"progress": "^2.0.3",
2525
"semver": "^7.3.5",

packages/gatsby-plugin-sharp/src/trace-svg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ exports.notMemoizedtraceSVG = async ({ file, args, fileArgs, reporter }) => {
128128
})
129129

130130
const svgToMiniDataURI = require(`mini-svg-data-uri`)
131-
const potrace = require(`potrace`)
131+
const potrace = require(`@gatsbyjs/potrace`)
132132
const trace = promisify(potrace.trace)
133133

134134
const defaultArgs = {

packages/gatsby-plugin-utils/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@
4747
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-utils#readme",
4848
"dependencies": {
4949
"@babel/runtime": "^7.15.4",
50+
"@gatsbyjs/potrace": "^2.2.0",
5051
"fs-extra": "^10.0.0",
5152
"gatsby-core-utils": "^3.13.0-next.0",
5253
"gatsby-sharp": "^0.7.0-next.0",
5354
"graphql-compose": "^9.0.7",
5455
"import-from": "^4.0.0",
5556
"joi": "^17.4.2",
56-
"mime": "^3.0.0"
57+
"mime": "^3.0.0",
58+
"mini-svg-data-uri": "^1.4.4",
59+
"svgo": "^2.8.0"
5760
},
5861
"devDependencies": {
5962
"@babel/cli": "^7.15.4",

packages/gatsby-plugin-utils/src/polyfill-remote-file/__tests__/gatsby-image-resolver.ts

+27
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,33 @@ describe(`gatsbyImageData`, () => {
681681
`)
682682
})
683683

684+
it(`should generate tracedSVG placeholder`, async () => {
685+
fetchRemoteFile.mockResolvedValueOnce(
686+
path.join(__dirname, `__fixtures__`, `dog-portrait.jpg`)
687+
)
688+
const fixedResult = await gatsbyImageResolver(
689+
portraitSource,
690+
{
691+
layout: `fixed`,
692+
width: 300,
693+
placeholder: PlaceholderType.TRACED_SVG,
694+
},
695+
actions
696+
)
697+
698+
expect(fetchRemoteFile).toHaveBeenCalledTimes(1)
699+
expect(fetchRemoteFile).toHaveBeenCalledWith({
700+
url: portraitSource.url,
701+
cacheKey: `1`,
702+
directory: expect.stringContaining(cacheDir),
703+
})
704+
expect(fixedResult?.placeholder).toMatchInlineSnapshot(`
705+
Object {
706+
"fallback": "data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='20'%20height='33'%20viewBox='0%200%2020%2033'%3e%3cpath%20d='M6%201C4%205%204%205%203%203L2%201C0%201%200%205%200%2014s0%209%203%208c4%200%204%200%204-2v-8H6c0-1%202-3%204-3s2%200%202-2l-1-4c0-3%200-3-3-3L6%201'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e",
707+
}
708+
`)
709+
})
710+
684711
it(`should render avif, webp other format in this order`, async () => {
685712
const constrainedResult = await gatsbyImageResolver(
686713
portraitSource,

packages/gatsby-plugin-utils/src/polyfill-remote-file/graphql/get-remote-file-enums.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function getRemoteFileEnums(
4949
values: {
5050
DOMINANT_COLOR: { value: `dominantColor` },
5151
BLURRED: { value: `blurred` },
52+
TRACED_SVG: { value: `tracedSVG` },
5253
NONE: { value: `none` },
5354
},
5455
})

0 commit comments

Comments
 (0)