1
- 'use strict' ;
2
-
3
1
const applySourceMap = require ( 'vinyl-sourcemaps-apply' ) ;
4
2
const CleanCSS = require ( 'clean-css' ) ;
5
3
const path = require ( 'path' ) ;
6
4
const PluginError = require ( 'plugin-error' ) ;
7
5
const through = require ( 'through2' ) ;
8
6
9
- module . exports = function gulpCleanCSS ( options , callback ) {
10
-
11
- options = Object . assign ( options || { } ) ;
7
+ module . exports = ( options , callback ) => {
12
8
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 ) ;
15
11
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 ) {
20
13
21
14
if ( file . isStream ( ) ) {
22
15
this . emit ( 'error' , new PluginError ( 'gulp-clean-css' , 'Streaming not supported!' ) ) ;
23
16
return cb ( null , file ) ;
24
17
}
25
18
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 ) ) ;
33
21
}
34
22
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
+ }
36
29
37
- if ( errors )
30
+ new CleanCSS ( _options ) . minify ( content , ( errors , css ) => {
31
+ if ( errors ) {
38
32
return cb ( errors . join ( ' ' ) ) ;
33
+ }
39
34
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
+ } ;
51
42
52
- callback ( details ) ;
43
+ if ( css . sourceMap ) {
44
+ details [ 'sourceMap' ] = css . sourceMap ;
53
45
}
46
+ _callback ( details ) ;
54
47
55
- file . contents = new Buffer ( css . styles ) ;
48
+ file . contents = new Buffer . from ( css . styles ) ;
56
49
57
50
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 ) )
63
55
} ) ;
64
-
65
- applySourceMap ( file , map ) ;
56
+ applySourceMap ( file , oMap ) ;
66
57
}
67
-
68
58
cb ( null , file ) ;
69
59
} ) ;
70
- } ;
71
-
72
- return through . obj ( transform ) ;
73
- } ;
60
+ } ) ;
61
+ } ;
0 commit comments