@@ -44,6 +44,7 @@ const memoize = require("../util/memoize");
4444/** @typedef {import("../CssModule") } CssModule */
4545/** @typedef {import("../Compilation") } Compilation */
4646/** @typedef {import("../Module").RuntimeRequirements } RuntimeRequirements */
47+ /** @typedef {import("../../declarations/WebpackOptions").CssParserExportType } CssParserExportType */
4748
4849const getPropertyName = memoize ( ( ) => require ( "../util/propertyName" ) ) ;
4950const getCssModulesPlugin = memoize ( ( ) => require ( "./CssModulesPlugin" ) ) ;
@@ -83,17 +84,18 @@ class CssGenerator extends Generator {
8384 * Generate JavaScript code that requires and concatenates all CSS imports
8485 * @param {NormalModule } module the module to generate CSS text for
8586 * @param {GenerateContext } generateContext the generate context
86- * @returns {{ expr: string }[] } JavaScript code that concatenates all imported CSS
87+ * @returns {{ expr: string, type: CssParserExportType }[] } JavaScript code that concatenates all imported CSS
8788 */
8889 _generateImportCode ( module , generateContext ) {
8990 const moduleGraph = generateContext . moduleGraph ;
90- /** @type {{ expr: string }[] } */
91+ /** @type {{ expr: string, type: CssParserExportType }[] } */
9192 const parts = [ ] ;
9293
9394 // Iterate through module.dependencies to maintain source order
9495 for ( const dep of module . dependencies ) {
9596 if ( dep instanceof CssImportDependency ) {
96- const depModule = moduleGraph . getModule ( dep ) ;
97+ /** @type {CssModule } */
98+ const depModule = /** @type {CssModule } */ ( moduleGraph . getModule ( dep ) ) ;
9799 const importVar = generateContext . runtimeTemplate . moduleExports ( {
98100 module : depModule ,
99101 chunkGraph : generateContext . chunkGraph ,
@@ -106,7 +108,10 @@ class CssGenerator extends Generator {
106108 RuntimeGlobals . compatGetDefaultExport
107109 ) ;
108110 parts . push ( {
109- expr : `(${ RuntimeGlobals . compatGetDefaultExport } (${ importVar } )() || "")`
111+ expr : `(${ RuntimeGlobals . compatGetDefaultExport } (${ importVar } )() || "")` ,
112+ type : /** @type {CssParserExportType } */ (
113+ /** @type {BuildMeta } */ ( depModule . buildMeta ) . exportType
114+ )
110115 } ) ;
111116 }
112117 }
@@ -308,21 +313,47 @@ class CssGenerator extends Generator {
308313 ...generateContext ,
309314 cssData
310315 } ) ;
316+
317+ const generateCssText = ( ) => {
318+ const importCode = this . _generateImportCode ( module , generateContext ) ;
319+ const moduleCode = this . _generateModuleCode ( module , generateContext ) ;
320+
321+ if ( importCode . length > 0 ) {
322+ if (
323+ exportType === "css-style-sheet" ||
324+ importCode . some ( ( part ) => part . type !== exportType )
325+ ) {
326+ generateContext . runtimeRequirements . add (
327+ RuntimeGlobals . cssMergeStyleSheets
328+ ) ;
329+
330+ return `${ RuntimeGlobals . cssMergeStyleSheets } ([${ [ ...importCode . map ( ( part ) => part . expr ) , JSON . stringify ( moduleCode ) ] . join ( ", " ) } ])` ;
331+ }
332+ return generateContext . runtimeTemplate . concatenation (
333+ ...importCode ,
334+ moduleCode
335+ ) ;
336+ }
337+ return JSON . stringify ( moduleCode ) ;
338+ } ;
339+
311340 /**
312341 * @returns {string | null } the default export
313342 */
314343 const generateJSDefaultExport = ( ) => {
315344 switch ( exportType ) {
316345 case "text" : {
317- const importCode = this . _generateImportCode ( module , generateContext ) ;
318- const moduleCode = this . _generateModuleCode ( module , generateContext ) ;
319-
320- return generateContext . runtimeTemplate . concatenation (
321- ...importCode ,
322- moduleCode
323- ) ;
346+ return generateCssText ( ) ;
347+ }
348+ case "css-style-sheet" : {
349+ const constOrVar = generateContext . runtimeTemplate . renderConst ( ) ;
350+ return `(${ generateContext . runtimeTemplate . basicFunction ( "" , [
351+ `${ constOrVar } cssText = ${ generateCssText ( ) } ;` ,
352+ `${ constOrVar } sheet = new CSSStyleSheet();` ,
353+ "sheet.replaceSync(cssText);" ,
354+ "return sheet;"
355+ ] ) } )()`;
324356 }
325- // case "style-sheet"
326357 default :
327358 return null ;
328359 }
@@ -428,7 +459,9 @@ class CssGenerator extends Generator {
428459 ) ;
429460 }
430461 case "css" : {
431- generateContext . runtimeRequirements . add ( RuntimeGlobals . hasCssModules ) ;
462+ if ( ! this . _generatesJsOnly ( module ) ) {
463+ generateContext . runtimeRequirements . add ( RuntimeGlobals . hasCssModules ) ;
464+ }
432465
433466 return InitFragment . addToSource ( source , initFragments , generateContext ) ;
434467 }
@@ -463,10 +496,7 @@ class CssGenerator extends Generator {
463496 * @returns {SourceTypes } available types (do not mutate)
464497 */
465498 getTypes ( module ) {
466- const exportType = /** @type {BuildMeta } */ ( module . buildMeta ) . exportType ;
467- const onlyJsOutput =
468- this . _exportsOnly || ( exportType && exportType !== "link" ) ;
469- if ( onlyJsOutput ) {
499+ if ( this . _generatesJsOnly ( module ) ) {
470500 return JS_TYPES ;
471501 }
472502 const sourceTypes = new Set ( ) ;
@@ -534,6 +564,18 @@ class CssGenerator extends Generator {
534564 updateHash ( hash , { module } ) {
535565 hash . update ( /** @type {boolean } */ ( this . _esModule ) . toString ( ) ) ;
536566 }
567+
568+ /**
569+ * @param {NormalModule } module module
570+ * @returns {boolean } true if the module only outputs JavaScript
571+ */
572+ _generatesJsOnly ( module ) {
573+ const exportType = /** @type {BuildMeta } */ ( module . buildMeta ) . exportType ;
574+ return (
575+ this . _exportsOnly ||
576+ /** @type {boolean } */ ( exportType && exportType !== "link" )
577+ ) ;
578+ }
537579}
538580
539581module . exports = CssGenerator ;
0 commit comments