postcss-url
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -0,1 +1,5 @@ | ||
# 1.1.0 - 2014-10-29 | ||
- Add `maxSize` (size in kbytes) and `basePath` (base path for images to inline) options for _inline_ mode. | ||
# 1.0.2 - 2014-10-10 | ||
@@ -2,0 +6,0 @@ |
150
index.js
@@ -6,5 +6,5 @@ /** | ||
var path = require("path") | ||
var reduceFunctionCall = require("reduce-function-call") | ||
var base64 = require("js-base64").Base64 | ||
var mime = require("mime") | ||
var reduceFunctionCall = require("reduce-function-call"); | ||
@@ -24,3 +24,3 @@ /** | ||
styles.eachDecl(function checkUrl(decl) { | ||
styles.eachDecl(function(decl) { | ||
if (!decl.value) { | ||
@@ -31,53 +31,113 @@ return | ||
if (decl.value.indexOf("url(") > -1) { | ||
var dirname = path.dirname(decl.source.file) | ||
decl.value = reduceFunctionCall(decl.value, "url", function(value) { | ||
// save quote style | ||
var quote = getQuote(value) | ||
value = unquote(value, quote) | ||
processDecl(decl, from, to, mode, options) | ||
} | ||
}) | ||
} | ||
} | ||
// ignore absolute url | ||
if (value.indexOf("/") === 0) { | ||
return value | ||
} | ||
/** | ||
* Processes one delcaration | ||
* | ||
* @param {Object} decl | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {String} mode | ||
* @param {Object} options | ||
*/ | ||
function processDecl(decl, from, to, mode, options) { | ||
var dirname = path.dirname(decl.source.file) | ||
decl.value = reduceFunctionCall(decl.value, "url", function(value) { | ||
// save quote style | ||
var quote = getQuote(value) | ||
value = unquote(value, quote) | ||
var newPath = value | ||
// ignore absolute url | ||
if (value.indexOf("/") === 0) { | ||
return value | ||
} | ||
if (mode === "rebase") { | ||
if (dirname !== from) { | ||
newPath = path.relative(from, dirname + path.sep + newPath) | ||
} | ||
newPath = path.resolve(from, newPath) | ||
newPath = path.relative(to, newPath) | ||
if (path.sep == "\\") { | ||
newPath = newPath.replace(/\\/g, "\/"); | ||
} | ||
} | ||
else if (mode === "inline") { | ||
var file = path.resolve(from, dirname !== from ? dirname + path.sep + value : value) | ||
if (!fs.existsSync(file)) { | ||
console.warn("Can't read file '" + file + "', ignoring") | ||
} | ||
else { | ||
var mimeType = mime.lookup(file) | ||
if (!mimeType) { | ||
console.warn("Unable to find asset mime-type for " + file) | ||
} | ||
else { | ||
file = fs.readFileSync(file).toString() | ||
newPath = "data:" + mimeType + ";base64," + base64.encode(file) | ||
} | ||
} | ||
} | ||
else { | ||
throw new Error("Unknow mode for postcss-url: " + mode) | ||
} | ||
var newPath = value | ||
return "url(" + quote + newPath + quote + ")" | ||
}) | ||
} | ||
}) | ||
switch (mode) { | ||
case "rebase": | ||
return processRebase(from, dirname, newPath, quote, to) | ||
break | ||
case "inline": | ||
return processInline(from, dirname, newPath, quote, value, options) | ||
break | ||
default: | ||
throw new Error("Unknow mode for postcss-url: " + mode) | ||
break | ||
} | ||
}) | ||
} | ||
/** | ||
* Fix url() according to source (`from`) or destination (`to`) | ||
* | ||
* @param {String} from | ||
* @param {String} dirname | ||
* @param {String} newPath | ||
* @param {String} quote | ||
* @param {String} to | ||
*/ | ||
function processRebase(from, dirname, newPath, quote, to) { | ||
if (dirname !== from) { | ||
newPath = path.relative(from, dirname + path.sep + newPath) | ||
} | ||
newPath = path.resolve(from, newPath) | ||
newPath = path.relative(to, newPath) | ||
if (path.sep == "\\") { | ||
newPath = newPath.replace(/\\/g, "\/"); | ||
} | ||
return createUrl(quote, newPath); | ||
} | ||
/** | ||
* Inline image in url() | ||
* | ||
* @param {String} from | ||
* @param {String} dirname | ||
* @param {String} newPath | ||
* @param {String} quote | ||
* @param {String} value | ||
* @param {Object} options | ||
*/ | ||
function processInline(from, dirname, newPath, quote, value, options) { | ||
var maxSize = typeof(options.maxSize) == "undefined" ? 14 : options.maxSize | ||
var basePath = options.basePath; | ||
var fullFilePath; | ||
maxSize *= 1024 * 8 | ||
if (basePath) { | ||
fullFilePath = path.join(basePath, value) | ||
} | ||
else { | ||
fullFilePath = dirname !== from ? dirname + path.sep + value : value | ||
} | ||
var file = path.resolve(from, fullFilePath) | ||
if (!fs.existsSync(file)) { | ||
console.warn("Can't read file '" + file + "', ignoring") | ||
} | ||
else { | ||
var mimeType = mime.lookup(file) | ||
var stats = fs.statSync(file); | ||
if (stats.size >= maxSize) { | ||
return createUrl(quote, newPath) | ||
} | ||
if (!mimeType) { | ||
console.warn("Unable to find asset mime-type for " + file) | ||
} | ||
else { | ||
file = fs.readFileSync(file).toString() | ||
newPath = "data:" + mimeType + ";base64," + base64.encode(file) | ||
} | ||
} | ||
return createUrl(quote, newPath) | ||
} | ||
function createUrl(quote, newPath) { | ||
return "url(" + quote + newPath + quote + ")" | ||
} | ||
/** | ||
* remove quote around a string | ||
@@ -84,0 +144,0 @@ * |
{ | ||
"name": "postcss-url", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "PostCSS plugin to rebase or inline on url().", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -48,2 +48,10 @@ # postcss-url [![Build Status](https://travis-ci.org/postcss/postcss-url.png)](https://travis-ci.org/postcss/postcss-url) | ||
##### `maxSize: "size in kbytes"` | ||
Specify the maximum file size to inline | ||
##### `basePath: "basePath for images to inline"` | ||
Specify the basePath from where to search images | ||
--- | ||
@@ -50,0 +58,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8097
154
70