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/0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
borodean committed Nov 3, 2014
2 parents 11bec66 + 97e93da commit f0e2b02
Show file tree
Hide file tree
Showing 39 changed files with 544 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/

*.actual.css
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Vadim Borodean

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
PostCSS Assets
==============

An asset manager for CSS.

Usage
-----

### [Gulp PostCSS](https://github.com/w0rm/gulp-postcss)

```js
gulp.task('assets', function () {
var postcss = require('postcss');
var assets = require('postcss-assets');

return gulp.src('source/*.css')
.pipe(postcss([ assets() ]))
.pipe(gulp.dest('build/'));
});
```

### [Grunt PostCSS](https://github.com/nDmitry/grunt-postcss)

```js
var assets = require('postcss-assets');

grunt.initConfig({
postcss: {
options: {
processors: [
assets().postcss
]
},
dist: { src: 'build/*.css' }
},
});
```
137 changes: 134 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,138 @@
var postcss = require('postcss');
var vendor = require('postcss/lib/vendor');

var mapFunctions = require('./lib/mapFunctions');
var parseBytes = require('./lib/parseBytes');
var unescapeCss = require('./lib/unescapeCss');

var fs = require('fs');
var path = require('path');
var url = require('url');

var base64 = require('js-base64').Base64;
var cssesc = require('cssesc');
var mime = require('mime');
var sizeOf = require('image-size');

const AUTO_SIZE = ['background-size', 'border-image-width', 'border-width',
'margin', 'padding'];
const AUTO_WIDTH = ['border-left', 'border-left-width', 'border-right',
'border-right-width', 'left', 'margin-left',
'margin-right', 'max-width', 'min-width', 'padding-left',
'padding-right', 'width'];
const AUTO_HEIGHT = ['border-bottom', 'border-bottom-width', 'border-top',
'border-top-width', 'bottom', 'height', 'margin-bottom',
'margin-top', 'max-height', 'min-height',
'padding-bottom', 'padding-top'];

module.exports = function (options) {

options = options || {};
return function (css) {};
};
options.baseUrl = options.baseUrl || '/';

if (options.basePath) {
options.basePath = path.resolve(options.basePath);
} else {
options.basePath = process.cwd();
}

if (options.loadPaths) {
options.loadPaths = options.loadPaths.map(function (loadPath) {
return path.resolve(options.basePath, loadPath);
});
} else {
options.loadPaths = [];
}
options.loadPaths.unshift(options.basePath);

if (options.relativeTo) {
options.relativeTo = path.resolve(options.relativeTo);
} else {
options.relativeTo = false;
}

function matchPath(assetPath) {
var exception, matchingPath;
var isFound = options.loadPaths.some(function (loadPath) {
matchingPath = path.join(loadPath, assetPath);
return fs.existsSync(matchingPath);
});
if (!isFound) {
exception = new Error("Asset not found or unreadable: " + assetPath);
exception.name = 'ENOENT';
throw exception;
}
return matchingPath;
}

function resolveDataUrl(assetStr) {
var resolvedPath = resolvePath(assetStr);
var mimeType = mime.lookup(resolvedPath);
var data = base64.encode(fs.readFileSync(resolvedPath));
return 'data:' + mimeType + ';base64,' + data;
}

function resolvePath(assetStr) {
var assetUrl = url.parse(unescapeCss(assetStr));
var assetPath = decodeURI(assetUrl.pathname);
return matchPath(assetPath);
}

function resolveUrl(assetStr) {
var assetUrl = url.parse(unescapeCss(assetStr));
var assetPath = decodeURI(assetUrl.pathname);
if (options.relativeTo) {
assetUrl.pathname = path.relative(options.relativeTo, matchPath(assetPath));
} else {
var baseToAsset = path.relative(options.basePath, matchPath(assetPath));
assetUrl.pathname = url.resolve(options.baseUrl, baseToAsset);
}
return cssesc(url.format(assetUrl));
}

function shouldBeInline(assetPath) {
if (options.inline && options.inline.maxSize) {
var size = fs.statSync(assetPath).size;
return (size <= parseBytes(options.inline.maxSize));
}
return false;
}

