/* * grunt-inline * https://github.com/chyingp/grunt-inline * * Copyright (c) 2015 Auguest G. casper & IMWEB TEAM */ 'use strict'; module.exports = function(grunt) { var path = require('path'); var datauri = require('datauri'); var UglifyJS = require("uglify-js"); var CleanCSS = require('clean-css'); grunt.registerMultiTask('inline', "Replaces , '; }else{ grunt.log.error("Couldn't find " + inlineFilePath + '!'); } } grunt.log.debug('ret = : ' + ret +'\n'); return ret; }).replace(//g, function(matchedWord, src){ var ret = matchedWord; if(!isRemotePath(src) && src.indexOf(options.tag)!=-1){ var inlineFilePath = path.resolve( path.dirname(filepath), src ).replace(/\?.*$/, ''); // 将参数去掉 if( grunt.file.exists(inlineFilePath) ){ var styleSheetContent = grunt.file.read( inlineFilePath ); ret = ''; }else{ grunt.log.error("Couldn't find " + inlineFilePath + '!'); } } grunt.log.debug('ret = : ' + ret +'\n'); return ret; }).replace(//g, function(matchedWord, src){ var ret = matchedWord; if(!grunt.file.isPathAbsolute(src) && src.indexOf(options.tag)!=-1){ var inlineFilePath = path.resolve( path.dirname(filepath), src ).replace(/\?.*$/, ''); // 将参数去掉 if( grunt.file.exists(inlineFilePath) ){ ret = matchedWord.replace(src, (new datauri(inlineFilePath)).content); }else{ grunt.log.error("Couldn't find " + inlineFilePath + '!'); } } grunt.log.debug('ret = : ' + ret +'\n'); return ret; }); return fileContent; } function css(filepath, fileContent, relativeTo, options) { if(relativeTo){ filepath = filepath.replace(/[^\/]+\//g, relativeTo); } fileContent = fileContent.replace(/url\(["']*([^)'"]+)["']*\)/g, function(matchedWord, imgUrl){ var newUrl = imgUrl; var flag = imgUrl.indexOf(options.tag)!=-1; // urls like "img/bg.png?__inline" will be transformed to base64 if(isBase64Path(imgUrl) || isRemotePath(imgUrl)){ return matchedWord; } grunt.log.debug( 'imgUrl: '+imgUrl); grunt.log.debug( 'filepath: '+filepath); var absoluteImgurl = path.resolve( path.dirname(filepath),imgUrl ); grunt.log.debug( 'absoluteImgurl: '+absoluteImgurl); newUrl = path.relative( path.dirname(filepath), absoluteImgurl ); grunt.log.debug( 'newUrl: '+newUrl); absoluteImgurl = absoluteImgurl.replace(/\?.*$/, ''); if(flag && grunt.file.exists(absoluteImgurl)){ newUrl = datauri(absoluteImgurl); }else{ newUrl = newUrl.replace(/\\/g, '/'); } return matchedWord.replace(imgUrl, newUrl); }); fileContent = options.cssmin ? CleanCSS.process(fileContent) : fileContent; return fileContent; } function cssInlineToHtml(htmlFilepath, filepath, fileContent, relativeTo, options) { if(relativeTo){ filepath = filepath.replace(/[^\/]+\//g, relativeTo); } fileContent = fileContent.replace(/url\(["']*([^)'"]+)["']*\)/g, function(matchedWord, imgUrl){ var newUrl = imgUrl; var flag = !!imgUrl.match(/\?__inline/); // urls like "img/bg.png?__inline" will be transformed to base64 grunt.log.debug('flag:'+flag); if(isBase64Path(imgUrl) || isRemotePath(imgUrl)){ return matchedWord; } grunt.log.debug( 'imgUrl: '+imgUrl); grunt.log.debug( 'filepath: '+filepath); var absoluteImgurl = path.resolve( path.dirname(filepath),imgUrl ); // img url relative to project root grunt.log.debug( 'absoluteImgurl: '+absoluteImgurl); newUrl = path.relative( path.dirname(htmlFilepath), absoluteImgurl ); // img url relative to the html file grunt.log.debug([htmlFilepath, filepath, absoluteImgurl, imgUrl]); grunt.log.debug( 'newUrl: '+newUrl); absoluteImgurl = absoluteImgurl.replace(/\?.*$/, ''); if(flag && grunt.file.exists(absoluteImgurl)){ newUrl = datauri(absoluteImgurl); }else{ newUrl = newUrl.replace(/\\/g, '/'); } return matchedWord.replace(imgUrl, newUrl); }); fileContent = options.cssmin ? CleanCSS.process(fileContent) : fileContent; return fileContent; } };