src/util.js
function createStylefromCode(styles, abspath) {
    //may import other file
    const arr = queryImport(styles)
    const baseRes = handleImportStyle(styles, abspath)
    const obj = {}
    const ast = csstree.parse(baseRes);
    csstree.walk(ast, {
        enter: function (node) {
            if (node.type === 'Rule' && node.prelude && node.prelude.type === 'SelectorList') {
                const clzName = []
                node.prelude.children.forEach(e => {
                    //we may handle each class selector here
                    let ans = selectorBlockHandler(e)
                    if (ans) {
                        //to save before value
                        obj[ans] = obj[ans] || {}
                        //save ans to log which to give value
                        clzName.push(ans)
                    }
                })
                const styleArray = []
                node.block.children.forEach(e => {
                    //handle with unit or function case
                    const styleItem = styleBlockHandler(e)
                    styleItem.length > 0 && styleArray.push(styleItem)
                })
                clzName.forEach(e => {
                    try {
                        const styleObject = transform(styleArray)
                        if (styleObject.fontWeight) {
                            //fontWeight must be string
                            styleObject.fontWeight = styleObject.fontWeight + ""
                        }
                        Object.assign(obj[e], styleObject)
                    } catch (e) {
                        console.error("convert ", abspath, " error:", e.message)
                    }

                })
            }
        },
    })
    return {styles: obj, imports: arr}
}
function createStylefromCode(styles, abspath) {
    //may import other file
    const arr = queryImport(styles)
    const baseRes = handleImportStyle(styles, abspath)
    const obj = {}
    const ast = csstree.parse(baseRes);
    csstree.walk(ast, {
        enter: function (node) {
            if (node.type === 'Rule' && node.prelude && node.prelude.type === 'SelectorList') {
                const clzName = []
                node.prelude.children.forEach(e => {
                    //we may handle each class selector here
                    let ans = selectorBlockHandler(e)
                    if (ans) {
                        //to save before value
                        obj[ans] = obj[ans] || {}
                        //save ans to log which to give value
                        clzName.push(ans)
                    }
                })
                const styleArray = []
                node.block.children.forEach(e => {
                    //handle with unit or function case
                    const styleItem = styleBlockHandler(e)
                    styleItem.length > 0 && styleArray.push(styleItem)
                })
                clzName.forEach(e => {
                    try {
                        const styleObject = transform(styleArray)
                        if (styleObject.fontWeight) {
                            //fontWeight must be string
                            styleObject.fontWeight = styleObject.fontWeight + ""
                        }
                        Object.assign(obj[e], styleObject)
                    } catch (e) {
                        console.error("convert ", abspath, " error:", e.message)
                    }

                })
            }
        },
    })
    return {styles: obj, imports: arr}
}
function createStylefromCode ( styles , abspath ) { //may import other file const arr = queryImport ( styles ) const baseRes = handleImportStyle ( styles , abspath ) const obj = { } const ast = csstree . parse ( baseRes ) ; csstree . walk ( ast , { enter : function ( node ) { if ( node . type === 'Rule' && node . prelude && node . prelude . type === 'SelectorList' ) { const clzName = [ ] node . prelude . children . forEach ( e => { //we may handle each class selector here let ans = selectorBlockHandler ( e ) if ( ans ) { //to save before value obj [ ans ] = obj [ ans ] || { } //save ans to log which to give value clzName . push ( ans ) } } ) const styleArray = [ ] node . block . children . forEach ( e => { //handle with unit or function case const styleItem = styleBlockHandler ( e ) styleItem . length > 0 && styleArray . push ( styleItem ) } ) clzName . forEach ( e => { try { const styleObject = transform ( styleArray ) if ( styleObject . fontWeight ) { //fontWeight must be string styleObject . fontWeight = styleObject . fontWeight + "" } Object . assign ( obj [ e ] , styleObject ) } catch ( e ) { console . error ( "convert " , abspath , " error:" , e . message ) } } ) } } , } ) return { styles : obj , imports : arr } }
create style object from css file content,which is raw string.
@param styles styles styles css file content
@param abspath project file path
@returns {{styles, imports: Array}}
create style object from css file content which is raw string .