@@ -5,55 +5,53 @@ import * as parse from './parse'
55import * as search from './search'
66
77export default class PurgecssPlugin {
8- constructor ( options ) {
9- this . options = options
10- }
8+ constructor ( options ) {
9+ this . options = options
10+ }
1111
12- apply ( compiler ) {
13- compiler . plugin ( 'this-compilation' , compilation => {
14- const entryPaths = parse . entryPaths ( this . options . paths )
12+ apply ( compiler ) {
13+ compiler . plugin ( 'this-compilation' , compilation => {
14+ const entryPaths = parse . entryPaths ( this . options . paths )
1515
16- parse . flatten ( entryPaths ) . forEach ( p => {
17- if ( ! fs . existsSync ( p ) )
18- throw new Error ( `Path ${ p } does not exist.` )
19- } )
20-
21- compilation . plugin ( 'additional-assets' , cb => {
22- // Go through chunks and purge as configured
23- compilation . chunks . forEach (
24- ( { name : chunkName , files, modules } ) => {
25- const assetsToPurge = search
26- . assets ( compilation . assets , [ '.css' ] )
27- . filter ( asset => files . indexOf ( asset . name ) >= 0 )
16+ parse . flatten ( entryPaths ) . forEach ( p => {
17+ if ( ! fs . existsSync ( p ) ) throw new Error ( `Path ${ p } does not exist.` )
18+ } )
2819
29- assetsToPurge . forEach ( ( { name, asset } ) => {
30- const filesToSearch = parse
31- . entries ( entryPaths , chunkName )
32- . concat (
33- search . files (
34- modules ,
35- this . options . moduleExtensions || [ ] ,
36- file => file . resource
37- )
38- )
39- . filter ( v => ! v . endsWith ( '.css' ) )
20+ compilation . plugin ( 'additional-assets' , cb => {
21+ // Go through chunks and purge as configured
22+ compilation . chunks . forEach ( chunk => {
23+ const { name : chunkName , files } = chunk
24+ const modules = chunk . mapModules ( mod => mod )
25+ const assetsToPurge = search
26+ . assets ( compilation . assets , [ '.css' ] )
27+ . filter ( asset => files . indexOf ( asset . name ) >= 0 )
4028
41- // Compile through Purgecss and attach to output.
42- // This loses sourcemaps should there be any!
43- const purgecss = new Purgecss ( {
44- content : filesToSearch ,
45- css : [ asset . source ( ) ] ,
46- stdin : true
47- } )
48- compilation . assets [ name ] = new ConcatSource (
49- purgecss . purge ( ) [ 0 ] . css
50- )
51- } )
52- }
29+ assetsToPurge . forEach ( ( { name, asset } ) => {
30+ const filesToSearch = parse
31+ . entries ( entryPaths , chunkName )
32+ . concat (
33+ search . files (
34+ modules ,
35+ this . options . moduleExtensions || [ ] ,
36+ file => file . resource
5337 )
38+ )
39+ . filter ( v => ! v . endsWith ( '.css' ) )
5440
55- cb ( )
41+ // Compile through Purgecss and attach to output.
42+ // This loses sourcemaps should there be any!
43+ const purgecss = new Purgecss ( {
44+ ...this . options ,
45+ content : filesToSearch ,
46+ css : [ asset . source ( ) ] ,
47+ stdin : true
5648 } )
49+ compilation . assets [ name ] = new ConcatSource ( purgecss . purge ( ) [ 0 ] . css )
50+ } )
5751 } )
58- }
52+
53+ cb ( )
54+ } )
55+ } )
56+ }
5957}
0 commit comments