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

Image untouched transformer plugin #1513

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 30 additions & 0 deletions packages/gatsby-remark-images/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"sourceMaps": true,
// "env": {
// "development": {
// "sourceMaps": "inline"
// },
// "production": {
// "sourceMaps": false
// }
// },
"presets": [
[
"env",
{
"targets": {
"node": 4.0,
"uglify": true,
}
}
],
"react",
"flow"
],
"plugins": [
"transform-runtime",
"transform-object-rest-spread",
"transform-async-to-generator",
"transform-flow-strip-types"
]
}
3 changes: 3 additions & 0 deletions packages/gatsby-transformer-untouched-images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/gatsby-node.js
/extend-node-type.js
/on-node-create.js
34 changes: 34 additions & 0 deletions packages/gatsby-transformer-untouched-images/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
30 changes: 30 additions & 0 deletions packages/gatsby-transformer-untouched-images/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# gatsby-transformer-untouched-image

Creates `ImageUntouched` nodes from images and leaves them alone. This is a much faster alternative to Sharp processing every image.

## Install

`npm install --save gatsby-transformer-untouched-images`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
`gatsby-transformer-untouched-images`,
]
```

## Parsing algorithm

It recongnizes files with the following extensions as images.

* jpeg
* jpg
* png
* webp
* tif
* tiff
* svg

Each image file is parsed into a node of type `ImageUntouched`.
1 change: 1 addition & 0 deletions packages/gatsby-transformer-untouched-images/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
24 changes: 24 additions & 0 deletions packages/gatsby-transformer-untouched-images/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "gatsby-transformer-untouched-images",
"version": "1.0.1",
"description": "Gatsby transformer plugin for images without Using Sharp.",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
},
"keywords": [
"gatsby",
"gatsby-plugin",
"sharp",
"image"
],
"author": "Chiedo <chiedo@chiedo.com>",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0",
"image-size": "^0.6.0"
},
"devDependencies": {
"babel-cli": "^6.24.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Process image nodes correctly correctly creates an ImageSharp node from a file image node 1`] = `
Array [
Array [
Object {
"children": Array [],
"id": "whatever >> ImageSharp",
"internal": Object {
"contentDigest": "whatever",
"type": "ImageSharp",
},
"parent": "whatever",
},
],
]
`;

exports[`Process image nodes correctly correctly creates an ImageSharp node from a file image node 2`] = `
Array [
Array [
Object {
"child": Object {
"children": Array [],
"id": "whatever >> ImageSharp",
"internal": Object {
"contentDigest": "whatever",
"type": "ImageSharp",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"extension": "png",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
},
},
},
],
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Promise = require(`bluebird`)

const { onCreateNode } = require(`../gatsby-node`)

describe(`Process image nodes correctly`, () => {
it(`correctly creates an ImageSharp node from a file image node`, async () => {
const node = {
extension: `png`,
id: `whatever`,
children: [],
internal: {
contentDigest: `whatever`,
},
}
const createNode = jest.fn()
const createParentChildLink = jest.fn()
const boundActionCreators = { createNode, createParentChildLink }

await onCreateNode({
node,
boundActionCreators,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const Promise = require(`bluebird`)
const path = require('path');


const {
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLBoolean,
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLEnumType,
} = require(`graphql`)

const {promisify} = require('util');
const ncp = require('ncp');
const ncpAsync = promisify(ncp);
const sizeOf = require('image-size');

module.exports = ({ type, pathPrefix, getNodeAndSavePathDependency }) => {
if (type.name !== `ImageUntouched`) {
return {}
}

return {
original: {
type: new GraphQLObjectType({
name: `ImageUntouchedOriginal`,
fields: {
width: { type: GraphQLFloat },
height: { type: GraphQLFloat },
src: { type: GraphQLString },
},
}),
args: {
},
async resolve(image, fieldArgs, context) {
const details = getNodeAndSavePathDependency(image.parent, context.path);
const dimensions = sizeOf(details.absolutePath);
const imageName = `${image.internal.contentDigest}${details.ext}`;
const publicPath = path.join(
process.cwd(),
`public`,
`static/${imageName}`
);

await ncpAsync(details.absolutePath, publicPath);

return {
width: dimensions.width,
height: dimensions.height,
src: '/static/' + imageName,
}
},
},
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.onCreateNode = require(`./on-node-create`)
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
26 changes: 26 additions & 0 deletions packages/gatsby-transformer-untouched-images/src/on-node-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const _ = require(`lodash`)

module.exports = async function onCreateNode({ node, boundActionCreators }) {
const { createNode, createParentChildLink } = boundActionCreators

const extensions = [`jpeg`, `jpg`, `png`, `webp`, `tif`, `tiff`, `svg`]
if (!_.includes(extensions, node.extension)) {
return
}

const imageNode = {
id: `${node.id} >> ImageUntouched`,
children: [],
parent: node.id,
internal: {
contentDigest: `${node.internal.contentDigest}`,
type: `ImageUntouched`,
},
}

createNode(imageNode)
createParentChildLink({ parent: node, child: imageNode })

return
}