postcss-base64
Advanced tools
Comparing version 0.4.1 to 0.5.1
13
index.js
var fs = require('fs'), | ||
path = require('path'), | ||
postcss = require('postcss'); | ||
@@ -11,9 +12,9 @@ | ||
function replaceFiles(string) { | ||
function replaceFiles(string, opts) { | ||
file = getUrl(string); | ||
ext = file.split('.')[1]; | ||
ext = path.extname(file).replace('.', ''); | ||
if(ext === 'svg') ext = ext + '+xml'; | ||
fileContents = fs.readFileSync(file); | ||
fileContents = fs.readFileSync(path.join(opts.root, file)); | ||
output = 'data:image/' + ext + ';base64,' + fileContents.toString('base64'); | ||
@@ -41,2 +42,6 @@ | ||
if(!opts.root) { | ||
opts.root = process.cwd(); | ||
} | ||
if(opts.extensions) { | ||
@@ -47,3 +52,3 @@ exts = '\\' + opts.extensions.join('|\\'); | ||
css.replaceValues(search, function (string) { | ||
return replaceFiles(string); | ||
return replaceFiles(string, opts); | ||
}); | ||
@@ -50,0 +55,0 @@ } |
{ | ||
"name": "postcss-base64", | ||
"version": "0.4.1", | ||
"version": "0.5.1", | ||
"description": "Replace values in url() statements with base64 encoded strings.", | ||
@@ -21,4 +21,4 @@ "author": "Jelmer de Maat", | ||
"devDependencies": { | ||
"ava": "^0.8.0" | ||
"ava": "^0.17.0" | ||
} | ||
} |
@@ -25,3 +25,3 @@ postcss-base64, a [PostCSS](https://github.com/postcss/postcss/) plugin, replaces urls or values inside `url()` functions with their base64 encoded strings. | ||
_Example: `extensions: ['.svg']`_ | ||
`extensions: ['.svg']` | ||
@@ -32,4 +32,10 @@ #### pattern | ||
_Example: `pattern: /<svg.*<\/svg>/i`_ | ||
`pattern: /<svg.*<\/svg>/i` | ||
#### root | ||
A root folder in which to search for the files. The path in the CSS file gets appended to this. Default: `process.cwd()` (current working directory). | ||
`root: 'any/path/to/files/'` | ||
#### prepend | ||
@@ -39,3 +45,3 @@ | ||
_Example: `prepend: 'data:image/svg+xml;base64,'`_ | ||
`prepend: 'data:image/svg+xml;base64,'` | ||
@@ -42,0 +48,0 @@ |
@@ -16,3 +16,3 @@ var fs = require('fs'), | ||
output = postcss().use(base64(opts)).process(src).css, | ||
outputFile = fs.writeFileSync('./prepend-output.css', output), | ||
outputFile = fs.writeFileSync(path.join(__dirname, 'prepend-output.css'), output), | ||
outputFileContents = fs.readFileSync(path.join(__dirname, 'prepend-output.css')).toString(); | ||
@@ -19,0 +19,0 @@ |
@@ -9,3 +9,4 @@ var fs = require('fs'), | ||
extensions: ['.png', '.svg'], | ||
pattern: /<svg.*<\/svg>/i | ||
pattern: /<svg.*<\/svg>/i, | ||
root: 'test/' | ||
}; | ||
@@ -16,3 +17,3 @@ | ||
output = postcss().use(base64(opts)).process(src).css, | ||
outputFile = fs.writeFileSync('./output.css', output), | ||
outputFile = fs.writeFileSync(path.join(__dirname, 'output.css'), output), | ||
outputFileContents = fs.readFileSync(path.join(__dirname, 'output.css')).toString(); | ||
@@ -19,0 +20,0 @@ |
10117
126
98