const parser = require ( 'postcss-less' ) ; const postcss = require ( 'postcss' ) ; import { debug as d } from '@hint/utils-debug' ; import { normalizeString } from '@hint/utils-string' ; import { HTMLElement } from '@hint/utils-dom' ; import { Parser } from 'hint/dist/src/lib/types' ; import { StyleEvents } from '@hint/parser-css' ; import { Engine } from 'hint' ; const debug = d ( __filename ) ; export default class CSSParser extends Parser < StyleEvents > { public constructor ( engine ) { super ( engine , 'less' ) ; const emitLESS = async ( code , resource , element ) => { try { await this . engine . emitAsync ( `parse::start::css` , { resource } ) ; const result = await postcss ( ) . process ( code , { from : resource , parser } ) ; const ast = result . root ! ; await this . engine . emitAsync ( `parse::end::css` , { ast , code , element , resource } ) ; } catch ( err ) { debug ( `Error parsing LESS code: ${ code } - ${ err }` ) ; } } ; engine . on ( 'fetch::end::*' , async ( fetchEnd ) => { const code = fetchEnd . response . body . content ; const mediaType = fetchEnd . response . mediaType ; const resource = fetchEnd . resource ; if ( mediaType === 'text/less' || mediaType === 'text/x-less' ) { await emitLESS ( code , resource , null ) ; } } ) ; engine . on ( 'element::style' , async ( { element , resource } ) => { const type = normalizeString ( element . getAttribute ( 'type' ) ) ; if ( type === 'text/less' || type === 'text/x-less' ) { await emitLESS ( element . innerHTML , resource , element ) ; } } ) ; } }