@@ -107,10 +107,7 @@ function AtImport(options) {
107107
108108 // Strip additional statements.
109109 bundle . forEach ( stmt => {
110- if ( stmt . type === "import" ) {
111- stmt . node . parent = undefined
112- styles . append ( stmt . node )
113- } else if ( stmt . type === "media" ) {
110+ if ( [ "charset" , "import" , "media" ] . includes ( stmt . type ) ) {
114111 stmt . node . parent = undefined
115112 styles . append ( stmt . node )
116113 } else if ( stmt . type === "nodes" ) {
@@ -150,15 +147,33 @@ function AtImport(options) {
150147 } , Promise . resolve ( ) )
151148 } )
152149 . then ( ( ) => {
150+ let charset
153151 const imports = [ ]
154152 const bundle = [ ]
155153
154+ function handleCharset ( stmt ) {
155+ if ( ! charset ) charset = stmt
156+ // charsets aren't case-sensitive, so convert to lower case to compare
157+ else if (
158+ stmt . node . params . toLowerCase ( ) !==
159+ charset . node . params . toLowerCase ( )
160+ ) {
161+ throw new Error (
162+ `Incompatable @charset statements:
163+ ${ stmt . node . params } specified in ${ stmt . node . source . input . file }
164+ ${ charset . node . params } specified in ${ charset . node . source . input . file } `
165+ )
166+ }
167+ }
168+
156169 // squash statements and their children
157170 statements . forEach ( stmt => {
158- if ( stmt . type === "import" ) {
171+ if ( stmt . type === "charset" ) handleCharset ( stmt )
172+ else if ( stmt . type === "import" ) {
159173 if ( stmt . children ) {
160174 stmt . children . forEach ( ( child , index ) => {
161175 if ( child . type === "import" ) imports . push ( child )
176+ else if ( child . type === "charset" ) handleCharset ( child )
162177 else bundle . push ( child )
163178 // For better output
164179 if ( index === 0 ) child . parent = stmt
@@ -169,7 +184,9 @@ function AtImport(options) {
169184 }
170185 } )
171186
172- return imports . concat ( bundle )
187+ return charset
188+ ? [ charset , ...imports . concat ( bundle ) ]
189+ : imports . concat ( bundle )
173190 } )
174191 }
175192
0 commit comments