From 44450eb1b31e8eda42438266faea01728d02ea74 Mon Sep 17 00:00:00 2001 From: tmihalski Date: Mon, 15 Jun 2015 17:33:30 -0500 Subject: [PATCH] Added options:maxSize support, grunt log adding in Grunt log, adding in support for options: maxSize. Grunt command would look like: grunt.initConfig({ inline: { dist: { options: { maxSize: 65536 // default to 64KB }, src: 'src/index.html', dest: 'dist/index.html' } } }); --- tasks/inline.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tasks/inline.js b/tasks/inline.js index 556077d..68123b2 100644 --- a/tasks/inline.js +++ b/tasks/inline.js @@ -166,10 +166,18 @@ module.exports = function(grunt) { var inlineFilePath = path.resolve( path.dirname(filepath), src ).replace(/\?.*$/, ''); // 将参数去掉 if( grunt.file.exists(inlineFilePath) ){ - ret = matchedWord.replace(src, (new datauri(inlineFilePath)).content); + + if (getFileSize(inlineFilePath) <= options.maxSize){ + ret = matchedWord.replace(src, (new datauri(inlineFilePath)).content); + grunt.log.writeln('Inlining Image: '+ inlineFilePath.cyan + ' -> file size:' + getFileSize(inlineFilePath)); + }else{ + grunt.log.error("Omiting inlining of "+ inlineFilePath.red +" -> Image larger than "+ options.maxSize +"."); + } + }else{ grunt.log.error("Couldn't find " + inlineFilePath + '!'); } + } grunt.log.debug('ret = : ' + ret +'\n'); @@ -244,4 +252,17 @@ module.exports = function(grunt) { return fileContent; } + + function getFileSize(fullPath) { + + if (!fs.existsSync(fullPath)) { + return false; + } + + var stats = fs.statSync(fullPath); + if (!stats.isFile()) { + return false; + } + return stats.size; + } };