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: sharp as optional image processor #6

Merged
merged 3 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"dependencies": {
"gm": "^1.23.1",
"loader-utils": "^1.1.0"
},
"peerDependencies": {
"sharp": "^0.20.7"
}
}
49 changes: 36 additions & 13 deletions progressive-loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const loaderUtils = require('loader-utils')
const gm = require('gm').subClass({ imageMagick: true })

module.exports = function loader (contentBuffer) {
module.exports = function loader(contentBuffer) {
this.cacheable && this.cacheable()
const callback = this.async()

Expand All @@ -15,7 +14,7 @@ module.exports = function loader (contentBuffer) {
/** @see https://github.com/zouhir/lqip-loader */
const contentIsUrlExport = /^module.exports = "data:(.*)base64,(.*)/.test(
content
);
)
const contentIsFileExport = /^module.exports = (.*)/.test(content)
let source = ''

Expand All @@ -29,25 +28,49 @@ module.exports = function loader (contentBuffer) {
source = content.match(/^module.exports = (.*);/)[1]
}

gm(path).size(function (err, size) {
if (err) {
console.error(err)
return
}
if (config.sharp) {
const sharpImg = require('sharp')(path)

sharpImg
.webp({ quality: 10 })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change to specific format, or just use the same format?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webp is only support by chrome, so it should definitely default to something else. You should run some examples through to see what ends up smaller, I don't recall the rest of the results from my original tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it. jpeg is smaller than tiff or png, also I have implemented it in https://apek.or.id/about

.resize(config.size || 9)
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
const result = {
lazySrc: 'data:image/webp;base64,' + data.toString('base64'),
aspect: info.width / info.height,
}
callback(
null,
`module.exports = {src:${source},` + JSON.stringify(result).slice(1)
)
})
.catch(console.error)
} else {
const gm = require('gm').subClass({ imageMagick: true })

this.resize(config.size || 9)
.toBuffer('gif', (err, buffer) => {
gm(path).size(function(err, size) {
if (err) {
console.error(err)
return
}

this.resize(config.size || 9).toBuffer('gif', (err, buffer) => {
if (err) {
console.error(err)
return
}
const result = {
lazySrc: 'data:image/gif;base64,' + buffer.toString('base64'),
aspect: size.width / size.height
aspect: size.width / size.height,
}
callback(null, `module.exports = {src:${source},` + JSON.stringify(result).slice(1))
callback(
null,
`module.exports = {src:${source},` + JSON.stringify(result).slice(1)
)
})
})
})
}
}

module.exports.raw = true
Loading