Skip to content

Commit

Permalink
breaking: convert source-filesystem to core-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored and axe312ger committed Sep 26, 2021
1 parent 51e08a9 commit b0fa455
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 96 deletions.
50 changes: 12 additions & 38 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const crypto = require(`crypto`)
const path = require(`path`)
const fs = require(`fs-extra`)

const Debug = require('debug')
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const { fetchRemoteFile } = require(`gatsby-core-utils`)
const svgToMiniDataURI = require('mini-svg-data-uri')
const { default: PQueue } = require('p-queue')
const SVGO = require('svgo')

const debug = new Debug('gatsby-transformer-inline-svg')
const queue = new PQueue({
concurrency: 5
})
Expand Down Expand Up @@ -80,23 +78,11 @@ exports.createSchemaCustomization = ({ actions }) => {
`)
}

async function parseSVG({
source,
uri,
store,
cache,
createNode,
createNodeId
}) {
async function parseSVG({ source, uri, store, cache, reporter }) {
// Get remote file
debug('Downloading ' + source.contentful_id + ': ' + uri)
const { absolutePath, relativePath } = await createRemoteFileNode({
const absolutePath = await fetchRemoteFile({
url: uri,
parentNodeId: source.id,
store,
cache,
createNode,
createNodeId
cache
})

// Read local file
Expand All @@ -110,9 +96,8 @@ async function parseSVG({

// Optimize
if (svg.indexOf('base64') !== -1) {
console.log(
'SVG contains pixel data. Pixel data was removed to avoid file size bloat.',
source.contentful_id + ': ' + absolutePath
reporter.info(
`SVG contains pixel data. Pixel data was removed to avoid file size bloat.\n${source.contentful_id}: ${absolutePath}`
)
}
const { data: optimizedSVG } = await svgo.optimize(svg, {
Expand All @@ -121,25 +106,18 @@ async function parseSVG({

// Create mini data URI
const dataURI = svgToMiniDataURI(optimizedSVG)
const directory = store.getState().program.directory

return {
content: optimizedSVG,
originalContent: svg,
dataURI,
absolutePath,
relativePath
relativePath: path.relative(directory, absolutePath)
}
}

exports.createResolvers = ({
actions,
cache,
createNodeId,
createResolvers,
store,
reporter
}) => {
const { createNode } = actions
exports.createResolvers = ({ cache, createResolvers, store, reporter }) => {
createResolvers({
ContentfulAsset: {
svg: {
Expand All @@ -154,8 +132,6 @@ exports.createResolvers = ({
file: { url, contentType }
} = source

debug({ source, url, contentType })

// Ensure to process only svgs and files with an url
if (contentType !== 'image/svg+xml' || !url) {
return null
Expand Down Expand Up @@ -187,17 +163,15 @@ exports.createResolvers = ({
uri,
store,
cache,
createNode,
createNodeId
reporter
})

sessionCache[cacheId] = result
await cache.set(cacheId, result)

debug('Processed and cached ' + url)
return result
} catch (err) {
console.error(err)
reporter.panic(err)
return null
}
})
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
},
"dependencies": {
"debug": "^4.1.1",
"fs-extra": "^8.1.0",
"gatsby-source-filesystem": "^2.1.48",
"fs-extra": "^10.0.0",
"gatsby-core-utils": "^3.0.0-zz-next.1",
"mini-svg-data-uri": "^1.1.3",
"p-queue": "^6.2.1",
"svgo": "^1.3.2"
Expand All @@ -69,7 +69,7 @@
"prettier": "^1.19.1"
},
"peerDependencies": {
"gatsby-source-contentful": "^2.1.62",
"gatsby": "^2.19.11 || ^3.0.0-next.0"
"gatsby": "^2.19.11 || ^3.0.0-next.0 || ^4.0.0-next.0",
"gatsby-source-contentful": ">=2.1.62"
}
}
Loading

0 comments on commit b0fa455

Please sign in to comment.