@@ -5,9 +5,12 @@ import path from 'path';
5
5
import MemoryFileSystem from 'memory-fs' ;
6
6
import { expect } from 'chai' ;
7
7
8
- const config = ( options ) => {
8
+ const basic = path . join ( '.' , 'basic' , 'index.js' ) ;
9
+ const less = path . join ( '.' , 'less' , 'index.js' ) ;
10
+
11
+ const config = ( options , entry = basic , extra ) => {
9
12
return {
10
- entry : './index.js' ,
13
+ entry : path . join ( __dirname , '..' , '..' , 'example' , entry ) ,
11
14
context : path . join ( __dirname , '..' , '..' , 'example' ) ,
12
15
output : {
13
16
path : path . join ( __dirname , 'dist' ) ,
@@ -17,75 +20,114 @@ const config = (options) => {
17
20
module : {
18
21
loaders : [ {
19
22
test : / \. c s s $ / ,
20
- loader : ExtractTextPlugin . extract ( 'style-loader' , 'css-loader' ) ,
23
+ loader : ExtractTextPlugin . extract (
24
+ 'style-loader' ,
25
+ 'css-loader?sourceMap'
26
+ ) ,
27
+ } , {
28
+ test : / \. l e s s $ / ,
29
+ loader : ExtractTextPlugin . extract (
30
+ 'css?-url&-autoprefixer&sourceMap!less?sourceMap'
31
+ ) ,
21
32
} ] ,
22
33
} ,
34
+ devtool : 'source-map' ,
23
35
plugins : [
24
36
new ExtractTextPlugin ( 'styles.css' ) ,
25
37
new CSSSplitWebpackPlugin ( options ) ,
26
38
] ,
39
+ ...extra ,
27
40
} ;
28
41
} ;
29
42
30
- const webpack = ( options ) => {
31
- const compiler = _webpack ( config ( options ) ) ;
32
- compiler . fs = new MemoryFileSystem ( ) ;
43
+ const webpack = ( options , inst , extra ) => {
44
+ const configuration = config ( options , inst , extra ) ;
45
+ const compiler = _webpack ( configuration ) ;
46
+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
33
47
return new Promise ( ( resolve ) => {
34
- compiler . run ( ( err , stats ) => {
48
+ compiler . run ( ( err , _stats ) => {
35
49
expect ( err ) . to . be . null ;
36
- resolve ( stats . toJson ( ) ) ;
50
+ const stats = _stats . toJson ( ) ;
51
+ const files = { } ;
52
+ stats . assets . forEach ( ( asset ) => {
53
+ files [ asset . name ] = compiler . outputFileSystem . readFileSync (
54
+ path . join ( configuration . output . path , asset . name )
55
+ ) ;
56
+ } ) ;
57
+ resolve ( { stats, files} ) ;
37
58
} ) ;
38
59
} ) ;
39
60
} ;
40
61
41
62
describe ( 'CSSSplitWebpackPlugin' , ( ) => {
42
- it ( 'should split files when needed' , ( ) => {
43
- return webpack ( { size : 3 , imports : true } ) . then ( ( stats ) => {
63
+ it ( 'should split files when needed' , ( ) =>
64
+ webpack ( { size : 3 , imports : true } ) . then ( ( { stats} ) => {
44
65
expect ( stats ) . to . not . be . null ;
45
66
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
46
67
. to . contain ( 'styles-2.css' ) ;
47
- } ) ;
48
- } ) ;
49
- it ( 'should ignore files that do not need splitting' , ( ) => {
50
- return webpack ( { size : 10 , imports : true } ) . then ( ( stats ) => {
68
+ } )
69
+ ) ;
70
+ it ( 'should ignore files that do not need splitting' , ( ) =>
71
+ webpack ( { size : 10 , imports : true } ) . then ( ( { stats} ) => {
51
72
expect ( stats ) . to . not . be . null ;
52
73
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
53
74
. to . not . contain ( 'styles-2.css' ) ;
54
- } ) ;
55
- } ) ;
56
- it ( 'should generate an import file when requested' , ( ) => {
57
- return webpack ( { size : 3 , imports : true } ) . then ( ( stats ) => {
75
+ } )
76
+ ) ;
77
+ it ( 'should generate an import file when requested' , ( ) =>
78
+ webpack ( { size : 3 , imports : true } ) . then ( ( { stats} ) => {
58
79
expect ( stats ) . to . not . be . null ;
59
80
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
60
81
. to . contain ( 'styles.css' ) ;
61
- } ) ;
62
- } ) ;
63
- it ( 'should remove the original asset when splitting' , ( ) => {
64
- return webpack ( { size : 3 , imports : false } ) . then ( ( stats ) => {
82
+ } )
83
+ ) ;
84
+ it ( 'should remove the original asset when splitting' , ( ) =>
85
+ webpack ( { size : 3 , imports : false } ) . then ( ( { stats} ) => {
65
86
expect ( stats ) . to . not . be . null ;
66
87
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
67
88
. to . not . contain ( 'styles.css' ) ;
68
- } ) ;
69
- } ) ;
70
- it ( 'should allow customization of import name' , ( ) => {
71
- return webpack ( { size : 3 , imports : 'potato.css' } ) . then ( ( stats ) => {
89
+ } )
90
+ ) ;
91
+ it ( 'should allow customization of import name' , ( ) =>
92
+ webpack ( { size : 3 , imports : 'potato.css' } ) . then ( ( { stats} ) => {
72
93
expect ( stats ) . to . not . be . null ;
73
94
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
74
95
. to . contain ( 'potato.css' ) ;
75
- } ) ;
76
- } ) ;
77
- it ( 'should allow preservation of the original unsplit file' , ( ) => {
78
- return webpack ( { size : 3 , imports : false , preserve : true } ) . then ( ( stats ) => {
96
+ } )
97
+ ) ;
98
+ it ( 'should allow preservation of the original unsplit file' , ( ) =>
99
+ webpack ( { size : 3 , imports : false , preserve : true } ) . then ( ( { stats} ) => {
79
100
expect ( stats ) . to . not . be . null ;
80
101
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
81
102
. to . contain ( 'styles.css' ) ;
82
- } ) ;
83
- } ) ;
103
+ } )
104
+ ) ;
84
105
it ( 'should give sensible names by default' , ( ) => {
85
- return webpack ( { size : 3 , imports : true , preserve : true } ) . then ( ( stats ) => {
106
+ return webpack ( { size : 3 , imports : true , preserve : true } ) . then ( ( { stats} ) => {
86
107
expect ( stats ) . to . not . be . null ;
87
108
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
88
109
. to . contain ( 'styles-split.css' ) ;
89
110
} ) ;
90
111
} ) ;
112
+ it ( 'should handle source maps properly' , ( ) =>
113
+ webpack ( { size : 3 } , less ) . then ( ( { files} ) => {
114
+ expect ( files ) . to . have . property ( 'styles-1.css.map' ) ;
115
+ const map = JSON . parse ( files [ 'styles-1.css.map' ] . toString ( 'utf8' ) ) ;
116
+ expect ( map ) . to . have . property ( 'version' , 3 ) ;
117
+ expect ( map ) . to . have . property ( 'sources' )
118
+ . to . have . property ( 0 )
119
+ . to . match ( / i n d e x .l e s s $ / ) ;
120
+ } )
121
+ ) ;
122
+ it ( 'should handle cases when there are no source maps' , ( ) =>
123
+ webpack ( { size : 3 } , less , { devtool : null } ) . then ( ( { files} ) => {
124
+ expect ( files ) . to . not . have . property ( 'styles-1.css.map' ) ;
125
+ } )
126
+ ) ;
127
+
128
+ it ( 'should fail with bad imports' , ( ) => {
129
+ expect ( ( ) =>
130
+ new CSSSplitWebpackPlugin ( { imports : ( ) => { } } )
131
+ ) . to . throw ( TypeError ) ;
132
+ } ) ;
91
133
} ) ;
0 commit comments