Permalink
Browse files

feat(core): add `deleteOriginals` option

  • Loading branch information...
sparanoid committed Sep 1, 2015
1 parent 28b68c1 commit d2beef785997487f55f29b62a74cf7a4704f0b8d
Showing with 39 additions and 9 deletions.
  1. +39 −9 tasks/assets_inline.js
@@ -35,7 +35,9 @@ module.exports = function(grunt) {
inlineSvgBase64: false,
includeTag: "",
assetsUrlPrefix: "",
verbose: false
verbose: false,
deleteOriginals: false
});
options.cssTags = this.options().cssTags || {
@@ -63,6 +65,8 @@ module.exports = function(grunt) {
return input.replace(/"/g, "'").replace(/(?:\r\n|\r|\n)/g, "");
};
var filesToDelete = [];
this.files.forEach(function(filePair) {
// Check that the source file exists
if(filePair.src.length === 0) { return; }
@@ -93,6 +97,10 @@ module.exports = function(grunt) {
var filePath = (style.substr(0,1) === "/") ? path.resolve(options.cssDir, style.substr(1)) : path.join(path.dirname(filePair.src), style);
grunt.log.writeln((' css: ').cyan + filePath);
$(this).replaceWith(options.cssTags.start + grunt.file.read(filePath) + options.cssTags.end);
if (options.deleteOriginals) {
filesToDelete.push(filePath);
}
});
$('script').each(function () {
@@ -114,6 +122,10 @@ module.exports = function(grunt) {
//create and replace script with new scipt tag
$(this).replaceWith(options.jsTags.start + uglifyJS(grunt.file.read(filePath)) + options.jsTags.end);
if (options.deleteOriginals) {
filesToDelete.push(filePath);
}
});
if (options.inlineSvg) {
@@ -133,6 +145,10 @@ module.exports = function(grunt) {
$(this).attr('src', 'data:image/svg+xml;utf8,' + processSvg(grunt.file.read(filePath)));
}
}
if (options.deleteOriginals) {
filesToDelete.push(filePath);
}
});
}
@@ -148,6 +164,10 @@ module.exports = function(grunt) {
grunt.log.writeln((' image: ').cyan + filePath);
$(this).attr('src', 'data:image/' + src.substr(src.lastIndexOf('.')+1) + ';base64,' + new Buffer(grunt.file.read(filePath, { encoding: null })).toString('base64'));
if (options.deleteOriginals) {
filesToDelete.push(filePath);
}
});
}
@@ -158,16 +178,26 @@ module.exports = function(grunt) {
grunt.log.writeln(('Created: ').green + path.resolve(filePair.dest) + '\n');
});
// Delete the original files
if (options.deleteOriginals) {
filesToDelete.forEach(function(filename) {
if (grunt.file.exists(filename)) {
grunt.file.delete(filename);
grunt.log.writeln(('Removed: ').green + filename);
}
});
}
function getAttributes(el) {
var attributes = {};
for (var index in el.attribs) {
var attr = el.attribs[index];
if (options.verbose) {
grunt.log.writeln((" attr: ").blue + index + ":" + attr);
}
attributes[ index ] = attr;
var attributes = {};
for (var index in el.attribs) {
var attr = el.attribs[index];
if (options.verbose) {
grunt.log.writeln((' attr: ').blue + index + ":" + attr);
}
return attributes;
attributes[ index ] = attr;
}
return attributes;
}
});
};

0 comments on commit d2beef7

Please sign in to comment.