Skip to content

Commit ab8b410

Browse files
committed
more usefull, better tests
1 parent ccdf193 commit ab8b410

File tree

5 files changed

+56
-65
lines changed

5 files changed

+56
-65
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- 0.6
4+
- 0.8

index.js

+10-18
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
var csso = require("csso");
66
module.exports = function(content) {
77
this.cacheable && this.cacheable();
8-
var isRequireUrl = !this || !this.options || !this.options.css ||
9-
typeof this.options.css.requireUrl === "string";
10-
var requireUrl = this && this.options && this.options.css &&
11-
this.options.css.requireUrl;
12-
if(typeof requireUrl !== "string") requireUrl = "!file!";
138
var result = [];
149
var tree = csso.parse(content, "stylesheet");
15-
if(this && this.minimize)
10+
if(this && this.minimize) {
1611
tree = csso.compress(tree);
17-
tree = csso.cleanInfo(tree);
12+
tree = csso.cleanInfo(tree);
13+
}
1814

1915
var imports = extractImports(tree);
20-
if(isRequireUrl)
21-
annotateUrls(tree);
16+
annotateUrls(tree);
2217

2318
imports.forEach(function(imp) {
2419
if(imp.media.length > 0) {
@@ -31,15 +26,12 @@ module.exports = function(content) {
3126
});
3227

3328
var css = JSON.stringify(csso.translate(tree));
34-
if(isRequireUrl) {
35-
var uriRegExp = /%CSSURL\[%(.*?)%\]CSSURL%/g;
36-
css = css.replace(uriRegExp, function(str) {
37-
match = /^%CSSURL\[%(.*?)%\]CSSURL%$/.exec(str);
38-
var url = JSON.parse("\"" + match[1] + "\"");
39-
return "\"+require(" + JSON.stringify(requireUrl + urlToRequire(url)) + ")+\"";
40-
});
41-
42-
}
29+
var uriRegExp = /%CSSURL\[%(.*?)%\]CSSURL%/g;
30+
css = css.replace(uriRegExp, function(str) {
31+
var match = /^%CSSURL\[%(.*?)%\]CSSURL%$/.exec(str);
32+
var url = JSON.parse("\"" + match[1] + "\"");
33+
return "\"+require(" + JSON.stringify(urlToRequire(url)) + ")+\"";
34+
});
4335
result.push(css);
4436
return "module.exports =\n\t" + result.join(" +\n\t") + ";";
4537
}

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "css-loader",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"author": "Tobias Koppers @sokra",
55
"description": "css loader module for webpack",
66
"dependencies": {
77
"csso": "1.3.x"
88
},
99
"devDependencies": {
10-
"vows": "0.6.2"
10+
"mocha": "1.8.x",
11+
"should": "1.1.x"
1112
},
1213
"scripts": {
13-
"test": "node node_modules/vows/bin/vows"
14+
"test": "mocha --reporter spec"
1415
},
1516
"licenses": [
1617
{

test/urlTest.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var should = require("should");
2+
var path = require("path");
3+
var cssLoader = require("../index.js");
4+
5+
function test(name, input, result) {
6+
it(name, function() {
7+
var output = cssLoader(input);
8+
output.should.be.eql("module.exports =\n\t" + result.join(" +\n\t") + ";");
9+
});
10+
}
11+
12+
function testMinimize(name, input, result) {
13+
it(name, function() {
14+
var output = cssLoader.call({minimize: true}, input);
15+
output.should.be.eql("module.exports =\n\t" + result.join(" +\n\t") + ";");
16+
});
17+
}
18+
19+
describe("url", function() {
20+
test("simple", ".class { a: b c d; }",
21+
["\".class { a: b c d; }\""]);
22+
test("simple2", ".class { a: b c d; }\n.two {}",
23+
["\".class { a: b c d; }\\n.two {}\""]);
24+
test("import", "@import url(test.css);\n.class { a: b c d; }",
25+
["require("+JSON.stringify("!"+path.join(__dirname, "..", "index.js")+"!./test.css")+")",
26+
"\"\\n.class { a: b c d; }\""]);
27+
test("import with media", "@import url(~test/css) screen and print;\n.class { a: b c d; }",
28+
["\"@media screen and print{\"",
29+
"require("+JSON.stringify("!"+path.join(__dirname, "..", "index.js")+"!test/css")+")",
30+
"\"}\"",
31+
"\"\\n.class { a: b c d; }\""]);
32+
test("background img", ".class { background: green url( \"img.png\" ) xyz }",
33+
["\".class { background: green url( \"+require(\"./img.png\")+\" ) xyz }\""]);
34+
test("background img 2", ".class { background: green url(~img/png ) url(aaa) xyz }",
35+
["\".class { background: green url(\"+require(\"img/png\")+\" ) url(\"+require(\"./aaa\")+\") xyz }\""]);
36+
testMinimize("minimized simple", ".class { a: b c d; }",
37+
["\".class{a:b c d}\""]);
38+
});

test/url_test.js

-44
This file was deleted.

0 commit comments

Comments
 (0)