-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add gatsby-transformer-sharp support #5
Comments
Thanks for the idea! ❤️ We'll probably not focus on that for now, but as always: contributions are very welcome! |
hi, I'll probably need this in a project this or early next week, I'll make a pr with the changes... @DSchau do you know any comparable plugins that are already doing this in a good way to use as an example, the gatsby-source-filesystem docs about |
My coworker and I had this working in a fork a while ago. I'm not sure if or how outdated it might be today but it might be useful to use as a reference. The changes are in a PR here: substantial#2 It's been a while since I worked on that, but I think the relevant sections to look at are |
I'm interested in this too - using ChildImageSharp seems a pretty fundamental thing in a source plugin for Gatsby. I needed to add this manually for a site using I'll try and get this working for Ghost now - ideally this shouldn't be in the gatsby-node.js file and should be handled by the source plugin - but that's a bit beyond my knowledge. I'll post my efforts at getting this to work though. // get all imageNodes
exports.sourceNodes = ({
actions,
cache,
createNodeId,
getNodesByType,
store,
reporter,
}) => {
const { createNode } = actions
const imageDownloads = []
const submissions = getNodesByType("submissions")
submissions.forEach(node => {
node.works &&
node.works.forEach(({ mediaItems }) => {
mediaItems &&
mediaItems.forEach(mediaSource => {
if (
mediaSource.mime !== "image/jpeg" &&
mediaSource.mime !== "image/png" &&
mediaSource.mime !== "image/gif" &&
mediaSource.mime !== "image/svg"
) {
// return reporter.warn(
// `The mime type is not an image, its ${mediaSource.mime}`
// )
return false
}
let fileType = mediaSource.mime.split("/")[1]
if (fileType === "jpeg") {
fileType = "jpg"
}
const imageUrl = `https://example.com/media/${
mediaSource.filename.split(".")[0]
}-resize-1920-2560.${fileType}`
imageDownloads.push(
createRemoteFileNode({
url: imageUrl,
store,
cache,
createNode,
createNodeId,
reporter,
})
.then(result => {
if (!result) {
return reporter.warn(`Could not download ${imageUrl}`)
}
mediaSource.imageFile___NODE = result.id
})
.catch(err => {
reporter.warn(err)
}),
)
})
})
})
return Promise.all(imageDownloads)
} |
This works for the demo site and will get all the add this to gatsby-node.js suggestions on the best way to do this very welcome // use gatsby-source-filesystem to create the nodes
const { createRemoteFileNode } = require("gatsby-source-filesystem");
// get all imageNodes
exports.sourceNodes = ({
actions,
cache,
createNodeId,
getNodesByType,
store,
reporter,
}) => {
const { createNode } = actions;
const imageDownloads = [];
const GhostPost = getNodesByType("GhostPost");
GhostPost.forEach((node) => {
const imageUrl = node.feature_image;
imageDownloads.push(
createRemoteFileNode({
url: imageUrl,
store,
cache,
createNode,
createNodeId,
reporter,
})
.then((result) => {
if (!result) {
return reporter.warn(`Could not download ${imageUrl}`);
}
node.feature_image_sharp___NODE = result.id;
})
.catch((err) => {
reporter.warn(err);
})
);
});
return Promise.all(imageDownloads);
}; |
For those looking for a solution gatsby-plugin-remote-images might get you there. |
I'll try to look into adding this via a PR but would be nice to add gatsby-transformer-sharp so we can do responsive images and use gatsby-image for lazyload, etc.
See https://github.com/angeloashmore/gatsby-source-shopify
The text was updated successfully, but these errors were encountered: