@@ -3,8 +3,96 @@ import Purgecss from 'purgecss';
3
3
import { ConcatSource } from 'webpack-sources' ;
4
4
import path from 'path' ;
5
5
6
- const entryPaths = paths => {
7
- const ret = paths || [ ] ;
6
+ var classCallCheck = function ( instance , Constructor ) {
7
+ if ( ! ( instance instanceof Constructor ) ) {
8
+ throw new TypeError ( "Cannot call a class as a function" ) ;
9
+ }
10
+ } ;
11
+
12
+ var createClass = function ( ) {
13
+ function defineProperties ( target , props ) {
14
+ for ( var i = 0 ; i < props . length ; i ++ ) {
15
+ var descriptor = props [ i ] ;
16
+ descriptor . enumerable = descriptor . enumerable || false ;
17
+ descriptor . configurable = true ;
18
+ if ( "value" in descriptor ) descriptor . writable = true ;
19
+ Object . defineProperty ( target , descriptor . key , descriptor ) ;
20
+ }
21
+ }
22
+
23
+ return function ( Constructor , protoProps , staticProps ) {
24
+ if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ;
25
+ if ( staticProps ) defineProperties ( Constructor , staticProps ) ;
26
+ return Constructor ;
27
+ } ;
28
+ } ( ) ;
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+ var _extends = Object . assign || function ( target ) {
37
+ for ( var i = 1 ; i < arguments . length ; i ++ ) {
38
+ var source = arguments [ i ] ;
39
+
40
+ for ( var key in source ) {
41
+ if ( Object . prototype . hasOwnProperty . call ( source , key ) ) {
42
+ target [ key ] = source [ key ] ;
43
+ }
44
+ }
45
+ }
46
+
47
+ return target ;
48
+ } ;
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+ var toConsumableArray = function ( arr ) {
85
+ if ( Array . isArray ( arr ) ) {
86
+ for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) arr2 [ i ] = arr [ i ] ;
87
+
88
+ return arr2 ;
89
+ } else {
90
+ return Array . from ( arr ) ;
91
+ }
92
+ } ;
93
+
94
+ var entryPaths = function entryPaths ( paths ) {
95
+ var ret = paths || [ ] ;
8
96
9
97
// Convert possible string to an array
10
98
if ( typeof ret === 'string' ) {
@@ -14,9 +102,13 @@ const entryPaths = paths => {
14
102
return ret ;
15
103
} ;
16
104
17
- const flatten = paths => Array . isArray ( paths ) ? paths : Object . keys ( paths ) . reduce ( ( acc , val ) => [ ...acc , ...paths [ val ] ] , [ ] ) ;
105
+ var flatten = function flatten ( paths ) {
106
+ return Array . isArray ( paths ) ? paths : Object . keys ( paths ) . reduce ( function ( acc , val ) {
107
+ return [ ] . concat ( toConsumableArray ( acc ) , toConsumableArray ( paths [ val ] ) ) ;
108
+ } , [ ] ) ;
109
+ } ;
18
110
19
- const entries = ( paths , chunkName ) => {
111
+ var entries = function entries ( paths , chunkName ) {
20
112
if ( Array . isArray ( paths ) ) {
21
113
return paths ;
22
114
}
@@ -25,61 +117,99 @@ const entries = (paths, chunkName) => {
25
117
return [ ] ;
26
118
}
27
119
28
- const ret = paths [ chunkName ] ;
120
+ var ret = paths [ chunkName ] ;
29
121
30
122
return Array . isArray ( ret ) ? ret : [ ret ] ;
31
123
} ;
32
124
33
- const assets = ( assets = [ ] , extensions = [ ] ) => Object . keys ( assets ) . map ( name => {
34
- return extensions . indexOf ( path . extname ( name . indexOf ( '?' ) >= 0 ? name . split ( '?' ) . slice ( 0 , - 1 ) . join ( '' ) : name ) ) >= 0 && { name, asset : assets [ name ] } ;
35
- } ) . filter ( a => a ) ;
125
+ var assets = function assets ( ) {
126
+ var assets = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : [ ] ;
127
+ var extensions = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : [ ] ;
128
+ return Object . keys ( assets ) . map ( function ( name ) {
129
+ return extensions . indexOf ( path . extname ( name . indexOf ( '?' ) >= 0 ? name . split ( '?' ) . slice ( 0 , - 1 ) . join ( '' ) : name ) ) >= 0 && { name : name , asset : assets [ name ] } ;
130
+ } ) . filter ( function ( a ) {
131
+ return a ;
132
+ } ) ;
133
+ } ;
36
134
37
- const files = ( modules = { } , extensions = [ ] , getter = a => a ) => Object . keys ( modules ) . map ( name => {
38
- const file = getter ( modules [ name ] ) ;
135
+ var files = function files ( ) {
136
+ var modules = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
137
+ var extensions = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : [ ] ;
138
+ var getter = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : function ( a ) {
139
+ return a ;
140
+ } ;
141
+ return Object . keys ( modules ) . map ( function ( name ) {
142
+ var file = getter ( modules [ name ] ) ;
143
+
144
+ if ( ! file ) {
145
+ return null ;
146
+ }
147
+
148
+ return extensions . indexOf ( path . extname ( file ) ) >= 0 && file ;
149
+ } ) . filter ( function ( a ) {
150
+ return a ;
151
+ } ) ;
152
+ } ;
39
153
40
- if ( ! file ) {
41
- return null ;
42
- }
154
+ var PurgecssPlugin = function ( ) {
155
+ function PurgecssPlugin ( options ) {
156
+ classCallCheck ( this , PurgecssPlugin ) ;
43
157
44
- return extensions . indexOf ( path . extname ( file ) ) >= 0 && file ;
45
- } ) . filter ( a => a ) ;
158
+ this . options = options ;
159
+ }
46
160
47
- class PurgecssPlugin {
48
- constructor ( options ) {
49
- this . options = options ;
50
- }
161
+ createClass ( PurgecssPlugin , [ {
162
+ key : 'apply' ,
163
+ value : function apply ( compiler ) {
164
+ var _this = this ;
51
165
52
- apply ( compiler ) {
53
- compiler . plugin ( 'this-compilation' , compilation => {
54
- const entryPaths$$1 = entryPaths ( this . options . paths ) ;
166
+ compiler . plugin ( 'this-compilation' , function ( compilation ) {
167
+ var entryPaths$$1 = entryPaths ( _this . options . paths ) ;
168
+
169
+ flatten ( entryPaths$$1 ) . forEach ( function ( p ) {
170
+ if ( ! fs . existsSync ( p ) ) throw new Error ( 'Path ' + p + ' does not exist.' ) ;
171
+ } ) ;
55
172
56
- flatten ( entryPaths$$1 ) . forEach ( p => {
57
- if ( ! fs . existsSync ( p ) ) throw new Error ( `Path ${ p } does not exist.` ) ;
173
+ compilation . plugin ( 'additional-assets' , function ( cb ) {
174
+ // Go through chunks and purge as configured
175
+ compilation . chunks . forEach ( function ( chunk ) {
176
+ var chunkName = chunk . name ,
177
+ files$$1 = chunk . files ;
178
+
179
+ var modules = chunk . mapModules ( function ( mod ) {
180
+ return mod ;
181
+ } ) ;
182
+ var assetsToPurge = assets ( compilation . assets , [ '.css' ] ) . filter ( function ( asset ) {
183
+ return files$$1 . indexOf ( asset . name ) >= 0 ;
58
184
} ) ;
59
185
60
- compilation . plugin ( 'additional-assets' , cb => {
61
- // Go through chunks and purge as configured
62
- compilation . chunks . forEach ( ( { name : chunkName , files : files$$1 , modules } ) => {
63
- const assetsToPurge = assets ( compilation . assets , [ '.css' ] ) . filter ( asset => files$$1 . indexOf ( asset . name ) >= 0 ) ;
64
-
65
- assetsToPurge . forEach ( ( { name, asset } ) => {
66
- const filesToSearch = entries ( entryPaths$$1 , chunkName ) . concat ( files ( modules , this . options . moduleExtensions || [ ] , file => file . resource ) ) . filter ( v => ! v . endsWith ( '.css' ) ) ;
67
-
68
- // Compile through Purgecss and attach to output.
69
- // This loses sourcemaps should there be any!
70
- const purgecss = new Purgecss ( {
71
- content : filesToSearch ,
72
- css : [ asset . source ( ) ] ,
73
- stdin : true
74
- } ) ;
75
- compilation . assets [ name ] = new ConcatSource ( purgecss . purge ( ) [ 0 ] . css ) ;
76
- } ) ;
77
- } ) ;
78
-
79
- cb ( ) ;
186
+ assetsToPurge . forEach ( function ( _ref ) {
187
+ var name = _ref . name ,
188
+ asset = _ref . asset ;
189
+
190
+ var filesToSearch = entries ( entryPaths$$1 , chunkName ) . concat ( files ( modules , _this . options . moduleExtensions || [ ] , function ( file ) {
191
+ return file . resource ;
192
+ } ) ) . filter ( function ( v ) {
193
+ return ! v . endsWith ( '.css' ) ;
194
+ } ) ;
195
+
196
+ // Compile through Purgecss and attach to output.
197
+ // This loses sourcemaps should there be any!
198
+ var purgecss = new Purgecss ( _extends ( { } , _this . options , {
199
+ content : filesToSearch ,
200
+ css : [ asset . source ( ) ] ,
201
+ stdin : true
202
+ } ) ) ;
203
+ compilation . assets [ name ] = new ConcatSource ( purgecss . purge ( ) [ 0 ] . css ) ;
80
204
} ) ;
205
+ } ) ;
206
+
207
+ cb ( ) ;
81
208
} ) ;
209
+ } ) ;
82
210
}
83
- }
211
+ } ] ) ;
212
+ return PurgecssPlugin ;
213
+ } ( ) ;
84
214
85
215
export default PurgecssPlugin ;
0 commit comments