return function (cssTree) {
cssTree.eachDecl(function (decl) {

decl.value = mapFunctions(decl.value, function (before, quote, assetStr, modifier, after) {

try {

var assetPath = resolvePath(assetStr);
var prop = vendor.unprefixed(decl.prop);

if (modifier === 'width' || AUTO_WIDTH.indexOf(prop) !== -1) {
return sizeOf(assetPath).width + 'px';
}

if (modifier === 'height' || AUTO_HEIGHT.indexOf(prop) !== -1) {
return sizeOf(assetPath).height + 'px';
}

if (modifier === 'size' || AUTO_SIZE.indexOf(prop) !== -1) {
var size = sizeOf(assetPath);
return size.width + 'px ' + size.height + 'px';
}

if (shouldBeInline(assetPath)) {
return 'url(' + before + quote + resolveDataUrl(assetStr) + quote + after + ')';
}

return 'url(' + before + quote + resolveUrl(assetStr) + quote + after + ')';

} catch (exception) {
if (exception.name !== 'ENOENT') {
throw exception;
}
console.warn('%s\nLoad paths:\n %s', exception.message, options.loadPaths.join('\n '));
}
});
});
};
};
12 changes: 12 additions & 0 deletions lib/mapFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const R_FUNC = /url\((\s*)((['"]?).*?\3.*?)(\s*)\)/gi;
const R_PARAMS = /^(['"]?)(.+?)\1(?:\s+(.+))?$/;

module.exports = function mapFunctions(cssValue, callback) {
return cssValue.replace(R_FUNC, function (matches, before, params, quote, after) {
params = params.match(R_PARAMS);
var assetStr = params[2];
var modifier = params[3];
var result = callback(before, quote, assetStr, modifier, after);
return result || matches;
});
};
11 changes: 11 additions & 0 deletions lib/parseBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function (string) {
string = string.toString();
var value = parseFloat(string, 10);
var unit = string.slice(-1).toUpperCase();
if (unit === 'K') {
value *= 1024;
} else if (unit === 'M') {
value *= Math.pow(1024, 2);
}
return Math.floor(value);
};
8 changes: 8 additions & 0 deletions lib/unescapeCss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const R_ESCAPE = /\\(?:([0-9a-f]{1,6} ?)|(.))/gi;

module.exports = function (str) {
return str.replace(R_ESCAPE, function (match, hex, char) {
if (hex) return String.fromCharCode(parseInt(hex, 16));
return char;
});
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
"name": "postcss-assets",
"version": "1.0.0",
"version": "0.9.0",
"description": "PostCSS plugin to manage assets",
"author": "Vadim Borodean <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/borodean/postcss-assets"
},
"dependencies": {
"cssesc": "^0.1.0",
"image-size": "^0.3.3",
"js-base64": "^2.1.5",
"mime": "^1.2.11",
"postcss": "^2.2.5"
},
"devDependencies": {
Expand Down
Binary file added test/fixtures/alpha/Gupsy Fortuneteller.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/alpha/Ich ♥ Bücher.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/alpha/blank.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/alpha/kateryna.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/alpha/kateryna_(shevchenko).jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/alpha/odalisque.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/beta/maria.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions test/fixtures/dimensions.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
body {
width: url('beta/maria.jpg' width);
height: url('beta/maria.jpg' height);
height: url('beta/maria.jpg?foo=bar#baz' height);
width: url('beta/maria.jpg');
height: url('beta/maria.jpg');
background-size: url('beta/maria.jpg' size);
-webkit-background-size: url('beta/maria.jpg');
-moz-background-size: url('beta/maria.jpg');
background-size: url('beta/maria.jpg');

background-size: url('beta/maria.jpg');
border-image-width: url('beta/maria.jpg');
border-width: url('beta/maria.jpg');
margin: url('beta/maria.jpg');
padding: url('beta/maria.jpg');

border-left: url('beta/maria.jpg') solid red;
border-left-width: url('beta/maria.jpg');
border-right: dashed url('beta/maria.jpg') blue;
border-right-width: url('beta/maria.jpg');
left: url('beta/maria.jpg');
margin-left: url('beta/maria.jpg');
margin-right: url('beta/maria.jpg');
max-width: url('beta/maria.jpg');
min-width: url('beta/maria.jpg');
padding-left: url('beta/maria.jpg');
padding-right: url('beta/maria.jpg');
width: url('beta/maria.jpg');

border-bottom: double green url('beta/maria.jpg');
border-bottom-width: url('beta/maria.jpg');
border-top: black url('beta/maria.jpg') solid;
border-top-width: url('beta/maria.jpg');
bottom: url('beta/maria.jpg');
height: url('beta/maria.jpg');
margin-bottom: url('beta/maria.jpg');
margin-top: url('beta/maria.jpg');
max-height: url('beta/maria.jpg');
min-height: url('beta/maria.jpg');
padding-bottom: url('beta/maria.jpg');
padding-top: url('beta/maria.jpg');
}
43 changes: 43 additions & 0 deletions test/fixtures/dimensions.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
body {
width: 45px;
height: 60px;
height: 60px;
width: 45px;
height: 60px;
background-size: 45px 60px;
-webkit-background-size: 45px 60px;
-moz-background-size: 45px 60px;
background-size: 45px 60px;

background-size: 45px 60px;
border-image-width: 45px 60px;
border-width: 45px 60px;
margin: 45px 60px;
padding: 45px 60px;

border-left: 45px solid red;
border-left-width: 45px;
border-right: dashed 45px blue;
border-right-width: 45px;
left: 45px;
margin-left: 45px;
margin-right: 45px;
max-width: 45px;
min-width: 45px;
padding-left: 45px;
padding-right: 45px;
width: 45px;

border-bottom: double green 60px;
border-bottom-width: 60px;
border-top: black 60px solid;
border-top-width: 60px;
bottom: 60px;
height: 60px;
margin-bottom: 60px;
margin-top: 60px;
max-height: 60px;
min-height: 60px;
padding-bottom: 60px;
padding-top: 60px;
}
5 changes: 5 additions & 0 deletions test/fixtures/inline.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: url('alpha/blank.gif');
background: url( 'alpha/blank.gif' );
background: url('alpha/kateryna.jpg');
}
5 changes: 5 additions & 0 deletions test/fixtures/inline.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: url('data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==');
background: url( 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==' );
background: url('/alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-basepath.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-basepath.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('/alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-baseurl-1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-baseurl-1.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('/content/theme/alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-baseurl-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-baseurl-2.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('http://example.com/alpha/kateryna.jpg');
}
5 changes: 5 additions & 0 deletions test/fixtures/resolve-loadpath.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: url('kateryna.jpg');
background: url('maria.jpg');
background: url('beta/maria.jpg');
}
5 changes: 5 additions & 0 deletions test/fixtures/resolve-loadpath.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: url('/alpha/kateryna.jpg');
background: url('/beta/maria.jpg');
background: url('/beta/maria.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-notfound.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('three-bears.jpg');
}
Loading

0 comments on commit f0e2b02

Please sign in to comment.