From b53172261f951025ce045fa2176fe229faf45f5c Mon Sep 17 00:00:00 2001 From: hahnzhu Date: Mon, 9 Sep 2013 13:01:03 +0800 Subject: [PATCH] update CssCompiler.js --- src/app/scripts/compilers/CssCompiler.js | 52 ++++++------------------ 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/src/app/scripts/compilers/CssCompiler.js b/src/app/scripts/compilers/CssCompiler.js index 69bd84b7..13b32003 100644 --- a/src/app/scripts/compilers/CssCompiler.js +++ b/src/app/scripts/compilers/CssCompiler.js @@ -25,7 +25,7 @@ module.exports = CssCompiler; * @param {Object} emitter compile event emitter */ CssCompiler.prototype.compile = function(file, emitter) { - // global.debug(file); + global.debug(file); var cleanCSS = require('clean-css'); var rootPath = file.src.substring(0, file.src.indexOf(file.name)); var source = fs.readFileSync(file.src, 'utf-8'); @@ -39,11 +39,8 @@ CssCompiler.prototype.compile = function(file, emitter) { minimized = cleanCSS.process(source, { removeEmpty: true, keepBreaks: iskeepbreaks, processImport: false }); } - // convert background image to base64 - var convertion = convertImageUrl(minimized, rootPath); - - // append timestamp to external assets - var result = file.settings.appendTimestamp ? appendTimestamp(convertion) : convertion; + // convert background image to base64 & append timestamp + var result = convertImageUrl(minimized, rootPath, file.settings.appendTimestamp); fs.outputFile(file.output, result, function(err) { if (err) { @@ -55,27 +52,29 @@ CssCompiler.prototype.compile = function(file, emitter) { emitter.emit('always'); }; + /** * convert external image file to data URIs - * @param {String} css css code - * @param {String} rootPath the css file path - * @return {String} the converted css code + * @param {String} css css code + * @param {String} rootPath the css file path + * @param {Boolean} timestamp whether append timestamp + * @return {String} the converted css code */ -function convertImageUrl (css, rootPath) { +function convertImageUrl (css, rootPath, timestamp) { css = css.replace(/background.+?url.?\(.+?\)/gi, function (matchStr) { - // console.log(matchStr) var str = matchStr, originalUrl = str.match(/url.?\((.+)\)/)[0]; // get original url str = str.replace(/\'|\"/g, '').match(/url.?\((.+)\)/)[1].trim(); - // console.log(str); - var url = str.split('?')[0], param = str.split('?')[1]; + if (param !== 'base64' && timestamp === true) { + return matchStr.replace(originalUrl, 'url('+ url + '?' + createTimestamp() +')'); + } // not convert of absolute url - if (param !== 'base64' || url.indexOf('/') === 0 || url.indexOf('http') === 0) { + else if (param !== 'base64' || url.indexOf('/') === 0 || url.indexOf('http') === 0) { return matchStr; } var dataUrl = img2base64(url, rootPath); @@ -83,7 +82,6 @@ function convertImageUrl (css, rootPath) { // replace original url with dataurl return matchStr.replace(originalUrl, 'url('+ dataUrl +')'); }); - return css; } @@ -107,30 +105,6 @@ function img2base64(url, rootPath){ } } - -/** - * append timestamp to external assets - * @param {string} source code - */ -function appendTimestamp(css) { - css = css.replace(/background.+?url.?\(.+?\)/gi, function (matchStr) { - - var str = matchStr, - originalUrl = str.match(/url.?\((.+)\)/)[0]; - - str = str.replace(/\'|\"/g, '').match(/url.?\((.+)\)/)[1].trim(); - var url = str.split('?')[0]; - - if (str.indexOf('data:image/') === 0) { - return matchStr; - } - // append timestamp - return matchStr.replace(originalUrl, 'url('+ url + '?' + createTimestamp() +')'); - }); - return css; -} - - /** * create timestamp */