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/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
borodean committed Feb 13, 2015
2 parents aa5f4a4 + f79dfe1 commit 0d2539a
Show file tree
Hide file tree
Showing 24 changed files with 184 additions and 168 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ Example:

```css
body {
background: url('foobar.jpg');
background: url('icons/baz.png');
background: resolve('foobar.jpg');
background: resolve('icons/baz.png');
}
```

PostCSS Assets would look for the files in load paths, then in the base path. If it succeed, it would resolve a true URL:
PostCSS Assets would look for the files relative to the source file, then in load paths, then in the base path. If it succeed, it would resolve a true URL:

```css
body {
Expand Down
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var sizeOf = require('image-size');

module.exports = function (options) {

var inputPath;

options = options || {};
options.baseUrl = options.baseUrl || '/';

Expand Down Expand Up @@ -65,7 +67,12 @@ module.exports = function (options) {

function matchPath(assetPath) {
var exception, matchingPath;
var isFound = options.loadPaths.some(function (loadPath) {
if (typeof inputPath === 'string') {
var loadPaths = [path.dirname(inputPath)].concat(options.loadPaths);
} else {
loadPaths = options.loadPaths;
}
var isFound = loadPaths.some(function (loadPath) {
matchingPath = path.join(loadPath, assetPath);
return fs.existsSync(matchingPath);
});
Expand Down Expand Up @@ -118,9 +125,12 @@ module.exports = function (options) {

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

inputPath = decl.source.input.file;

try {
decl.value = mapFunctions(decl.value, {
'url': function (assetStr) {
'resolve': function (assetStr) {
assetStr.value = resolveUrl(assetStr.value);
return 'url(' + assetStr + ')';
},
Expand All @@ -146,11 +156,9 @@ module.exports = function (options) {
} catch (exception) {
switch (exception.name) {
case 'ECORRUPT':
console.warn(exception.message);
break;
throw decl.error(exception.message);
case 'ENOENT':
console.warn('%s\nLoad paths:\n %s', exception.message, options.loadPaths.join('\n '));
break;
throw decl.error(exception.message + '\nLoad paths:\n ' + options.loadPaths.join('\n '));
default:
throw exception;
}
Expand All @@ -161,4 +169,4 @@ module.exports = function (options) {

module.exports.postcss = function (cssTree) {
module.exports()(cssTree);
}
};
5 changes: 0 additions & 5 deletions lib/mapFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ module.exports = function mapFunctions(cssValue, map) {
var type = node[0];
var children = node.slice(1);

if (type === 'uri') {
node = gonzales.srcToCSSP(gonzales.csspToSrc(node), 'funktion');
return traverse(node);
}

if (type === 'funktion') {
var name = children[0][1];
var body = children[1].slice(1).map(function (x) {
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-assets",
"version": "1.1.4",
"version": "2.0.0",
"description": "PostCSS plugin to manage assets",
"author": "Vadim Borodean <[email protected]>",
"license": "MIT",
Expand All @@ -12,13 +12,14 @@
"cssesc": "^0.1.0",
"gonzales": "^1.0.7",
"image-size": "^0.3.5",
"mime": "^1.2.11",
"postcss": "^4.0.0"
"mime": "^1.3.4",
"postcss": "^4.0.3"
},
"devDependencies": {
"tape": "^3.0.3"
"chai": "^2.0.0",
"mocha": "^2.1.0"
},
"scripts": {
"test": "tape test"
"test": "mocha -R spec test"
}
}
Binary file added test/fixtures/alpha/kobzar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/fixtures/cachebuster.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
background: url('test/fixtures/alpha/kateryna.jpg');
background: url('test/fixtures/alpha/kateryna.jpg?foo=bar');
background: url('kateryna.jpg');
background: resolve('test/fixtures/alpha/kateryna.jpg');
background: resolve('test/fixtures/alpha/kateryna.jpg?foo=bar');
background: resolve('kateryna.jpg');
}
2 changes: 0 additions & 2 deletions test/fixtures/dimensions.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ body {
height: height('beta/maria.jpg', 2);

width: width('beta/maria.jpg', 1.3);

width: width('alpha/invalid.jpg');
}
2 changes: 0 additions & 2 deletions test/fixtures/dimensions.expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ body {
height: 30px;

width: 34.6154px;

width: width('alpha/invalid.jpg');
}
Binary file added test/fixtures/kobzar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/fixtures/resolve-basepath.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
background: resolve('alpha/kateryna.jpg');
}
2 changes: 1 addition & 1 deletion test/fixtures/resolve-baseurl-1.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
background: resolve('alpha/kateryna.jpg');
}
2 changes: 1 addition & 1 deletion test/fixtures/resolve-baseurl-2.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
background: url('alpha/kateryna.jpg');
background: resolve('alpha/kateryna.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-css-relative-paths.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: resolve('kobzar.jpg');
}
3 changes: 3 additions & 0 deletions test/fixtures/resolve-css-relative-paths.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: url('/fixtures/kobzar.jpg');
}
6 changes: 3 additions & 3 deletions test/fixtures/resolve-loadpath.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
background: url('kateryna.jpg');
background: url('maria.jpg');
background: url('beta/maria.jpg');
background: resolve('kateryna.jpg');
background: resolve('maria.jpg');
background: resolve('beta/maria.jpg');
}
3 changes: 0 additions & 3 deletions test/fixtures/resolve-notfound.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/resolve-notfound.expected.css

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/resolve-relative.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
background: url('kateryna.jpg');
background: resolve('kateryna.jpg');
}
28 changes: 14 additions & 14 deletions test/fixtures/resolve-spelling.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
body {
color: red;
background: url(kateryna.jpg);
background: url( kateryna.jpg );
background: no-repeat url(kateryna.jpg) #000;
background: resolve(kateryna.jpg);
background: resolve( kateryna.jpg );
background: no-repeat resolve(kateryna.jpg) #000;
background:
url('kateryna.jpg'),
url('odalisque.jpg');
background: url('kateryna.jpg');
background: url('./kateryna.jpg');
background: url("kateryna.jpg");
background: url('odalisque.jpg');
background: url('kateryna_(shevchenko).jpg');
background: url('\6b ateryna.jpg');
background: url('kateryna.jpg?foo=bar#baz');
background: url('kateryna.jpg#baz');
background: url('Gupsy Fortuneteller.jpg');
resolve('kateryna.jpg'),
resolve('odalisque.jpg');
background: resolve('kateryna.jpg');
background: resolve('./kateryna.jpg');
background: resolve("kateryna.jpg");
background: resolve('odalisque.jpg');
background: resolve('kateryna_(shevchenko).jpg');
background: resolve('\6b ateryna.jpg');
background: resolve('kateryna.jpg?foo=bar#baz');
background: resolve('kateryna.jpg#baz');
background: resolve('Gupsy Fortuneteller.jpg');
}
2 changes: 1 addition & 1 deletion test/fixtures/resolve.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
background: url('test/fixtures/alpha/kateryna.jpg');
background: resolve('test/fixtures/alpha/kateryna.jpg');
}
125 changes: 75 additions & 50 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
var postcss = require('postcss');
var expect = require('chai').expect;

var plugin = require('..');

var fs = require('fs');
var test = require('tape');

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

function fixturePath(name) {
return 'test/fixtures/' + name + '.css';
}

function fixture(name) {
return fs.readFileSync('test/fixtures/' + name + '.css', 'utf8').trim();
return fs.readFileSync(fixturePath(name), 'utf8').trim();
}

function process(name, opts, postcssOpts) {
return postcss().use(plugin(opts)).process(fixture(name), postcssOpts).css.trim();
function process(css, opts, postcssOpts) {
return postcss().use(plugin(opts)).process(css, postcssOpts).css.trim();
}

function compareFixtures(t, name, msg, opts, postcssOpts) {
function processFixture(name, opts, postcssOpts) {
return process(fixture(name), opts, { from: fixturePath(name) });
}

var actual = process(name, opts, postcssOpts);
var expected = fixture(name + '.expected');
function compareFixtures(name, opts, postcssOpts) {
return function () {
var actual = processFixture(name, opts, postcssOpts);
var expected = fixture(name + '.expected');

fs.writeFile('test/fixtures/' + name + '.actual.css', actual);
t.equal(actual, expected, msg);
fs.writeFile('test/fixtures/' + name + '.actual.css', actual);
expect(actual).to.equal(expected);
};
}

function modifyFile(path) {
Expand All @@ -32,76 +41,92 @@ function modifyFile(path) {
fs.utimesSync(path, atime, mtime);
}

test('path resolving', function (t) {

compareFixtures(t, 'resolve', 'resolves paths');
describe('path resolving', function () {
it('resolves paths', compareFixtures('resolve'));

compareFixtures(t, 'resolve-basepath', 'resolves relative to the basePath', {
it('resolves relative to the basePath', compareFixtures('resolve-basepath', {
basePath: 'test/fixtures'
});
}));

compareFixtures(t, 'resolve-loadpath', 'resolves relative to the loadPaths', {
it('resolves relative to the loadPaths', compareFixtures('resolve-loadpath', {
basePath: 'test/fixtures',
loadPaths: ['alpha/', 'beta/']
});
}));

compareFixtures(t, 'resolve-loadpath', 'resolves with loadPaths of a various spelling', {
it('resolves with loadPaths of a various spelling', compareFixtures('resolve-loadpath', {
basePath: 'test/fixtures',
loadPaths: ['./alpha/', 'beta']
});
}));

compareFixtures(t, 'resolve-baseurl-1', 'resolves relative to the baseUrl', {
it('resolves relative to the baseUrl', compareFixtures('resolve-baseurl-1', {
basePath: 'test/fixtures',
baseUrl: '/content/theme/'
});
}));

compareFixtures(t, 'resolve-baseurl-2', 'resolves relative to the baseUrl', {
it('resolves relative to the baseUrl', compareFixtures('resolve-baseurl-2', {
basePath: 'test/fixtures',
baseUrl: 'http://example.com'
});
}));

it('resolves relative to the CSS file', compareFixtures('resolve-css-relative-paths', {
basePath: 'test',
loadPaths: ['fixtures/alpha']
}));

compareFixtures(t, 'resolve-relative', 'resolves relative paths', {
it('resolves relative paths', compareFixtures('resolve-relative', {
basePath: 'test/fixtures/alpha',
relativeTo: 'test/fixtures/beta'
});
}));

compareFixtures(t, 'resolve-spelling', 'recognizes various spelling', {
it('recognizes various spelling', compareFixtures('resolve-spelling', {
basePath: 'test/fixtures',
loadPaths: ['alpha/']
});
}));

compareFixtures(t, 'resolve-notfound', 'does nothing', {
basePath: 'test/fixtures'
it('throws an error', function () {
expect(function () {
process('body { background: resolve("three-bears.jpg"); }');
}).to.throw('Asset not found or unreadable');
});

t.end();
});

test('path inlining', function (t) {
compareFixtures(t, 'inline', 'base64-encodes assets', { basePath: 'test/fixtures/' });
t.end();
describe('path inlining', function () {
it('base64-encodes assets', compareFixtures('inline', { basePath: 'test/fixtures/' }));
});

test('dimensions', function (t) {
compareFixtures(t, 'dimensions', 'resolves dimensions', { basePath: 'test/fixtures/' });
t.end();
describe('dimensions', function () {
it('resolves dimensions', compareFixtures('dimensions', { basePath: 'test/fixtures/' }));

it('throws an error', function () {
expect(function () {
process('body { width: width("test/fixtures/alpha/invalid.jpg"); }');
}).to.throw('Image corrupted');
});
});

test('cachebuster', function (t) {
var options = {
cachebuster: true,
loadPaths: ['test/fixtures/alpha/']
};
var a = process('cachebuster', options);
modifyFile('test/fixtures/alpha/kateryna.jpg');
var b = process('cachebuster', options);
t.notEqual(a, b, 'busts cache');
describe('cachebuster', function () {
it('busts cache', function () {
var options = {
cachebuster: true,
loadPaths: ['test/fixtures/alpha/']
};

options.cachebuster = function (path) {
return path[path.length - 1];
};
var resultA = processFixture('cachebuster', options);
modifyFile('test/fixtures/alpha/kateryna.jpg');

var resultB = processFixture('cachebuster', options);

compareFixtures(t, 'cachebuster', 'accepts buster function', options);
expect(resultA).to.not.equal(resultB);
});

t.end();
it('accepts buster function', function () {
var options = {
cachebuster: function (path) {
return path[path.length - 1];
},
loadPaths: ['test/fixtures/alpha/']
};

compareFixtures('cachebuster', options)();
});
});
Loading

0 comments on commit 0d2539a

Please sign in to comment.