11import fs from 'fs' ;
22import path from 'path' ;
33import webpack from 'webpack' ;
4+ import sources from 'webpack-sources' ;
5+
6+ const { RawSource, ConcatSource } = sources ;
47
58const NS = path . dirname ( fs . realpathSync ( __filename ) ) ;
69
7- class CssDependency {
8- constructor ( { identifier, content, media, sourceMap } ) {
10+ class CssDependency extends webpack . Dependency {
11+ constructor ( { identifier, content, media, sourceMap } , context ) {
12+ super ( ) ;
913 this . identifier = identifier ;
1014 this . content = content ;
1115 this . media = media ;
1216 this . sourceMap = sourceMap ;
17+ this . context = context ;
1318 }
1419
1520 getResourceIdentifier ( ) {
@@ -23,16 +28,35 @@ class CssDependencyTemplate {
2328
2429class CssModule extends webpack . Module {
2530 constructor ( dependency ) {
26- super ( NS ) ;
31+ super ( NS , dependency . context ) ;
2732 this . _identifier = dependency . identifier ;
2833 this . content = dependency . content ;
2934 this . media = dependency . media ;
3035 this . sourceMap = dependency . sourceMap ;
3136 }
3237
38+ source ( ) {
39+ // TODO add webpack support for omitting js source
40+ return new RawSource ( '// extracted by mini-css-extract-plugin' ) ;
41+ }
42+
43+ size ( ) {
44+ return 0 ;
45+ }
46+
3347 identifier ( ) {
3448 return `css-module ${ this . _identifier } ` ;
3549 }
50+
51+ readableIdentifier ( requestShortener ) {
52+ return `css-modules ${ requestShortener . shorten ( this . _identifier ) } ` ;
53+ }
54+
55+ build ( options , compilation , resolver , fs , callback ) {
56+ this . buildInfo = { } ;
57+ this . buildMeta = { } ;
58+ callback ( ) ;
59+ }
3660}
3761
3862class CssModuleFactory {
@@ -42,6 +66,16 @@ class CssModuleFactory {
4266}
4367
4468class MiniCssExtractPlugin {
69+ constructor ( options ) {
70+ this . options = Object . assign ( {
71+ filename : '[name].css' ,
72+ } , options ) ;
73+ if ( ! this . options . chunkFilename ) {
74+ // TODO use webpack conversion style here
75+ this . options . chunkFilename = this . options . filename ;
76+ }
77+ }
78+
4579 apply ( compiler ) {
4680 compiler . hooks . thisCompilation . tap ( 'mini-css-extract-plugin' , ( compilation ) => {
4781 compilation . hooks . normalModuleLoader . tap ( 'mini-css-extract-plugin' , ( lc , m ) => {
@@ -52,38 +86,44 @@ class MiniCssExtractPlugin {
5286 throw new Error ( `Exported value was not extracted as an array: ${ JSON . stringify ( content ) } ` ) ;
5387 }
5488 for ( const line of content ) {
55- module . addDependency ( new CssDependency ( line ) ) ;
89+ module . addDependency ( new CssDependency ( line , m . context ) ) ;
5690 }
5791 } ;
5892 } ) ;
5993 compilation . dependencyFactories . set ( CssDependency , new CssModuleFactory ( ) ) ;
6094 compilation . dependencyTemplates . set ( CssDependency , new CssDependencyTemplate ( ) ) ;
61- compilation . mainTemplate . hooks . shouldRenderModuleForJavascript . tap ( 'mini-css-extract-plugin' , ( module ) => {
62- if ( module . type === NS ) return false ;
63- return undefined ;
64- } ) ;
65- compilation . chunkTemplate . hooks . shouldRenderModuleForJavascript . tap ( 'mini-css-extract-plugin' , ( module ) => {
66- if ( module . type === NS ) return false ;
67- return undefined ;
68- } ) ;
69- compilation . mainTemplate . hooks . renderManifest . tap ( 'mini-css-extract-plugin' , ( result , options ) => {
70- const chunk = options . chunk ;
71- const renderedContent = Array . from ( chunk . modulesIterable , m => module [ NS ] ) . filter ( Boolean ) ;
72- if ( renderedContent . length > 0 ) {
95+ compilation . mainTemplate . hooks . renderManifest . tap ( 'mini-css-extract-plugin' , ( result , { chunk } ) => {
96+ const renderedModules = Array . from ( chunk . modulesIterable ) . filter ( module => module . type === NS ) ;
97+ if ( renderedModules . length > 0 ) {
7398 result . push ( {
74- render : ( ) => this . renderContentAsset ( renderedContent ) ,
75- filenameTemplate : filename ,
99+ render : ( ) => this . renderContentAsset ( renderedModules ) ,
100+ filenameTemplate : this . options . filename ,
76101 pathOptions : {
77- chunk
102+ chunk,
78103 } ,
79- identifier : `extract-text-webpack-plugin.${ id } . ${ chunk . id } `
104+ identifier : `extract-text-webpack-plugin.${ chunk . id } ` ,
80105 } ) ;
81106 }
82107 } ) ;
83108 } ) ;
84109 }
110+ renderContentAsset ( modules ) {
111+ modules . sort ( ( a , b ) => a . index2 - b . index2 ) ;
112+ // TODO put @import on top
113+ const source = new ConcatSource ( ) ;
114+ for ( const m of modules ) {
115+ source . add ( m . content ) ;
116+ source . add ( '\n' ) ;
117+ if ( m . media ) {
118+ source . prepend ( `@media ${ m . media } {\n` ) ;
119+ source . add ( '}\n' ) ;
120+ }
121+ return source ;
122+ }
123+ return source ;
124+ }
85125}
86126
87127MiniCssExtractPlugin . loader = require . resolve ( './loader' ) ;
88128
89- module . exports = MiniCssExtractPlugin ;
129+ export default MiniCssExtractPlugin ;
0 commit comments