@@ -107,10 +107,7 @@ function AtImport(options) {
107
107
108
108
// Strip additional statements.
109
109
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 ) ) {
114
111
stmt . node . parent = undefined
115
112
styles . append ( stmt . node )
116
113
} else if ( stmt . type === "nodes" ) {
@@ -150,15 +147,33 @@ function AtImport(options) {
150
147
} , Promise . resolve ( ) )
151
148
} )
152
149
. then ( ( ) => {
150
+ let charset
153
151
const imports = [ ]
154
152
const bundle = [ ]
155
153
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
+
156
169
// squash statements and their children
157
170
statements . forEach ( stmt => {
158
- if ( stmt . type === "import" ) {
171
+ if ( stmt . type === "charset" ) handleCharset ( stmt )
172
+ else if ( stmt . type === "import" ) {
159
173
if ( stmt . children ) {
160
174
stmt . children . forEach ( ( child , index ) => {
161
175
if ( child . type === "import" ) imports . push ( child )
176
+ else if ( child . type === "charset" ) handleCharset ( child )
162
177
else bundle . push ( child )
163
178
// For better output
164
179
if ( index === 0 ) child . parent = stmt
@@ -169,7 +184,9 @@ function AtImport(options) {
169
184
}
170
185
} )
171
186
172
- return imports . concat ( bundle )
187
+ return charset
188
+ ? [ charset , ...imports . concat ( bundle ) ]
189
+ : imports . concat ( bundle )
173
190
} )
174
191
}
175
192
0 commit comments