grunt-data-uri
Advanced tools
Comparing version 0.0.2 to 0.1.0
{ | ||
"name": "grunt-data-uri", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Convert to data-uri from image path.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -15,3 +15,3 @@ #grunt-data-uri | ||
Add your project's `grunt.js` (`Gruntfile.js` when ~0.4.0a). | ||
Add your project's `grunt.js` (`Gruntfile.js` when ~0.4.0rc5). | ||
@@ -39,3 +39,5 @@ ```javascript | ||
// adjust relative path? | ||
fixDirLevel: true | ||
fixDirLevel: true, | ||
// img detecting base dir | ||
// baseDir: './' | ||
} | ||
@@ -47,2 +49,4 @@ } | ||
For traversal image files. If `options.baseDir` is specified, use `baseDir` instead of *src css exsting dir*. That's useful when image paths in your css are absolute. | ||
###Before `sample/css/raw/main.css` | ||
@@ -82,2 +86,11 @@ | ||
div { background-image: url('../img/not_encode.png'); } | ||
``` | ||
``` | ||
##Changelog | ||
+ 0.1.0 | ||
+ Add `baseDir` option | ||
+ 0.0.2 | ||
+ Add `datauri` module | ||
+ 0.0.1 | ||
+ first commit |
@@ -15,7 +15,14 @@ /* | ||
path = require('path'), | ||
datauri = require("datauri"); | ||
datauri = require('datauri'); | ||
var RE_CSS_URLFUNC = /(?:url\(["']?)(.*?)(?:["']?\))/, | ||
util = grunt.util || grunt.utils, // for 0.4.0 | ||
baseGrunt = path.resolve('./'); | ||
gruntfileDir = path.resolve('./'), | ||
expandFiles = grunt.file.expandFiles ? | ||
function(files) { | ||
return grunt.file.expandFiles(files); | ||
}: | ||
function(files) { | ||
return grunt.file.expand({filter: 'isFile'}, files); | ||
}; | ||
@@ -26,19 +33,34 @@ grunt.registerMultiTask('dataUri', 'Convert your css file image path!!', function() { | ||
var options = this.options ? this.options() : this.data.options, // for 0.4.0 | ||
srcFiles = grunt.file.expandFiles(this.data.src), | ||
srcFiles = expandFiles(this.data.src), | ||
destDir = path.resolve(this.data.dest), | ||
haystack = grunt.file.expandFiles(options.target); | ||
haystack = []; | ||
expandFiles(options.target).forEach(function(imgPath) { | ||
haystack.push(path.resolve(imgPath)); | ||
}); | ||
srcFiles.forEach(function(src) { | ||
var content = grunt.file.read(src), | ||
matches = content.match(new RegExp(RE_CSS_URLFUNC.source, 'g')), | ||
baseSrc = path.resolve(path.dirname(src)), | ||
var content = grunt.file.read(src), | ||
matches = content.match(new RegExp(RE_CSS_URLFUNC.source, 'g')), | ||
outputTo = destDir+'/'+path.basename(src), | ||
baseDir, | ||
uris; | ||
// Detect baseDir for using traversal image files | ||
baseDir = options.baseDir ? path.resolve(options.baseDir) // specified base dir | ||
: path.resolve(path.dirname(src)); // detected from src | ||
// Not found image path | ||
if (!matches) { | ||
grunt.log.subhead('file uri not found on '+src); | ||
grunt.log.subhead('SRC: file uri not found on '+src); | ||
grunt.file.write(outputTo, content); | ||
grunt.log.ok('Skipped'); | ||
grunt.log.ok('=> ' + outputTo); | ||
return; | ||
} | ||
// Change base to src(css, html, js) existing dir | ||
grunt.file.setBase(baseDir); | ||
// List uniq image URIs | ||
uris = util._.uniq(matches.map(function(m) { | ||
@@ -48,6 +70,3 @@ return m.match(RE_CSS_URLFUNC)[1]; | ||
// Change base to src(css, html, js) existing dir | ||
grunt.file.setBase(baseSrc); | ||
// Not external http resource | ||
// Exclude external http resource | ||
uris = uris.filter(function(u) { | ||
@@ -57,9 +76,16 @@ return !u.match('(data:|http)'); | ||
grunt.log.subhead(uris.length+' file uri found on '+src); | ||
grunt.log.subhead('SRC: '+uris.length+' file uri found on '+src); | ||
// Process urls | ||
uris.forEach(function(u) { | ||
var src, replacement, | ||
needle = path.resolve(u).slice((baseGrunt+'/').length); | ||
var src, replacement, needle; | ||
// To current dir when specified uri is like root | ||
if (u.indexOf('/') === 0) { | ||
u = '.' + u; | ||
} | ||
// Resolve image realpath | ||
needle = path.resolve(u); | ||
// Assume file existing cause found from haystack | ||
@@ -74,3 +100,3 @@ if (haystack.indexOf(needle) !== -1) { | ||
// Diff of directory level | ||
replacement = adjustDirectoryLevel(u, destDir, baseSrc); | ||
replacement = adjustDirectoryLevel(u, destDir, baseDir); | ||
grunt.log.ok('Adjust: '+ u + ' -> ' + replacement); | ||
@@ -87,3 +113,3 @@ } else { | ||
// Revert base to gruntjs executing current dir | ||
grunt.file.setBase(baseGrunt); | ||
grunt.file.setBase(gruntfileDir); | ||
grunt.file.write(outputTo, content); | ||
@@ -102,3 +128,4 @@ grunt.log.ok('=> ' + outputTo); | ||
function adjustDirectoryLevel(relativePath, toDir, fromDir) { | ||
var resolvedPath = relativePath; | ||
// fix ../path/to/img.jpg to path/to/img.jpg | ||
var resolvedPath = relativePath.replace(/^\.\//, ''); | ||
@@ -105,0 +132,0 @@ if (toDir === fromDir) { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
9038
167
93
0