1
1
var should = require ( "should" ) ;
2
2
var path = require ( "path" ) ;
3
3
var cssLoader = require ( "../index.js" ) ;
4
+ var vm = require ( "vm" ) ;
4
5
5
- function test ( name , input , result , query ) {
6
+ function test ( name , input , result , query , modules ) {
6
7
it ( name , function ( ) {
7
- var output ;
8
- cssLoader . call ( {
8
+ var output = cssLoader . call ( {
9
9
loaders : [ { request : "loader" } ] ,
10
10
loaderIndex : 0 ,
11
11
resource : "test.css" ,
12
- query : query ,
13
- callback : function ( err , result ) {
14
- output = result ;
15
- }
12
+ query : query
16
13
} , input ) ;
17
- output . should . be . eql ( "module.exports =\n\t" + result . join ( " +\n\t" ) + ";" ) ;
14
+ assetEvaluated ( output , result , modules ) ;
18
15
} ) ;
19
16
}
20
17
21
- function testMinimize ( name , input , result ) {
18
+ function testMinimize ( name , input , result , query , modules ) {
22
19
it ( name , function ( ) {
23
- var output ;
24
- cssLoader . call ( {
20
+ var output = cssLoader . call ( {
25
21
loaders : [ { request : "loader" } ] ,
26
22
loaderIndex : 0 ,
27
23
resource : "test.css" ,
28
24
minimize : true ,
29
- callback : function ( err , result ) {
30
- output = result ;
31
- }
25
+ query : query
32
26
} , input ) ;
33
- output . should . be . eql ( "module.exports =\n\t" + result . join ( " +\n\t" ) + ";" ) ;
27
+ assetEvaluated ( output , result , modules ) ;
34
28
} ) ;
35
29
}
36
30
31
+ function assetEvaluated ( output , result , modules ) {
32
+ try {
33
+ var fn = vm . runInThisContext ( "(function(module, exports, require) {" + output + "})" , "testcase.js" ) ;
34
+ var m = { exports : { } , id : 1 } ;
35
+ fn ( m , m . exports , function ( module ) {
36
+ if ( module === require . resolve ( "../mergeImport" ) )
37
+ return require ( "../mergeImport" ) ;
38
+ if ( module === require . resolve ( "../cssToString" ) )
39
+ return require ( "../cssToString" ) ;
40
+ if ( module . indexOf ( "-!loader!" ) === 0 )
41
+ module = module . substr ( 9 ) ;
42
+ if ( modules && modules [ module ] )
43
+ return modules [ module ] ;
44
+ return "{" + module + "}" ;
45
+ } ) ;
46
+ } catch ( e ) {
47
+ console . error ( output ) ;
48
+ throw e ;
49
+ }
50
+ delete m . exports . toString ;
51
+ m . exports . should . be . eql ( result ) ;
52
+
53
+ }
54
+
37
55
describe ( "url" , function ( ) {
38
- test ( "empty" , "" ,
39
- [ "\"\"" ] ) ;
40
- testMinimize ( "empty minimized" , "" ,
41
- [ "\"\"" ] ) ;
42
- test ( "simple" , ".class { a: b c d; }" ,
43
- [ "\".class { a: b c d; }\"" ] ) ;
44
- test ( "simple2" , ".class { a: b c d; }\n.two {}" ,
45
- [ "\".class { a: b c d; }\\n.two {}\"" ] ) ;
46
- test ( "import" , "@import url(test.css);\n.class { a: b c d; }" ,
47
- [ "require(" + JSON . stringify ( "!" + path . join ( __dirname , ".." , "index.js" ) + "!./test.css" ) + ")" ,
48
- "\"\\n.class { a: b c d; }\"" ] ) ;
49
- test ( "import 2" , "@import url('test.css');" ,
50
- [ "require(" + JSON . stringify ( "!" + path . join ( __dirname , ".." , "index.js" ) + "!./test.css" ) + ")" ,
51
- "\"\"" ] ) ;
52
- test ( "import with media" , "@import url(~test/css) screen and print;\n.class { a: b c d; }" ,
53
- [ "\"@media screen and print{\"" ,
54
- "require(" + JSON . stringify ( "!" + path . join ( __dirname , ".." , "index.js" ) + "!test/css" ) + ")" ,
55
- "\"}\"" ,
56
- "\"\\n.class { a: b c d; }\"" ] ) ;
57
- test ( "import external" , "@import url(http://example.com/style.css);\n@import url(\"//example.com/style.css\");" ,
58
- [ "\"@import url(http://example.com/style.css);\"" ,
59
- "\"@import url(//example.com/style.css);\"" ,
60
- "\"\\n\"" ] ) ;
61
- test ( "background img" , ".class { background: green url( \"img.png\" ) xyz }" ,
62
- [ "\".class { background: green url( \"+require(\"./img.png\")+\" ) xyz }\"" ] ) ;
63
- test ( "background img 2" , ".class { background: green url(~img/png ) url(aaa) xyz }" ,
64
- [ "\".class { background: green url(\"+require(\"img/png\")+\" ) url(\"+require(\"./aaa\")+\") xyz }\"" ] ) ;
65
- test ( "background img 3" , ".class { background: green url( 'img.png' ) xyz }" ,
66
- [ "\".class { background: green url( \"+require(\"./img.png\")+\" ) xyz }\"" ] ) ;
67
- test ( "background img absolute" , ".class { background: green url(/img.png) xyz }" ,
68
- [ "\".class { background: green url(/img.png) xyz }\"" ] ) ;
69
- test ( "background img absolute with root" , ".class { background: green url(/img.png) xyz }" ,
70
- [ "\".class { background: green url(\"+require(\"./img.png\")+\") xyz }\"" ] , "?root=." ) ;
56
+ test ( "empty" , "" , [
57
+ [ 1 , "" , "" ]
58
+ ] ) ;
59
+ testMinimize ( "empty minimized" , "" , [
60
+ [ 1 , "" , "" ]
61
+ ] ) ;
62
+ test ( "simple" , ".class { a: b c d; }" , [
63
+ [ 1 , ".class { a: b c d; }" , "" ]
64
+ ] ) ;
65
+ test ( "simple2" , ".class { a: b c d; }\n.two {}" , [
66
+ [ 1 , ".class { a: b c d; }\n.two {}" , "" ]
67
+ ] ) ;
68
+ test ( "import" , "@import url(test.css);\n.class { a: b c d; }" , [
69
+ [ 2 , ".test{a: b}" , "" ] ,
70
+ [ 1 , "\n.class { a: b c d; }" , "" ]
71
+ ] , "" , {
72
+ "./test.css" : [ [ 2 , ".test{a: b}" , "" ] ]
73
+ } ) ;
74
+ test ( "import 2" , "@import url('test.css');\n.class { a: b c d; }" , [
75
+ [ 2 , ".test{a: b}" , "screen" ] ,
76
+ [ 1 , "\n.class { a: b c d; }" , "" ]
77
+ ] , "" , {
78
+ "./test.css" : [ [ 2 , ".test{a: b}" , "screen" ] ]
79
+ } ) ;
80
+ test ( "import with media" , "@import url('~test/css') screen and print;\n.class { a: b c d; }" , [
81
+ [ 3 , ".test{a: b}" , "((min-width: 100px)) and (screen and print)" ] ,
82
+ [ 2 , ".test{c: d}" , "screen and print" ] ,
83
+ [ 1 , "\n.class { a: b c d; }" , "" ]
84
+ ] , "" , {
85
+ "test/css" : [
86
+ [ 3 , ".test{a: b}" , "(min-width: 100px)" ] ,
87
+ [ 2 , ".test{c: d}" , "" ]
88
+ ]
89
+ } ) ;
90
+ test ( "import external" , "@import url(http://example.com/style.css);\n@import url(\"//example.com/style.css\");" , [
91
+ [ 1 , "@import url(http://example.com/style.css);" , "" ] ,
92
+ [ 1 , "@import url(//example.com/style.css);" , "" ] ,
93
+ [ 1 , "\n" , "" ]
94
+ ] ) ;
95
+ test ( "background img" , ".class { background: green url( \"img.png\" ) xyz }" , [
96
+ [ 1 , ".class { background: green url( {./img.png} ) xyz }" , "" ]
97
+ ] ) ;
98
+ test ( "background img 2" , ".class { background: green url(~img/png) url(aaa) xyz }" , [
99
+ [ 1 , ".class { background: green url({img/png}) url({./aaa}) xyz }" , "" ]
100
+ ] ) ;
101
+ test ( "background img 3" , ".class { background: green url( 'img.png' ) xyz }" , [
102
+ [ 1 , ".class { background: green url( {./img.png} ) xyz }" , "" ]
103
+ ] ) ;
104
+ test ( "background img absolute" , ".class { background: green url(/img.png) xyz }" , [
105
+ [ 1 , ".class { background: green url(/img.png) xyz }" , "" ]
106
+ ] ) ;
107
+ test ( "background img absolute with root" , ".class { background: green url(/img.png) xyz }" , [
108
+ [ 1 , ".class { background: green url({./img.png}) xyz }" , "" ]
109
+ ] , "?root=." ) ;
71
110
test ( "background img external" ,
72
- ".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }" ,
73
- [ "\".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }\"" ] ) ;
111
+ ".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }" , [
112
+ [ 1 , ".class { background: green url(data:image/png;base64,AAA) url(http://example.com/image.jpg) url(//example.com/image.png) xyz }" , "" ]
113
+ ] ) ;
74
114
test ( "background img external data" ,
75
- ".class { background-image: url(\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\") }" ,
76
- [ "\".class { background-image: url(\\\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\\\") }\"" ] ) ;
115
+ ".class { background-image: url(\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\") }" , [
116
+ [ 1 , ".class { background-image: url(\"data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 42 26' fill='%23007aff'><rect width='4' height='4'/><rect x='8' y='1' width='34' height='2'/><rect y='11' width='4' height='4'/><rect x='8' y='12' width='34' height='2'/><rect y='22' width='4' height='4'/><rect x='8' y='23' width='34' height='2'/></svg>\") }" , "" ]
117
+ ] ) ;
77
118
test ( "filter hash" ,
78
- ".highlight { filter: url(#highlight); }" ,
79
- [ "\".highlight { filter: url(#highlight); }\"" ] ) ;
80
- test ( "font face" , "@font-face { src: url(regular.woff) format('woff'), url(~truetype/regular.ttf) format('truetype') }" ,
81
- [ "\"@font-face { src: url(\"+require(\"./regular.woff\")+\") format('woff'), url(\"+require(\"truetype/regular.ttf\")+\") format('truetype') }\"" ] ) ;
82
- test ( "media query" , "@media (min-width: 500px) { body { background: url(image.png); } }" ,
83
- [ "\"@media (min-width: 500px) { body { background: url(\"+require(\"./image.png\")+\"); } }\"" ] ) ;
84
- testMinimize ( "minimized simple" , ".class { a: b c d; }" ,
85
- [ "\".class{a:b c d}\"" ] ) ;
119
+ ".highlight { filter: url(#highlight); }" , [
120
+ [ 1 , ".highlight { filter: url(#highlight); }" , "" ]
121
+ ] ) ;
122
+ test ( "font face" , "@font-face { src: url(regular.woff) format('woff'), url(~truetype/regular.ttf) format('truetype') }" , [
123
+ [ 1 , "@font-face { src: url({./regular.woff}) format('woff'), url({truetype/regular.ttf}) format('truetype') }" , "" ]
124
+ ] ) ;
125
+ test ( "media query" , "@media (min-width: 500px) { body { background: url(image.png); } }" , [
126
+ [ 1 , "@media (min-width: 500px) { body { background: url({./image.png}); } }" , "" ]
127
+ ] ) ;
128
+ testMinimize ( "minimized simple" , ".class { a: b c d; }" , [
129
+ [ 1 , ".class{a:b c d}" , "" ]
130
+ ] ) ;
86
131
} ) ;
0 commit comments