@@ -6,7 +6,7 @@ const loaderUtils = require('loader-utils');
66
77module . exports = postcss . plugin (
88 'postcss-css-loader-parser' ,
9- ( options ) => ( css ) => {
9+ ( options ) => ( css , result ) => {
1010 const imports = { } ;
1111 let exports = { } ;
1212 const importItems = [ ] ;
@@ -32,18 +32,42 @@ module.exports = postcss.plugin(
3232 }
3333
3434 if ( options . import ) {
35- css . walkAtRules ( / ^ i m p o r t $ / i, ( rule ) => {
36- const values = Tokenizer . parseValues ( rule . params ) ;
35+ css . walkAtRules ( / ^ i m p o r t $ / i, ( atrule ) => {
36+ // Convert only top-level @import
37+ if ( atrule . parent . type !== 'root' ) {
38+ return ;
39+ }
40+
41+ if ( atrule . nodes ) {
42+ result . warn (
43+ "It looks like you didn't end your @import statement correctly. " +
44+ 'Child nodes are attached to it.' ,
45+ { node : atrule }
46+ ) ;
47+ return ;
48+ }
49+
50+ const values = Tokenizer . parseValues ( atrule . params ) ;
3751 let [ url ] = values . nodes [ 0 ] . nodes ;
52+
3853 if ( url && url . type === 'url' ) {
3954 ( { url } = url ) ;
4055 } else if ( url && url . type === 'string' ) {
4156 url = url . value ;
42- } else throw rule . error ( `Unexpected format ${ rule . params } ` ) ;
57+ } else {
58+ result . warn ( `Unable to find uri in '${ atrule . toString ( ) } '` , {
59+ node : atrule ,
60+ } ) ;
61+
62+ return ;
63+ }
64+
4365 if ( ! url . replace ( / \s / g, '' ) . length ) {
4466 return ;
4567 }
68+
4669 values . nodes [ 0 ] . nodes . shift ( ) ;
70+
4771 const mediaQuery = Tokenizer . stringifyValues ( values ) ;
4872
4973 if ( loaderUtils . isUrlRequest ( url ) ) {
@@ -54,7 +78,7 @@ module.exports = postcss.plugin(
5478 url,
5579 mediaQuery,
5680 } ) ;
57- rule . remove ( ) ;
81+ atrule . remove ( ) ;
5882 } ) ;
5983 }
6084
0 commit comments