@@ -5,9 +5,12 @@ import path from 'path';
55import MemoryFileSystem from 'memory-fs' ;
66import { expect } from 'chai' ;
77
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 ) => {
912 return {
10- entry : './index.js' ,
13+ entry : path . join ( __dirname , '..' , '..' , 'example' , entry ) ,
1114 context : path . join ( __dirname , '..' , '..' , 'example' ) ,
1215 output : {
1316 path : path . join ( __dirname , 'dist' ) ,
@@ -17,75 +20,114 @@ const config = (options) => {
1720 module : {
1821 loaders : [ {
1922 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+ ) ,
2132 } ] ,
2233 } ,
34+ devtool : 'source-map' ,
2335 plugins : [
2436 new ExtractTextPlugin ( 'styles.css' ) ,
2537 new CSSSplitWebpackPlugin ( options ) ,
2638 ] ,
39+ ...extra ,
2740 } ;
2841} ;
2942
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 ( ) ;
3347 return new Promise ( ( resolve ) => {
34- compiler . run ( ( err , stats ) => {
48+ compiler . run ( ( err , _stats ) => {
3549 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} ) ;
3758 } ) ;
3859 } ) ;
3960} ;
4061
4162describe ( '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} ) => {
4465 expect ( stats ) . to . not . be . null ;
4566 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
4667 . 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} ) => {
5172 expect ( stats ) . to . not . be . null ;
5273 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
5374 . 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} ) => {
5879 expect ( stats ) . to . not . be . null ;
5980 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
6081 . 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} ) => {
6586 expect ( stats ) . to . not . be . null ;
6687 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
6788 . 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} ) => {
7293 expect ( stats ) . to . not . be . null ;
7394 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
7495 . 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} ) => {
79100 expect ( stats ) . to . not . be . null ;
80101 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
81102 . to . contain ( 'styles.css' ) ;
82- } ) ;
83- } ) ;
103+ } )
104+ ) ;
84105 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} ) => {
86107 expect ( stats ) . to . not . be . null ;
87108 expect ( stats . assetsByChunkName ) . to . have . property ( 'main' )
88109 . to . contain ( 'styles-split.css' ) ;
89110 } ) ;
90111 } ) ;
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+ } ) ;
91133} ) ;
0 commit comments