const postcss = require ( 'postcss' ) ; const rtl = require ( 'postcss-rtl' ) ; const CSS_PROCESSOR = postcss ( [ rtl ( ) ] ) ; function generateRtlCss ( css ) { return CSS_PROCESSOR . process ( css ) . css . replace ( /\[dir=rtl\]/g , ':host(:dir(rtl))' ) . replace ( /\[dir=ltr\]/g , ':host(:dir(ltr))' ) . replace ( /\[dir\]/g , '' ) ; } module . exports = function loader ( content , map , meta ) { const callback = this . async ( ) ; const styleRe = RegExp ( /(<style[^>]*>)(\s*[^<\s](.*\n)*?\s*)(<\/style>)/gm ) ; try { const newContent = content . replace ( styleRe , ( match , g1 , g2 , g3 , g4 ) => `${ g1 }${ generateRtlCss ( g2 ) }${ g4 }` ) ; callback ( null , newContent ) ; } catch ( e ) { console . warn ( e . toString ( ) ) ; throw e ; } } ;