import postCss from 'postcss' ; import { loadDiagnostic } from './diagnostics' ; import type * as d from './declarations' ; import * as util from './util' ; export function postcss ( opts = { } ) { return { name : 'postcss' , pluginType : 'css' , transform ( sourceText , fileName , context ) { if ( ! opts . hasOwnProperty ( 'plugins' ) || opts . plugins . length === 0 ) { return null ; } if ( ! context || ! util . usePlugin ( fileName ) ) { return null ; } const renderOpts = util . getRenderOptions ( opts , sourceText , context ) ; const results = { id : util . createResultsId ( fileName ) , } ; if ( sourceText . trim ( ) === '' ) { results . code = '' ; return Promise . resolve ( results ) ; } return new Promise < d . PluginTransformResults > ( ( resolve ) => { postCss ( renderOpts . plugins ) . process ( renderOpts . data , { from : fileName , } ) . then ( ( postCssResults ) => { const warnings = postCssResults . warnings ( ) ; if ( warnings . length > 0 ) { warnings . forEach ( ( warn ) => { const err = { reason : warn . text , level : warn . type , column : warn . column || - 1 , line : warn . line || - 1 , } ; loadDiagnostic ( context , err , fileName ) ; } ) ; const mappedWarnings = warnings . map ( ( warn ) => { return `${ warn . type } ${ warn . plugin ? `(${ warn . plugin })` : '' }: ${ warn . text }` ; } ) . join ( ', ' ) ; results . code = `/**  postcss ${ mappedWarnings }  **/` ; resolve ( results ) ; } else { results . code = postCssResults . css . toString ( ) ; results . dependencies = postCssResults . messages . filter ( ( message ) => message . type === 'dependency' ) . map ( ( dependency ) => dependency . file ) ; context . fs . writeFile ( results . id , results . code , { inMemoryOnly : true } ) . then ( ( ) => { resolve ( results ) ; } ) ; } return results ; } ) . catch ( ( err ) => { loadDiagnostic ( context , err , fileName ) ; results . code = `/**  postcss error${ err && err . message ? ': ' + err . message : '' }  **/` ; resolve ( results ) ; } ) ; } ) ; } , } ; }