@@ -8360,6 +8360,12 @@ const jsenvPluginImportMetaCss = () => {
83608360 return null ;
83618361 }
83628362 if ( urlInfo . context . build ) {
8363+ if ( hasModuleScopeAssignment ( urlInfo . contentAst ) ) {
8364+ urlInfo . contentSideEffects . push ( {
8365+ has : true ,
8366+ reason : "import.meta.css assigned at module scope" ,
8367+ } ) ;
8368+ }
83638369 const packageName = urlInfo . packageName ;
83648370 const packageDirectoryUrl =
83658371 urlInfo . packageDirectoryUrl || urlInfo . context . rootDirectoryUrl ;
@@ -8403,6 +8409,28 @@ const jsenvPluginImportMetaCss = () => {
84038409 } ;
84048410} ;
84058411
8412+ // A stylesheet assigned at module scope is adopted when the module runs: that
8413+ // assignment IS the module's side effect. It has to be told, because a
8414+ // package.json "sideEffects" list is authoritative once it exists — a module
8415+ // it does not name is dropped whole when it is imported for nothing else, and
8416+ // its css silently leaves the build.
8417+ const hasModuleScopeAssignment = ( ast ) => {
8418+ for ( const node of ast . body ) {
8419+ if ( node . type !== "ExpressionStatement" ) {
8420+ continue ;
8421+ }
8422+ const { expression } = node ;
8423+ if (
8424+ expression . type === "AssignmentExpression" &&
8425+ expression . operator === "=" &&
8426+ getImportMetaPropertyName ( expression . left ) === "css"
8427+ ) {
8428+ return true ;
8429+ }
8430+ }
8431+ return false ;
8432+ } ;
8433+
84068434const babelPluginRewriteImportMetaCssAssignment = (
84078435 { types : t } ,
84088436 { relativeUrl } ,
0 commit comments