@@ -3,8 +3,6 @@ import { inspect } from "node:util";
33
44import { debug } from "debug" ;
55import {
6- Features ,
7- transform as lightningcss ,
86 type ContainerRule ,
97 type MediaQuery as CSSMediaQuery ,
108 type CustomAtRules ,
@@ -17,12 +15,14 @@ import { maybeMutateReactNativeOptions, parsePropAtRule } from "./atRules";
1715import type {
1816 CompilerOptions ,
1917 ContainerQuery ,
18+ StyleRuleMapping ,
2019 UniqueVarInfo ,
2120} from "./compiler.types" ;
2221import { parseContainerCondition } from "./container-query" ;
2322import { parseDeclaration , round } from "./declarations" ;
2423import { inlineVariables } from "./inline-variables" ;
2524import { extractKeyFrames } from "./keyframes" ;
25+ import { lightningcssLoader } from "./lightningcss-loader" ;
2626import { parseMediaQuery } from "./media-query" ;
2727import { StylesheetBuilder } from "./stylesheet" ;
2828import { supportsConditionValid } from "./supports" ;
@@ -58,6 +58,8 @@ export function compile(code: Buffer | string, options: CompilerOptions = {}) {
5858
5959 const builder = new StylesheetBuilder ( options ) ;
6060
61+ const { lightningcss, Features } = lightningcssLoader ( ) ;
62+
6163 logger ( `Lightningcss first pass` ) ;
6264
6365 /**
@@ -182,7 +184,11 @@ export function compile(code: Buffer | string, options: CompilerOptions = {}) {
182184/**
183185 * Extracts style declarations and animations from a given CSS rule, based on its type.
184186 */
185- function extractRule ( rule : Rule , builder : StylesheetBuilder ) {
187+ function extractRule (
188+ rule : Rule ,
189+ builder : StylesheetBuilder ,
190+ mapping : StyleRuleMapping = { } ,
191+ ) {
186192 // Check the rule's type to determine which extraction function to call
187193 switch ( rule . type ) {
188194 case "keyframes" : {
@@ -192,12 +198,12 @@ function extractRule(rule: Rule, builder: StylesheetBuilder) {
192198 }
193199 case "container" : {
194200 // If the rule is a container, extract it with the `extractedContainer` function
195- extractContainer ( rule . value , builder ) ;
201+ extractContainer ( rule . value , builder , mapping ) ;
196202 break ;
197203 }
198204 case "media" : {
199205 // If the rule is a media query, extract it with the `extractMedia` function
200- extractMedia ( rule . value , builder ) ;
206+ extractMedia ( rule . value , builder , mapping ) ;
201207 break ;
202208 }
203209 case "nested-declarations" : {
@@ -227,21 +233,21 @@ function extractRule(rule: Rule, builder: StylesheetBuilder) {
227233 const value = rule . value ;
228234
229235 const declarationBlock = value . declarations ;
230- const mapping = parsePropAtRule ( value . rules ) ;
236+ mapping = { ... mapping , ... parsePropAtRule ( value . rules ) } ;
231237
232238 // If the rule is a style declaration, extract it with the `getExtractedStyle` function and store it in the `declarations` map
233239 builder = builder . fork ( "style" , value . selectors ) ;
234240
235241 if ( declarationBlock ) {
236- if ( declarationBlock . declarations ) {
242+ if ( declarationBlock . declarations ?. length ) {
237243 builder . newRule ( mapping ) ;
238244 for ( const declaration of declarationBlock . declarations ) {
239245 parseDeclaration ( declaration , builder ) ;
240246 }
241247 builder . applyRuleToSelectors ( ) ;
242248 }
243249
244- if ( declarationBlock . importantDeclarations ) {
250+ if ( declarationBlock . importantDeclarations ?. length ) {
245251 builder . newRule ( mapping , { important : true } ) ;
246252 for ( const declaration of declarationBlock . importantDeclarations ) {
247253 parseDeclaration ( declaration , builder ) ;
@@ -252,21 +258,21 @@ function extractRule(rule: Rule, builder: StylesheetBuilder) {
252258
253259 if ( value . rules ) {
254260 for ( const nestedRule of value . rules ) {
255- extractRule ( nestedRule , builder ) ;
261+ extractRule ( nestedRule , builder , mapping ) ;
256262 }
257263 }
258264
259265 break ;
260266 }
261267 case "layer-block" :
262268 for ( const layerRule of rule . value . rules ) {
263- extractRule ( layerRule , builder ) ;
269+ extractRule ( layerRule , builder , mapping ) ;
264270 }
265271 break ;
266272 case "supports" :
267273 if ( supportsConditionValid ( rule . value . condition ) ) {
268274 for ( const layerRule of rule . value . rules ) {
269- extractRule ( layerRule , builder ) ;
275+ extractRule ( layerRule , builder , mapping ) ;
270276 }
271277 }
272278 break ;
@@ -303,7 +309,11 @@ function extractRule(rule: Rule, builder: StylesheetBuilder) {
303309 *
304310 * @returns undefined if no screen media queries are found in the mediaRule, else it returns the extracted styles.
305311 */
306- function extractMedia ( mediaRule : MediaRule , builder : StylesheetBuilder ) {
312+ function extractMedia (
313+ mediaRule : MediaRule ,
314+ builder : StylesheetBuilder ,
315+ mapping : StyleRuleMapping ,
316+ ) {
307317 builder = builder . fork ( "media" ) ;
308318
309319 // Initialize an empty array to store screen media queries
@@ -336,7 +346,7 @@ function extractMedia(mediaRule: MediaRule, builder: StylesheetBuilder) {
336346
337347 // Iterate over all rules in the mediaRule and extract their styles using the updated CompilerCollection
338348 for ( const rule of mediaRule . rules ) {
339- extractRule ( rule , builder ) ;
349+ extractRule ( rule , builder , mapping ) ;
340350 }
341351}
342352
@@ -348,6 +358,7 @@ function extractMedia(mediaRule: MediaRule, builder: StylesheetBuilder) {
348358function extractContainer (
349359 containerRule : ContainerRule ,
350360 builder : StylesheetBuilder ,
361+ mapping : StyleRuleMapping ,
351362) {
352363 builder = builder . fork ( "container" ) ;
353364
@@ -363,6 +374,6 @@ function extractContainer(
363374 builder . addContainerQuery ( query ) ;
364375
365376 for ( const rule of containerRule . rules ) {
366- extractRule ( rule , builder ) ;
377+ extractRule ( rule , builder , mapping ) ;
367378 }
368379}
0 commit comments