responsive-loader
Advanced tools
Comparing version 0.5.3 to 0.6.0
# Change Log | ||
## 0.6.0 | ||
- Add linting ([#7](https://github.com/herrstucki/responsive-loader/pull/7)) | ||
- Breaking (maybe): Require node >= v4 | ||
## 0.5.3 | ||
@@ -4,0 +9,0 @@ |
66
index.js
@@ -1,8 +0,7 @@ | ||
/* eslint-disable */ | ||
var path = require('path'); | ||
var loaderUtils = require('loader-utils'); | ||
var jimp = require('jimp'); | ||
var queue = require('d3-queue').queue; | ||
const path = require('path'); | ||
const loaderUtils = require('loader-utils'); | ||
const jimp = require('jimp'); | ||
const queue = require('d3-queue').queue; | ||
var MIMES = { | ||
const MIMES = { | ||
'jpg': 'image/jpeg', | ||
@@ -13,17 +12,17 @@ 'jpeg': 'image/jpeg', | ||
module.exports = function(content) { | ||
module.exports = function loader(content) { | ||
this.cacheable && this.cacheable(); | ||
var loaderCallback = this.async(); | ||
var query = loaderUtils.parseQuery(this.query); | ||
var options = this.options.responsiveLoader || {}; | ||
var sizes = query.sizes || query.size || [Number.MAX_SAFE_INTEGER]; | ||
var name = query.name || '[hash]-[width].'; | ||
const loaderCallback = this.async(); | ||
const query = loaderUtils.parseQuery(this.query); | ||
const options = this.options.responsiveLoader || {}; | ||
const sizes = query.sizes || query.size || [Number.MAX_SAFE_INTEGER]; | ||
const name = query.name || '[hash]-[width].'; | ||
// JPEG compression | ||
var quality = parseInt(query.quality, 10) || 95; | ||
const quality = parseInt(query.quality, 10) || 95; | ||
// Useful when converting from PNG to JPG | ||
var background = parseInt(query.background, 16) || 0xFFFFFFFF; | ||
const background = parseInt(query.background, 16) || 0xFFFFFFFF; | ||
// Specify ext to convert to another format | ||
var ext = query.ext || path.extname(this.resourcePath).replace(/\./, ''); | ||
var mime = MIMES[ext]; | ||
var loaderContext = this; | ||
const ext = query.ext || path.extname(this.resourcePath).replace(/\./, ''); | ||
const mime = MIMES[ext]; | ||
const loaderContext = this; | ||
@@ -46,3 +45,3 @@ if (!sizes) { | ||
jimp.read(loaderContext.resourcePath, function(err, img) { | ||
return jimp.read(loaderContext.resourcePath, (err, img) => { | ||
if (err) { | ||
@@ -53,3 +52,3 @@ return loaderCallback(err); | ||
function resizeImage(width, queueCallback) { | ||
img | ||
img | ||
.clone() | ||
@@ -59,12 +58,12 @@ .resize(width, jimp.AUTO) | ||
.background(background) | ||
.getBuffer(mime, function(err, buf) { | ||
.getBuffer(mime, function resizeCallback(queueErr, buf) { | ||
if (err) { | ||
return queueCallback(err); | ||
return queueCallback(queueErr); | ||
} | ||
var fileName = loaderUtils.interpolateName(loaderContext, name + ext, {content: buf}).replace(/\[width\]/ig, width); | ||
const fileName = loaderUtils.interpolateName(loaderContext, name + ext, {content: buf}).replace(/\[width\]/ig, width); | ||
loaderContext.emitFile(fileName, buf); | ||
queueCallback(null, { | ||
return queueCallback(null, { | ||
src: '__webpack_public_path__ + ' + JSON.stringify(fileName + ' ' + width + 'w'), | ||
@@ -77,7 +76,7 @@ path: '__webpack_public_path__ + ' + JSON.stringify(fileName), | ||
var q = queue(); | ||
var widthsToGenerate = new Set(); | ||
const q = queue(); | ||
const widthsToGenerate = new Set(); | ||
(Array.isArray(sizes) ? sizes : [sizes]).forEach(function(size) { | ||
var width = Math.min(img.bitmap.width, parseInt(size, 10)); | ||
(Array.isArray(sizes) ? sizes : [sizes]).forEach((size) => { | ||
const width = Math.min(img.bitmap.width, parseInt(size, 10)); | ||
@@ -89,15 +88,10 @@ // Only resize images if they aren't an exact copy of one already being resized... | ||
} | ||
}); | ||
q.awaitAll(function(err, files) { | ||
var srcset = files.map(function(f) { | ||
return f.src; | ||
}).join('+","+'); | ||
return q.awaitAll((queueErr, files) => { | ||
const srcset = files.map(f => f.src).join('+","+'); | ||
var images = files.map(function(f) { | ||
return '{path:' + f.path + ',width:' + f.width + '}'; | ||
}).join(','); | ||
const images = files.map(f => '{path:' + f.path + ',width:' + f.width + '}').join(','); | ||
var firstImagePath = files[0].path; | ||
const firstImagePath = files[0].path; | ||
@@ -104,0 +98,0 @@ loaderCallback(null, 'module.exports = {srcSet:' + srcset + ',images:[' + images + '],src:' + firstImagePath + ',toString:function(){return ' + firstImagePath + '}};'); |
{ | ||
"name": "responsive-loader", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "A webpack loader for responsive images", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"build-test": "webpack --config=./test/webpack.config.js", | ||
"test": "npm run build-test && tape './test/build/test.js' | tap-spec" | ||
"lint": "eslint --ignore-path=.gitignore .", | ||
"test": "npm run lint && npm run build-test && tape './test/build/test.js' | tap-spec" | ||
}, | ||
@@ -34,2 +38,5 @@ "files": [ | ||
"devDependencies": { | ||
"eslint": "^3.2.2", | ||
"eslint-config-interactivethings": "^3.0.0", | ||
"eslint-plugin-react": "^5.2.2", | ||
"jimp": "^0.2.21", | ||
@@ -36,0 +43,0 @@ "tap-spec": "^4.1.1", |
10706
7
79