88const { ConcatSource } = require ( "webpack-sources" ) ;
99const HotUpdateChunk = require ( "../HotUpdateChunk" ) ;
1010const RuntimeGlobals = require ( "../RuntimeGlobals" ) ;
11+ const CssExportDependency = require ( "../dependencies/CssExportDependency" ) ;
1112const CssImportDependency = require ( "../dependencies/CssImportDependency" ) ;
1213const CssUrlDependency = require ( "../dependencies/CssUrlDependency" ) ;
1314const StaticExportsDependency = require ( "../dependencies/StaticExportsDependency" ) ;
@@ -85,6 +86,10 @@ class CssModulesPlugin {
8586 CssUrlDependency ,
8687 new CssUrlDependency . Template ( )
8788 ) ;
89+ compilation . dependencyTemplates . set (
90+ CssExportDependency ,
91+ new CssExportDependency . Template ( )
92+ ) ;
8893 compilation . dependencyFactories . set (
8994 CssImportDependency ,
9095 normalModuleFactory
@@ -103,12 +108,36 @@ class CssModulesPlugin {
103108 validateParserOptions ( parserOptions ) ;
104109 return new CssParser ( ) ;
105110 } ) ;
111+ normalModuleFactory . hooks . createParser
112+ . for ( "css/global" )
113+ . tap ( plugin , parserOptions => {
114+ validateParserOptions ( parserOptions ) ;
115+ return new CssParser ( { allowPseudoBlocks : false } ) ;
116+ } ) ;
117+ normalModuleFactory . hooks . createParser
118+ . for ( "css/module" )
119+ . tap ( plugin , parserOptions => {
120+ validateParserOptions ( parserOptions ) ;
121+ return new CssParser ( { allowPseudoBlocks : true } ) ;
122+ } ) ;
106123 normalModuleFactory . hooks . createGenerator
107124 . for ( "css" )
108125 . tap ( plugin , generatorOptions => {
109126 validateGeneratorOptions ( generatorOptions ) ;
110127 return new CssGenerator ( ) ;
111128 } ) ;
129+ normalModuleFactory . hooks . createGenerator
130+ . for ( "css/global" )
131+ . tap ( plugin , generatorOptions => {
132+ validateGeneratorOptions ( generatorOptions ) ;
133+ return new CssGenerator ( ) ;
134+ } ) ;
135+ normalModuleFactory . hooks . createGenerator
136+ . for ( "css/module" )
137+ . tap ( plugin , generatorOptions => {
138+ validateGeneratorOptions ( generatorOptions ) ;
139+ return new CssGenerator ( ) ;
140+ } ) ;
112141 compilation . hooks . contentHash . tap ( "JavascriptModulesPlugin" , chunk => {
113142 const {
114143 chunkGraph,
@@ -204,25 +233,43 @@ class CssModulesPlugin {
204233 renderChunk ( { chunk, chunkGraph, codeGenerationResults } ) {
205234 const modules = this . getOrderedChunkCssModules ( chunk , chunkGraph ) ;
206235 const source = new ConcatSource ( ) ;
236+ const metaData = [ ] ;
207237 for ( const module of modules ) {
208238 try {
239+ const codeGenResult = codeGenerationResults . get ( module , chunk . runtime ) ;
240+
209241 const s =
210- codeGenerationResults . getSource ( module , chunk . runtime , "css" ) ||
211- codeGenerationResults . getSource ( module , chunk . runtime , "css-import" ) ;
242+ codeGenResult . sources . get ( "css" ) ||
243+ codeGenResult . sources . get ( "css-import" ) ;
212244 if ( s ) {
213245 source . add ( s ) ;
214246 source . add ( "\n" ) ;
215247 }
248+ const exports =
249+ codeGenResult . data && codeGenResult . data . get ( "css-exports" ) ;
250+ metaData . push (
251+ `${
252+ exports
253+ ? Array . from (
254+ exports ,
255+ ( [ n , v ] ) =>
256+ `${ escapeCssIdentifierPart (
257+ n ,
258+ true
259+ ) } (${ escapeCssIdentifierPart ( v , true ) } )`
260+ ) . join ( "" )
261+ : ""
262+ } ${ escapeCssIdentifierPart ( chunkGraph . getModuleId ( module ) , true ) } `
263+ ) ;
216264 } catch ( e ) {
217265 e . message += `\nduring rendering of css ${ module . identifier ( ) } ` ;
218266 throw e ;
219267 }
220268 }
221269 source . add (
222- `head{--webpack-${ escapeCssIdentifierPart ( chunk . id ) } :${ Array . from (
223- modules ,
224- m => `${ escapeCssIdentifierPart ( chunkGraph . getModuleId ( m ) , true ) } `
225- ) . join ( "," ) } ;}`
270+ `head{--webpack-${ escapeCssIdentifierPart ( chunk . id ) } :${ metaData . join (
271+ ","
272+ ) } ;}`
226273 ) ;
227274 return source ;
228275 }
0 commit comments