@@ -9,17 +9,18 @@ const { Template } = webpack;
9
9
const NS = path . dirname ( fs . realpathSync ( __filename ) ) ;
10
10
11
11
class CssDependency extends webpack . Dependency {
12
- constructor ( { identifier, content, media, sourceMap } , context ) {
12
+ constructor ( { identifier, content, media, sourceMap } , context , identifierIndex ) {
13
13
super ( ) ;
14
14
this . identifier = identifier ;
15
+ this . identifierIndex = identifierIndex ;
15
16
this . content = content ;
16
17
this . media = media ;
17
18
this . sourceMap = sourceMap ;
18
19
this . context = context ;
19
20
}
20
21
21
22
getResourceIdentifier ( ) {
22
- return `css-module-${ this . identifier } ` ;
23
+ return `css-module-${ this . identifier } - ${ this . identifierIndex } ` ;
23
24
}
24
25
}
25
26
@@ -31,6 +32,7 @@ class CssModule extends webpack.Module {
31
32
constructor ( dependency ) {
32
33
super ( NS , dependency . context ) ;
33
34
this . _identifier = dependency . identifier ;
35
+ this . _identifierIndex = dependency . identifierIndex ;
34
36
this . content = dependency . content ;
35
37
this . media = dependency . media ;
36
38
this . sourceMap = dependency . sourceMap ;
@@ -46,11 +48,11 @@ class CssModule extends webpack.Module {
46
48
}
47
49
48
50
identifier ( ) {
49
- return `css ${ this . _identifier } ` ;
51
+ return `css ${ this . _identifier } ${ this . _identifierIndex } ` ;
50
52
}
51
53
52
54
readableIdentifier ( requestShortener ) {
53
- return `css ${ requestShortener . shorten ( this . _identifier ) } ` ;
55
+ return `css ${ requestShortener . shorten ( this . _identifier ) } ${ this . _identifierIndex ? ` ( ${ this . _identifierIndex } )` : '' } ` ;
54
56
}
55
57
56
58
build ( options , compilation , resolver , fileSystem , callback ) {
@@ -86,8 +88,11 @@ class MiniCssExtractPlugin {
86
88
if ( ! Array . isArray ( content ) && content != null ) {
87
89
throw new Error ( `Exported value was not extracted as an array: ${ JSON . stringify ( content ) } ` ) ;
88
90
}
91
+ const identifierCountMap = new Map ( ) ;
89
92
for ( const line of content ) {
90
- module . addDependency ( new CssDependency ( line , m . context ) ) ;
93
+ const count = identifierCountMap . get ( line . identifier ) || 0 ;
94
+ module . addDependency ( new CssDependency ( line , m . context , count ) ) ;
95
+ identifierCountMap . set ( line . identifier , count + 1 ) ;
91
96
}
92
97
} ;
93
98
} ) ;
@@ -190,19 +195,33 @@ class MiniCssExtractPlugin {
190
195
191
196
renderContentAsset ( modules ) {
192
197
modules . sort ( ( a , b ) => a . index2 - b . index2 ) ;
193
- // TODO put @import on top
194
198
const source = new ConcatSource ( ) ;
199
+ const externalsSource = new ConcatSource ( ) ;
195
200
for ( const m of modules ) {
196
- if ( m . media ) {
197
- source . add ( `@media ${ m . media } {\n` ) ;
198
- }
199
- source . add ( m . content ) ;
200
- source . add ( '\n' ) ;
201
- if ( m . media ) {
202
- source . add ( '}\n' ) ;
201
+ if ( / ^ @ i m p o r t u r l / . test ( m . content ) ) {
202
+ // HACK for IE
203
+ // http://stackoverflow.com/a/14676665/1458162
204
+ let { content } = m ;
205
+ if ( m . media ) {
206
+ // insert media into the @import
207
+ // this is rar
208
+ // TODO improve this and parse the CSS to support multiple medias
209
+ content = content . replace ( / ; | \s * $ / , m . media ) ;
210
+ }
211
+ externalsSource . add ( content ) ;
212
+ externalsSource . add ( '\n' ) ;
213
+ } else {
214
+ if ( m . media ) {
215
+ source . add ( `@media ${ m . media } {\n` ) ;
216
+ }
217
+ source . add ( m . content ) ;
218
+ source . add ( '\n' ) ;
219
+ if ( m . media ) {
220
+ source . add ( '}\n' ) ;
221
+ }
203
222
}
204
223
}
205
- return source ;
224
+ return new ConcatSource ( externalsSource , source ) ;
206
225
}
207
226
}
208
227
0 commit comments