33const base64EncodedConditionalImport = require ( "./base64-encoded-import" )
44
55module . exports = function applyConditions ( bundle , atRule ) {
6- bundle . forEach ( stmt => {
6+ const firstImportStatementIndex = bundle . findIndex (
7+ stmt => stmt . type === "import" ,
8+ )
9+ const lastImportStatementIndex = bundle . findLastIndex (
10+ stmt => stmt . type === "import" ,
11+ )
12+
13+ bundle . forEach ( ( stmt , index ) => {
14+ if ( stmt . type === "charset" || stmt . type === "warning" ) {
15+ return
16+ }
17+
718 if (
8- stmt . type === "charset" ||
9- stmt . type === "warning" ||
10- ! stmt . conditions ?. length
19+ stmt . type === "layer" &&
20+ ( ( index < lastImportStatementIndex && stmt . conditions ?. length ) ||
21+ ( index > firstImportStatementIndex && index < lastImportStatementIndex ) )
1122 ) {
23+ stmt . type = "import"
24+ stmt . node = stmt . node . clone ( {
25+ name : "import" ,
26+ params : base64EncodedConditionalImport (
27+ `'data:text/css;base64,${ Buffer . from ( stmt . node . toString ( ) ) . toString (
28+ "base64" ,
29+ ) } '`,
30+ stmt . conditions ,
31+ ) ,
32+ } )
33+
34+ return
35+ }
36+
37+ if ( ! stmt . conditions ?. length ) {
1238 return
1339 }
1440
@@ -20,8 +46,15 @@ module.exports = function applyConditions(bundle, atRule) {
2046 return
2147 }
2248
23- const { nodes } = stmt
24- const { parent } = nodes [ 0 ]
49+ let nodes
50+ let parent
51+ if ( stmt . type === "layer" ) {
52+ nodes = [ stmt . node ]
53+ parent = stmt . node . parent
54+ } else {
55+ nodes = stmt . nodes
56+ parent = nodes [ 0 ] . parent
57+ }
2558
2659 const atRules = [ ]
2760
0 commit comments