Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Warn and detect if duplicated image path found

If an image included twice, the output file could be much bigger, so
better to warn about it!
  • Loading branch information...
commit f269aebe78666a1b08ecc458bb727f87ba15ec5c 1 parent bd0263f
@Ajnasz authored
Showing with 21 additions and 7 deletions.
  1. +21 −7 tasks/css_img_2_data_uri.js
View
28 tasks/css_img_2_data_uri.js
@@ -84,7 +84,7 @@ module.exports = function (grunt) {
if (imagePath) {
getMD5ForImage(imagePath, function (base64) {
- cb(replaceLine(line, base64, mime.lookup(imagePath)));
+ cb(replaceLine(line, base64, mime.lookup(imagePath)), imagePath);
});
} else {
cb(line);
@@ -94,21 +94,29 @@ module.exports = function (grunt) {
}
}
- function doStuff(data, filePath, cb) {
+ function convertPathsToDataUri(data, filePath, cb) {
var lines = data.toString('utf-8').split('\n'),
outputLines = [],
- processedLines = 0;
+ processedLines = 0,
+ imagePaths = {},
+ duplicates = [];
function finishLineProcess() {
processedLines += 1;
if (processedLines === lines.length) {
- cb(outputLines.join('\n'));
+ cb(outputLines.join('\n'), duplicates);
}
}
lines.forEach(function (line, index) {
- processLine(line, filePath, function (line) {
+ processLine(line, filePath, function (line, imagePath) {
+ if (imagePath) {
+ if (imagePaths[imagePath] === true) {
+ duplicates.push(index);
+ }
+ imagePaths[imagePath] = true;
+ }
outputLines[index] = line;
finishLineProcess();
});
@@ -128,7 +136,7 @@ module.exports = function (grunt) {
if (err) {
throw err;
}
- doStuff(data, filePath, cb);
+ convertPathsToDataUri(data, filePath, cb);
});
});
}
@@ -145,7 +153,13 @@ module.exports = function (grunt) {
doneCounter = 0;
files.forEach(function (f) {
- build(f.src, function (css) {
+ build(f.src, function (css, duplicates) {
+
+ if (duplicates.length) {
+ grunt.log.error('possible duplicated images in the following lines: '
+ + duplicates.join(', '));
+ }
+
grunt.file.write(f.dest, css);
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
Please sign in to comment.
Something went wrong with that request. Please try again.