import { TransformCallback } from 'stream' ; import { BalmError } from '@balm-core/index' ; const PLUGIN_NAME = 'htmlmin' ; function gulpHtmlmin ( options ) { options = BalmJS . utils . deepMerge ( { } , options ) ; function transform ( file , encoding , callback ) { if ( file . isNull ( ) ) { callback ( null , file ) ; return ; } function minify ( buf , enc , cb ) { try { const contents = Buffer . from ( require ( 'html-minifier' ) . minify ( buf . toString ( ) , options ) ) ; if ( cb === callback ) { file . contents = contents ; cb ( null , file ) ; } else { cb ( null , contents ) ; callback ( null , file ) ; } } catch ( e ) { const opts = Object . assign ( options , { fileName : file . path } ) ; const error = new PluginError ( PLUGIN_NAME , e as BalmError , opts ) ; cb === callback ? cb ( error ) : callback ( error ) ; } } if ( file . isStream ( ) ) { file . contents = file . contents . pipe ( through2 ( minify ) ) ; } else { minify ( file . contents , null , callback ) ; } } return through2 . obj ( transform ) ; } export default gulpHtmlmin ;