Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-utils): enable tracedsvg #35328

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ describe(`remote-file`, () => {
expect($el.prop("tagName")).to.be.equal("IMG")
expect($el.prop("src")).to.contain("data:image/jpg;base64")
})
cy.get(".constrained_traced [data-placeholder-image]")
.first()
.should($el => {
expect($el.prop("tagName")).to.be.equal("IMG")
expect($el.prop("src")).to.contain("data:image/svg+xml,%3csvg")
})
cy.get(".full [data-placeholder-image]")
.first()
.should($el => {
Expand Down
10 changes: 10 additions & 0 deletions e2e-tests/development-runtime/src/pages/remote-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const RemoteFile = ({ data }) => {
image={node.constrained}
alt=""
/>
<GatsbyImage
className="constrained_traced"
image={node.constrained_traced}
alt=""
/>
<GatsbyImage className="full" image={node.full} alt="" />
</div>
</div>
Expand Down Expand Up @@ -64,6 +69,11 @@ export const pageQuery = graphql`
width: 300
placeholder: BLURRED
)
constrained_traced: gatsbyImage(
layout: CONSTRAINED
width: 300
placeholder: TRACED_SVG
)
full: gatsbyImage(layout: FULL_WIDTH, width: 500, placeholder: NONE)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ describe(`remote-file`, () => {
expect($el.prop("tagName")).to.be.equal("IMG")
expect($el.prop("src")).to.contain("data:image/jpg;base64")
})
cy.get(".constrained_traced [data-placeholder-image]")
.first()
.should($el => {
expect($el.prop("tagName")).to.be.equal("IMG")
expect($el.prop("src")).to.contain("data:image/svg+xml,%3csvg")
})
cy.get(".full [data-placeholder-image]")
.first()
.should($el => {
Expand Down
10 changes: 10 additions & 0 deletions e2e-tests/production-runtime/src/pages/remote-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const RemoteFile = ({ data }) => {
image={node.constrained}
alt=""
/>
<GatsbyImage
className="constrained_traced"
image={node.constrained_traced}
alt=""
/>
<GatsbyImage className="full" image={node.full} alt="" />
</div>
</div>
Expand Down Expand Up @@ -61,6 +66,11 @@ export const pageQuery = graphql`
width: 300
placeholder: BLURRED
)
constrained_traced: gatsbyImage(
layout: CONSTRAINED
width: 300
placeholder: TRACED_SVG
)
full: gatsbyImage(layout: FULL_WIDTH, width: 500, placeholder: NONE)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@babel/runtime": "^7.15.4",
"@gatsbyjs/potrace": "^2.2.0",
"async": "^3.2.3",
"bluebird": "^3.7.2",
"debug": "^4.3.4",
Expand All @@ -19,7 +20,6 @@
"got": "^11.8.3",
"lodash": "^4.17.21",
"mini-svg-data-uri": "^1.4.4",
"potrace": "^2.1.8",
"probe-image-size": "^7.2.3",
"progress": "^2.0.3",
"semver": "^7.3.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/trace-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ exports.notMemoizedtraceSVG = async ({ file, args, fileArgs, reporter }) => {
})

const svgToMiniDataURI = require(`mini-svg-data-uri`)
const potrace = require(`potrace`)
const potrace = require(`@gatsbyjs/potrace`)
const trace = promisify(potrace.trace)

const defaultArgs = {
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-plugin-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-utils#readme",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@gatsbyjs/potrace": "^2.2.0",
"fs-extra": "^10.0.0",
"gatsby-core-utils": "^3.13.0-next.0",
"gatsby-sharp": "^0.7.0-next.0",
"graphql-compose": "^9.0.7",
"import-from": "^4.0.0",
"joi": "^17.4.2",
"mime": "^3.0.0"
"mime": "^3.0.0",
"mini-svg-data-uri": "^1.4.4",
"svgo": "^2.8.0"
},
"devDependencies": {
"@babel/cli": "^7.15.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,33 @@ describe(`gatsbyImageData`, () => {
`)
})

it(`should generate tracedSVG placeholder`, async () => {
fetchRemoteFile.mockResolvedValueOnce(
path.join(__dirname, `__fixtures__`, `dog-portrait.jpg`)
)
const fixedResult = await gatsbyImageResolver(
portraitSource,
{
layout: `fixed`,
width: 300,
placeholder: PlaceholderType.TRACED_SVG,
},
actions
)

expect(fetchRemoteFile).toHaveBeenCalledTimes(1)
expect(fetchRemoteFile).toHaveBeenCalledWith({
url: portraitSource.url,
cacheKey: `1`,
directory: expect.stringContaining(cacheDir),
})
expect(fixedResult?.placeholder).toMatchInlineSnapshot(`
Object {
"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",
}
`)
})

it(`should render avif, webp other format in this order`, async () => {
const constrainedResult = await gatsbyImageResolver(
portraitSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function getRemoteFileEnums(
values: {
DOMINANT_COLOR: { value: `dominantColor` },
BLURRED: { value: `blurred` },
TRACED_SVG: { value: `tracedSVG` },
NONE: { value: `none` },
},
})
Expand Down
Loading