postcss-url
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -0,1 +1,5 @@ | ||
# 3.1.0 - 2015-05-01 | ||
- Added: New copy value for `url` option | ||
# 3.0.0 - 2015-03-02 | ||
@@ -2,0 +6,0 @@ |
78
index.js
@@ -10,2 +10,5 @@ /** | ||
var reduceFunctionCall = require("reduce-function-call") | ||
var mkdirp = require("mkdirp") | ||
var crypto = require("crypto") | ||
var pathIsAbsolute = require("path-is-absolute") | ||
@@ -109,2 +112,4 @@ /** | ||
return processInline(from, dirname, urlMeta, options) | ||
case "copy": | ||
return processCopy(from, dirname, urlMeta, to, options) | ||
default: | ||
@@ -208,1 +213,74 @@ throw new Error("Unknow mode for postcss-url: " + mode) | ||
/** | ||
* Copy images from readed from url() to an specific assets destination (`assetsPath`) | ||
* and fix url() according to that path. | ||
* You can rename the assets by a hash or keep the real filename. | ||
* | ||
* Option assetsPath is require and is relative to the css destination (`to`) | ||
* | ||
* @param {String} from from | ||
* @param {String} dirname to dirname | ||
* @param {String} urlMeta url meta data | ||
* @param {String} to destination | ||
* @param {Object} options plugin options | ||
* @return {String} new url | ||
*/ | ||
function processCopy(from, dirname, urlMeta, to, options) { | ||
if ( from === to ) { | ||
console.warn("Option `to` of postscss is required, ignoring") | ||
return createUrl(urlMeta) | ||
} | ||
var relativeAssetsPath = (options && options.assetsPath) ? options.assetsPath : "" | ||
var absoluteAssetsPath | ||
var filePathUrl = path.resolve(dirname, urlMeta.value) | ||
var nameUrl = path.basename(filePathUrl) | ||
// remove hash or parameters in the url. e.g., url('glyphicons-halflings-regular.eot?#iefix') | ||
var filePath = url.parse(filePathUrl, true).pathname | ||
var name = path.basename(filePath) | ||
var useHash = options.useHash || false | ||
//check if the file exist in the source | ||
try { | ||
var contents = fs.readFileSync(filePath) | ||
} catch (err) { | ||
console.warn("Can't read file '" + filePath + "', ignoring") | ||
return createUrl(urlMeta) | ||
} | ||
if (useHash) { | ||
absoluteAssetsPath = path.resolve(to, relativeAssetsPath) | ||
// create the destination directory if it not exist | ||
mkdirp.sync(absoluteAssetsPath) | ||
name = crypto.createHash("sha1") | ||
.update(contents) | ||
.digest("hex") | ||
.substr(0, 16) | ||
nameUrl = name + path.extname(filePathUrl) | ||
name += path.extname(filePath) | ||
} else { | ||
if ( !pathIsAbsolute.posix(from) ) { | ||
from = path.resolve(from) | ||
} | ||
relativeAssetsPath = path.join(relativeAssetsPath, dirname.replace(new RegExp(from + "[\/]\?"), ""), path.dirname(urlMeta.value)) | ||
absoluteAssetsPath = path.resolve(to, relativeAssetsPath) | ||
// create the destination directory if it not exist | ||
mkdirp.sync(absoluteAssetsPath) | ||
} | ||
absoluteAssetsPath = path.join(absoluteAssetsPath, name) | ||
// if the file don't exist in the destination, create it. | ||
try { | ||
fs.accessSync(absoluteAssetsPath) | ||
} catch (err) { | ||
fs.writeFileSync(absoluteAssetsPath, contents) | ||
} | ||
return createUrl(urlMeta, path.join(relativeAssetsPath, nameUrl)) | ||
} |
{ | ||
"name": "postcss-url", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "PostCSS plugin to rebase or inline on url().", | ||
@@ -30,2 +30,4 @@ "keywords": [ | ||
"mime": "^1.2.11", | ||
"mkdirp": "^0.5.0", | ||
"path-is-absolute": "^1.0.0", | ||
"reduce-function-call": "^1.0.1" | ||
@@ -32,0 +34,0 @@ }, |
# postcss-url [![Build Status](https://travis-ci.org/postcss/postcss-url.png)](https://travis-ci.org/postcss/postcss-url) | ||
> [PostCSS](https://github.com/postcss/postcss) plugin to rebase or inline on url(). | ||
> [PostCSS](https://github.com/postcss/postcss) plugin to rebase, inline or copy on url(). | ||
@@ -25,3 +25,3 @@ ## Installation | ||
.use(url({ | ||
url: "rebase" // or "inline" | ||
url: "rebase" // or "inline" or "copy" | ||
})) | ||
@@ -31,2 +31,3 @@ .process(css, { | ||
// "inline" mode might need `from` option only | ||
// "copy" mode need `from` and `to` option to work | ||
from: "src/stylesheet/index.css" | ||
@@ -52,2 +53,6 @@ to: "dist/index.css" | ||
##### `url: "copy"` | ||
Allow you to copy and rebase assets according to postcss `to`, `assetsPath` and `from` options (`assetsPath` is relative to the option `to`). | ||
##### `url: {Function}` | ||
@@ -66,2 +71,10 @@ | ||
#### `assetsPath: "the destination where postcss-url is going to copy the assets"` | ||
If you specify an assetsPath the assets files would be copy in that destination | ||
#### `useHash: "If is set in true the copy method is going to rename the path of the files by a hash name"` | ||
By default is set in false but you can change that | ||
--- | ||
@@ -68,0 +81,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
14890
249
92
6
+ Addedmkdirp@^0.5.0
+ Addedpath-is-absolute@^1.0.0
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)