1- 'use strict' ;
2-
31const applySourceMap = require ( 'vinyl-sourcemaps-apply' ) ;
42const CleanCSS = require ( 'clean-css' ) ;
53const path = require ( 'path' ) ;
64const PluginError = require ( 'plugin-error' ) ;
75const through = require ( 'through2' ) ;
86
9- module . exports = function gulpCleanCSS ( options , callback ) {
10-
11- options = Object . assign ( options || { } ) ;
7+ module . exports = ( options , callback ) => {
128
13- if ( arguments . length === 1 && Object . prototype . toString . call ( arguments [ 0 ] ) === '[object Function]' )
14- callback = arguments [ 0 ] ;
9+ let _options = Object . assign ( { } , options || { } ) ;
10+ let _callback = callback || ( o => undefined ) ;
1511
16- let transform = function ( file , enc , cb ) {
17-
18- if ( ! file || ! file . contents )
19- return cb ( null , file ) ;
12+ return through . obj ( function ( file , enc , cb ) {
2013
2114 if ( file . isStream ( ) ) {
2215 this . emit ( 'error' , new PluginError ( 'gulp-clean-css' , 'Streaming not supported!' ) ) ;
2316 return cb ( null , file ) ;
2417 }
2518
26- if ( file . sourceMap )
27- options . sourceMap = JSON . parse ( JSON . stringify ( file . sourceMap ) ) ;
28-
29- let contents = file . contents ? file . contents . toString ( ) : '' ;
30- let pass = { [ file . path ] : { styles : contents } } ;
31- if ( ! options . rebaseTo && options . rebase !== false ) {
32- options . rebaseTo = path . dirname ( file . path ) ;
19+ if ( file . sourceMap ) {
20+ _options . sourceMap = JSON . parse ( JSON . stringify ( file . sourceMap ) ) ;
3321 }
3422
35- new CleanCSS ( options ) . minify ( pass , function ( errors , css ) {
23+ const content = {
24+ [ file . path ] : { styles : file . contents . toString ( ) }
25+ } ;
26+ if ( ! _options . rebaseTo && _options . rebase !== false ) {
27+ _options . rebaseTo = path . dirname ( file . path ) ;
28+ }
3629
37- if ( errors )
30+ new CleanCSS ( _options ) . minify ( content , ( errors , css ) => {
31+ if ( errors ) {
3832 return cb ( errors . join ( ' ' ) ) ;
33+ }
3934
40- if ( typeof callback === 'function' ) {
41- let details = {
42- 'stats' : css . stats ,
43- 'errors' : css . errors ,
44- 'warnings' : css . warnings ,
45- 'path' : file . path ,
46- 'name' : file . path . split ( file . base ) [ 1 ]
47- } ;
48-
49- if ( css . sourceMap )
50- details [ 'sourceMap' ] = css . sourceMap ;
35+ let details = {
36+ 'stats' : css . stats ,
37+ 'errors' : css . errors ,
38+ 'warnings' : css . warnings ,
39+ 'path' : file . path ,
40+ 'name' : file . path . split ( file . base ) [ 1 ]
41+ } ;
5142
52- callback ( details ) ;
43+ if ( css . sourceMap ) {
44+ details [ 'sourceMap' ] = css . sourceMap ;
5345 }
46+ _callback ( details ) ;
5447
55- file . contents = new Buffer ( css . styles ) ;
48+ file . contents = new Buffer . from ( css . styles ) ;
5649
5750 if ( css . sourceMap ) {
58-
59- let map = JSON . parse ( css . sourceMap ) ;
60- map . file = path . relative ( file . base , file . path ) ;
61- map . sources = map . sources . map ( function ( src ) {
62- return path . relative ( file . base , file . path )
51+ const iMap = JSON . parse ( css . sourceMap ) ;
52+ const oMap = Object . assign ( { } , iMap , {
53+ file : path . relative ( file . base , file . path ) ,
54+ sources : iMap . sources . map ( ( ) => path . relative ( file . base , file . path ) )
6355 } ) ;
64-
65- applySourceMap ( file , map ) ;
56+ applySourceMap ( file , oMap ) ;
6657 }
67-
6858 cb ( null , file ) ;
6959 } ) ;
70- } ;
71-
72- return through . obj ( transform ) ;
73- } ;
60+ } ) ;
61+ } ;
0 commit comments