@@ -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 ) => {
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' ) ,
@@ -18,8 +21,14 @@ const config = (options) => {
18
21
loaders : [ {
19
22
test : / \. c s s $ / ,
20
23
loader : ExtractTextPlugin . extract ( 'style-loader' , 'css-loader' ) ,
24
+ } , {
25
+ test : / \. l e s s $ / ,
26
+ loader : ExtractTextPlugin . extract (
27
+ 'css?-url&-autoprefixer&sourceMap!less?sourceMap'
28
+ ) ,
21
29
} ] ,
22
30
} ,
31
+ devtool : 'source-map' ,
23
32
plugins : [
24
33
new ExtractTextPlugin ( 'styles.css' ) ,
25
34
new CSSSplitWebpackPlugin ( options ) ,
@@ -28,64 +37,79 @@ const config = (options) => {
28
37
} ;
29
38
30
39
const webpack = ( options ) => {
31
- const compiler = _webpack ( config ( options ) ) ;
32
- compiler . fs = new MemoryFileSystem ( ) ;
40
+ const configuration = config ( options ) ;
41
+ const compiler = _webpack ( configuration ) ;
42
+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
33
43
return new Promise ( ( resolve ) => {
34
- compiler . run ( ( err , stats ) => {
44
+ compiler . run ( ( err , _stats ) => {
35
45
expect ( err ) . to . be . null ;
36
- resolve ( stats . toJson ( ) ) ;
46
+ const stats = _stats . toJson ( ) ;
47
+ const files = { } ;
48
+ stats . assets . forEach ( ( asset ) => {
49
+ files [ asset . name ] = compiler . outputFileSystem . readFileSync (
50
+ path . join ( configuration . output . path , asset . name )
51
+ ) ;
52
+ } ) ;
53
+ resolve ( { stats, files} ) ;
37
54
} ) ;
38
55
} ) ;
39
56
} ;
40
57
41
58
describe ( 'CSSSplitWebpackPlugin' , ( ) => {
42
- it ( 'should split files when needed' , ( ) => {
43
- return webpack ( { size : 3 , imports : true } ) . then ( ( stats ) => {
59
+ it ( 'should split files when needed' , ( ) =>
60
+ webpack ( { size : 3 , imports : true } ) . then ( ( { stats} ) => {
44
61
expect ( stats ) . to . not . be . null ;
45
62
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
46
63
. 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 ) => {
64
+ } )
65
+ ) ;
66
+ it ( 'should ignore files that do not need splitting' , ( ) =>
67
+ webpack ( { size : 10 , imports : true } ) . then ( ( { stats} ) => {
51
68
expect ( stats ) . to . not . be . null ;
52
69
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
53
70
. 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 ) => {
71
+ } )
72
+ ) ;
73
+ it ( 'should generate an import file when requested' , ( ) =>
74
+ webpack ( { size : 3 , imports : true } ) . then ( ( { stats} ) => {
58
75
expect ( stats ) . to . not . be . null ;
59
76
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
60
77
. to . contain ( 'styles.css' ) ;
61
- } ) ;
62
- } ) ;
63
- it ( 'should remove the original asset when splitting' , ( ) => {
64
- return webpack ( { size : 3 , imports : false } ) . then ( ( stats ) => {
78
+ } )
79
+ ) ;
80
+ it ( 'should remove the original asset when splitting' , ( ) =>
81
+ webpack ( { size : 3 , imports : false } ) . then ( ( { stats} ) => {
65
82
expect ( stats ) . to . not . be . null ;
66
83
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
67
84
. 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 ) => {
85
+ } )
86
+ ) ;
87
+ it ( 'should allow customization of import name' , ( ) =>
88
+ webpack ( { size : 3 , imports : 'potato.css' } ) . then ( ( { stats} ) => {
72
89
expect ( stats ) . to . not . be . null ;
73
90
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
74
91
. 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 ) => {
92
+ } )
93
+ ) ;
94
+ it ( 'should allow preservation of the original unsplit file' , ( ) =>
95
+ webpack ( { size : 3 , imports : false , preserve : true } ) . then ( ( { stats} ) => {
79
96
expect ( stats ) . to . not . be . null ;
80
97
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
81
98
. to . contain ( 'styles.css' ) ;
82
- } ) ;
83
- } ) ;
99
+ } )
100
+ ) ;
84
101
it ( 'should give sensible names by default' , ( ) => {
85
- return webpack ( { size : 3 , imports : true , preserve : true } ) . then ( ( stats ) => {
102
+ return webpack ( { size : 3 , imports : true , preserve : true } ) . then ( ( { stats} ) => {
86
103
expect ( stats ) . to . not . be . null ;
87
104
expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
88
105
. to . contain ( 'styles-split.css' ) ;
89
106
} ) ;
90
107
} ) ;
108
+ it ( 'should handle source maps properly' , ( ) =>
109
+ webpack ( { size : 3 } , less ) . then ( ( { files} ) => {
110
+ expect ( files ) . to . have . property ( 'styles-1.css.map' ) ;
111
+ const map = JSON . parse ( files [ 'styles-1.css.map' ] . toString ( 'utf8' ) ) ;
112
+ expect ( map ) . to . have . property ( 'version' , 3 ) ;
113
+ } )
114
+ ) ;
91
115
} ) ;
0 commit comments