import { TransformCallback } from 'stream' ; import replaceExtension from 'replace-ext' ; import applySourceMap from 'vinyl-sourcemaps-apply' ; import { BalmError } from '@balm-core/index' ; const PLUGIN_NAME = 'sass' ; interface GulpSass { ( options ) : any ; compiler ? : any ; } const gulpSass = ( options ) : any => through2 . obj ( ( file , encoding , cb ) => { if ( file . isNull ( ) ) { return cb ( null , file ) ; } if ( file . isStream ( ) ) { return cb ( new PluginError ( PLUGIN_NAME , 'Streaming not supported' ) ) ; } if ( path . basename ( file . path as string ) . indexOf ( '_' ) === 0 ) { return cb ( ) ; } if ( ! file . contents . length ) { file . path = replaceExtension ( file . path , '.css' ) ; return cb ( null , file ) ; } const opts = ( options || { } ) as any ; opts . data = file . contents . toString ( ) ; opts . file = file . path ; if ( path . extname ( file . path as string ) === '.sass' ) { opts . indentedSyntax = true ; } if ( opts . includePaths ) { if ( typeof opts . includePaths === 'string' ) { opts . includePaths = [ opts . includePaths ] ; } } else { opts . includePaths = [ ] ; } opts . includePaths . unshift ( path . dirname ( file . path as string ) ) ; if ( file . sourceMap ) { opts . sourceMap = file . path ; opts . omitSourceMapUrl = true ; opts . sourceMapContents = true ; } const filePush = ( sassObj ) => { let sassMap ; let sassMapFile ; let sassFileSrc ; let sassFileSrcPath ; let sourceFileIndex ; if ( sassObj . map ) { sassMap = JSON . parse ( sassObj . map . toString ( ) as string ) ; sassMapFile = sassMap . file . replace ( /^stdout$/ , 'stdin' ) ; sassFileSrc = file . relative ; sassFileSrcPath = path . dirname ( sassFileSrc ) ; if ( sassFileSrcPath ) { sourceFileIndex = sassMap . sources . indexOf ( sassMapFile ) ; sassMap . sources = sassMap . sources . map ( ( source , index ) => { return index === sourceFileIndex ? source : path . join ( sassFileSrcPath , source ) ; } ) ; } sassMap . sources = sassMap . sources . filter ( ( src ) => src !== 'stdin' && src ) ; sassMap . file = replaceExtension ( sassFileSrc , '.css' ) ; applySourceMap ( file , sassMap ) ; } file . contents = sassObj . css ; file . path = replaceExtension ( file . path , '.css' ) ; if ( file . stat ) { file . stat . atime = file . stat . mtime = file . stat . ctime = new Date ( ) ; } cb ( null , file ) ; } ; const errorM = ( error ) => { const filePath = ( ( error . file === 'stdin' ? file . path : error . file ) || file . path ) as string ; const relativePath = path . relative ( process . cwd ( ) , filePath ) ; const message = [ relativePath , error . formatted ] . join ( '\n' ) ; error . messageFormatted = message ; error . messageOriginal = error . message ; error . message = require ( 'strip-ansi' ) ( message ) ; error . relativePath = relativePath ; return cb ( new PluginError ( PLUGIN_NAME , error as BalmError ) ) ; } ; try { gulpSass . compiler && filePush ( gulpSass . compiler . renderSync ( opts ) ) ; } catch ( error ) { return errorM ( error ) ; } } ) ; export default ( options ) => { gulpSass . compiler = require ( 'sass' ) ; return gulpSass ( options ) ; } ;