src/util.js
function handleImportStyle(styles, initPath, mayRM) {
    if (!!mayRM) {
        styles = styles.replace(mayRM, '')
    }
    if (styles.indexOf('@import') < 0) return styles
    const ast = csstree.parse(styles);
    csstree.walk(ast, {
        enter: function (node) {
            if (node.type === 'Atrule') {
                if (node.name === 'import') {
                    const pat = node.prelude.children.head.data.value
                    if (initPath && typeof pat === 'string') {
                        const realP = pat.substring(1, pat.length - 1)
                        const newPath = path.dirname(initPath)
                        const resPath = path.resolve(newPath, realP)
                        const text = fs.readFileSync(resPath).toString()
                        const re = new RegExp("[\\s]*@import[\\s]+\"" + realP + "\";", "g");
                        //handle with loop import
                        styles = styles.replace(re, handleImportStyle(text, resPath, re))
                    }
                }
            }
        }
    })
    return styles
}
function handleImportStyle(styles, initPath, mayRM) {
    if (!!mayRM) {
        styles = styles.replace(mayRM, '')
    }
    if (styles.indexOf('@import') < 0) return styles
    const ast = csstree.parse(styles);
    csstree.walk(ast, {
        enter: function (node) {
            if (node.type === 'Atrule') {
                if (node.name === 'import') {
                    const pat = node.prelude.children.head.data.value
                    if (initPath && typeof pat === 'string') {
                        const realP = pat.substring(1, pat.length - 1)
                        const newPath = path.dirname(initPath)
                        const resPath = path.resolve(newPath, realP)
                        const text = fs.readFileSync(resPath).toString()
                        const re = new RegExp("[\\s]*@import[\\s]+\"" + realP + "\";", "g");
                        //handle with loop import
                        styles = styles.replace(re, handleImportStyle(text, resPath, re))
                    }
                }
            }
        }
    })
    return styles
}
function handleImportStyle ( styles , initPath , mayRM ) { if ( ! ! mayRM ) { styles = styles . replace ( mayRM , '' ) } if ( styles . indexOf ( '@import' ) < 0 ) return styles const ast = csstree . parse ( styles ) ; csstree . walk ( ast , { enter : function ( node ) { if ( node . type === 'Atrule' ) { if ( node . name === 'import' ) { const pat = node . prelude . children . head . data . value if ( initPath && typeof pat === 'string' ) { const realP = pat . substring ( 1 , pat . length - 1 ) const newPath = path . dirname ( initPath ) const resPath = path . resolve ( newPath , realP ) const text = fs . readFileSync ( resPath ) . toString ( ) const re = new RegExp ( "[\\s]*@import[\\s]+\"" + realP + "\";" , "g" ) ; //handle with loop import styles = styles . replace ( re , handleImportStyle ( text , resPath , re ) ) } } } } } ) return styles }
handle with import procedure,may search involved file in project
@param styles styles css file content
@param initPath project path, used for searching target file
@param mayRM this param avoid loop importing
@returns handled styles content
handle with import procedure may search involved file in project