Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
borodean committed Apr 24, 2016
2 parents f7be70e + 7ed9db8 commit 5e739f6
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 196 deletions.
18 changes: 1 addition & 17 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
{
"extends": "eslint:recommended",
"env": {
"node": true
},
"rules": {
"indent": [2, 2],
"linebreak-style": [2, "unix"],
"no-console": [0],
"quotes": [2, "single"],
"semi": [2, "always"],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, {
"anonymous": "always",
"named": "never"
}],
"space-before-keywords": [2, "always"]
},
"extends": "airbnb/legacy",
"root": true
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ var options = {
};
```

To relate to a particular directory, set it as a string:

```js
var options = {
relative: 'assets/css'
};
```

Cachebuster
-----------

Expand Down Expand Up @@ -235,4 +243,4 @@ Full list of options
| `baseUrl` | URL of the project when running the web server. | `/` |
| `cachebuster` | If cache should be busted. Pass a function to define custom busting strategy. | `false` |
| `loadPaths` | Specific directories to look for the files. | `[]` |
| `relative` | Should the resolved path be relative to the input file | `false` |
| `relative` | Directory to relate to when resolving URLs. When `true`, relates to the input file. When `false`, disables relative URLs. | `false` |
98 changes: 55 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,94 @@
var Assets = require('assets');
var dirname = require('path').dirname;
var functions = require('postcss-functions');
var path = require('path');
var postcss = require('postcss');
var quote = require('./lib/quote');
var unescapeCss = require('./lib/unescape-css');
var unquote = require('./lib/unquote');
var util = require('util');

module.exports = postcss.plugin('postcss-assets', function (options) {
options = options || {};
function formatUrl(url) {
return util.format('url(%s)', quote(url));
}

if (options.relative === undefined) {
options.relative = false;
function formatSize(measurements) {
return util.format('%dpx %dpx', measurements.width, measurements.height);
}

function formatWidth(measurements) {
return util.format('%dpx', measurements.width);
}

function formatHeight(measurements) {
return util.format('%dpx', measurements.height);
}

function plugin(options) {
var params = options || {};
var resolver;

if (params.relative === undefined) {
params.relative = false;
}

var resolver = Assets(options);
resolver = new Assets(options);

function measure(path, density) {
return resolver.size(path)
.then(function (size) {
.then(function correctDensity(size) {
if (density !== undefined) {
size.width = Number((size.width / density).toFixed(4));
size.height = Number((size.height / density).toFixed(4));
return {
width: Number((size.width / density).toFixed(4)),
height: Number((size.height / density).toFixed(4))
};
}
return size;
});
}

return postcss()
.use(function (css) {
.use(function appendInputDir(css) {
var inputDir;

if (css.source.input.file) {
var inputDir = path.dirname(css.source.input.file);
inputDir = dirname(css.source.input.file);

resolver.options.loadPaths = resolver.options.loadPaths || [];
resolver.options.loadPaths.unshift(inputDir);

if (options.relative) {
if (params.relative === true) {
resolver.options.relativeTo = inputDir;
}
}

if (typeof params.relative === 'string') {
resolver.options.relativeTo = params.relative;
}
})
.use(functions({
functions: {
resolve: function (path) {
path = unquote(unescapeCss(path));
return resolver.url(path)
.then(function (url) {
return util.format('url(%s)', quote(url));
});
resolve: function resolve(path) {
var normalizedPath = unquote(unescapeCss(path));
return resolver.url(normalizedPath).then(formatUrl);
},
inline: function (path) {
path = unquote(unescapeCss(path));
return resolver.data(path)
.then(function (data) {
return util.format('url(%s)', quote(data));
});
inline: function inline(path) {
var normalizedPath = unquote(unescapeCss(path));
return resolver.data(normalizedPath).then(formatUrl);
},
size: function (path, density) {
path = unquote(unescapeCss(path));
return measure(path, density)
.then(function (size) {
return util.format('%dpx %dpx', size.width, size.height);
});
size: function size(path, density) {
var normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatSize);
},
width: function (path, density) {
path = unquote(unescapeCss(path));
return measure(path, density)
.then(function (size) {
return util.format('%dpx', size.width);
});
width: function width(path, density) {
var normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatWidth);
},
height: function (path, density) {
path = unquote(unescapeCss(path));
return measure(path, density)
.then(function (size) {
return util.format('%dpx', size.height);
});
height: function height(path, density) {
var normalizedPath = unquote(unescapeCss(path));
return measure(normalizedPath, density).then(formatHeight);
}
}
}));
});
}

module.exports = postcss.plugin('postcss-assets', plugin);
16 changes: 9 additions & 7 deletions lib/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ var util = require('util');

var R_QUOTES = /'/g;

module.exports = function (string) {
function escapeQuote(match, offset, string) {
if (string[offset - 1] === '\\') {
return match;
}
return '\\' + match;
}

module.exports = function quote(string) {
if (string[0] === "'" || string[0] === '"') {
return string;
}
return util.format("'%s'", string.replace(R_QUOTES, function (match, offset, string) {
if (string[offset - 1] === '\\') {
return match;
}
return '\\' + match;
}));
return util.format("'%s'", string.replace(R_QUOTES, escapeQuote));
};
16 changes: 9 additions & 7 deletions lib/unescape-css.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var R_ESCAPE = /\\(?:([0-9a-f]{1,6} ?)|(.))/gi;

module.exports = function (string) {
return string.replace(R_ESCAPE, function (match, hex, char) {
if (hex) {
return String.fromCharCode(parseInt(hex, 16));
}
return char;
});
function unescapeSequence(match, hex, char) {
if (hex) {
return String.fromCharCode(parseInt(hex, 16));
}
return char;
}

module.exports = function unescapeCss(string) {
return string.replace(R_ESCAPE, unescapeSequence);
};
2 changes: 1 addition & 1 deletion lib/unquote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function (string) {
module.exports = function unquote(string) {
if (string[0] !== '\'' && string[0] !== '"') {
return string;
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-assets",
"version": "4.0.1",
"version": "4.1.0",
"description": "PostCSS plugin to manage assets",
"keywords": [
"assets",
Expand Down Expand Up @@ -33,8 +33,9 @@
"postcss-functions": "^2.1.0"
},
"devDependencies": {
"ava": "^0.11.0",
"eslint": "^1.10.3",
"nyc": "^5.5.0"
"ava": "^0.13.0",
"eslint": "^2.5.3",
"eslint-config-airbnb": "^6.2.0",
"nyc": "^6.1.1"
}
}
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "airbnb/base"
}
Loading

0 comments on commit 5e739f6

Please sign in to comment.