Skip to content

Commit

Permalink
Merge pull request #226 from XhmikosR/v2.x
Browse files Browse the repository at this point in the history
[2.x] ES6 tweaks and switch to using xo directly
  • Loading branch information
bezoerb authored Nov 11, 2019
2 parents b943218 + 6040336 commit d4f06c2
Show file tree
Hide file tree
Showing 5 changed files with 1,488 additions and 37 deletions.
31 changes: 15 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';
const postcss = require('postcss');
const Bluebird = require('bluebird');
const isString = require('lodash.isstring');
const defaults = require('lodash.defaults');
const debug = require('debug')('image-inliner');
const defaults = require('lodash.defaults');
const escape = require('lodash.escaperegexp');
const reduce = require('lodash.reduce');
const filesize = require('filesize');
const getResource = require('asset-resolver').getResource;
const getDataUri = require('./lib/image').getDataUri;
const {getResource} = require('asset-resolver');
const {getDataUri} = require('./lib/image');

module.exports = postcss.plugin('postcss-image-inliner', opts => {
opts = defaults(opts || {}, {
Expand All @@ -18,7 +17,7 @@ module.exports = postcss.plugin('postcss-image-inliner', opts => {
strict: false
});

if (isString(opts.assetPaths)) {
if (!Array.isArray(opts.assetPaths)) {
opts.assetPaths = [opts.assetPaths];
}

Expand All @@ -28,7 +27,7 @@ module.exports = postcss.plugin('postcss-image-inliner', opts => {
const encoding = resource.mime === 'image/svg+xml' ? 'utf-8' : 'binary';
const size = Buffer.byteLength(resource.contents, encoding);
if (opts.maxFileSize && opts.maxFileSize < size) {
const msg = 'Too big. ' + filesize(size) + ' exceeds the allowed ' + filesize(opts.maxFileSize);
const msg = `Too big. ${filesize(size)} exceeds the allowed ${filesize(opts.maxFileSize)}`;
debug(msg);
return Bluebird.reject(new Error(msg));
}
Expand All @@ -40,14 +39,14 @@ module.exports = postcss.plugin('postcss-image-inliner', opts => {
return getResource(filepath, {
base: opts.assetPaths,
filter: assertSize
}).catch(err => {
debug(err.message, filepath, 'could not be resolved');
}).catch(error => {
debug(error.message, filepath, 'could not be resolved');
});
}

function loop(cb) {
const matcher = /url\(\s*(?:['"]*)(?!['"]*data:)(.*?)(?:['"]*)\s*\)/gm;
return function (decl) {
return decl => {
let match;
while ((match = matcher.exec(decl.value)) !== null) {
cb(decl, match[match.length - 1]);
Expand All @@ -65,7 +64,7 @@ module.exports = postcss.plugin('postcss-image-inliner', opts => {
}, {});
}

return function (css) {
return css => {
const replacements = {};
const filter = /^(background(?:-image)?)|(content)|(cursor)/;
css.walkDecls(filter, loop((decl, url) => {
Expand All @@ -80,20 +79,20 @@ module.exports = postcss.plugin('postcss-image-inliner', opts => {
.then(data => {
css.walkDecls(filter, loop((decl, url) => {
if (data[url]) {
const regexp = new RegExp('[\'"]?' + escape(url) + '[\'"]?');
decl.value = decl.value.replace(regexp, '\'' + data[url] + '\'');
const regexp = new RegExp(`['"]?${escape(url)}['"]?`);
decl.value = decl.value.replace(regexp, `'${data[url]}'`);
debug(url, 'successfully replaced with ', data[url]);
} else {
debug(url, 'failed');
if (opts.strict) {
throw new Error('No file found for ' + url);
throw new Error(`No file found for ${url}`);
}
}
}));
}).catch(err => {
debug(err);
}).catch(error => {
debug(error);
return new Bluebird((resolve, reject) => {
reject(err);
reject(error);
});
});
};
Expand Down
12 changes: 6 additions & 6 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ function encodeSvg(content) {
}

function computeDataUri(opts) {
return function (file, key) {
return (file, key) => {
// If the url is SVG, let's compress and use the XML directly
if (file.mime === 'image/svg+xml' && !opts.b64Svg) {
return svgo.optimize(file.contents.toString('utf-8')).then(result => {
debug('optimising svg');
return {
data: 'data:image/svg+xml;charset=US-ASCII,' + encodeSvg(result.data),
data: `data:image/svg+xml;charset=US-ASCII,${encodeSvg(result.data)}`,
key
};
}).catch(err => {
debug('errored', err.message);
throw err;
}).catch(error => {
debug('errored', error.message);
throw error;
});
}

// Otherwise we base64 encode the image
return {
data: 'data:' + file.mime + ';base64,' + Buffer.from(file.contents, 'binary').toString('base64'),
data: `data:${file.mime};base64,${Buffer.from(file.contents, 'binary').toString('base64')}`,
key
};
};
Expand Down
Loading

0 comments on commit d4f06c2

Please sign in to comment.