@@ -11,78 +11,79 @@ const chalk = require('chalk');
1111 */
1212function OmitJSforCSSPlugin ( options ) {
1313 const defaults = {
14- preview : false , // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit)
15- cacheOnWatch : false , // OPTIONAL - {Boolean} - Whether it should cache the JS filenames that should be omitted on watch
16- verbose : false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted
14+ preview : false , // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit)
15+ cacheOnWatch : false , // OPTIONAL - {Boolean} - Whether it should cache the JS filenames that should be omitted on watch
16+ verbose : false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted
1717 } ;
18-
19- if ( typeof options !== " undefined" && ( options === null || typeof options !== " object" ) || Array . isArray ( options ) ) {
18+
19+ if ( ( typeof options !== ' undefined' && ( options === null || typeof options !== ' object' ) ) || Array . isArray ( options ) ) {
2020 throw new Error ( 'OmitJSforCSSPlugin only takes an options "object" as an argument' ) ;
2121 }
2222
2323 this . options = Object . assign ( { } , defaults , options || { } ) ;
2424 this . cacheOmittedFilename = [ ] ;
25- } ;
25+ }
2626
2727/**
2828 * @function omitFiles
2929 * @param {Object } omitted - The omitted file's details
3030 * @param {Object } compilation
3131 */
32- OmitJSforCSSPlugin . prototype . omitFiles = function ( omitted , compilation ) {
33- if ( this . options . preview ) {
34- console . log ( chalk . bold ( chalk . red ( 'PREVIEW' ) ) + chalk . grey ( ' File to be omitted for ' ) + chalk . bold ( chalk . green ( omitted . chunkName ) ) + ' : ' + chalk . bold ( chalk . green ( omitted . filename ) ) ) ;
35- }
36- else {
37- this . options . verbose && console . log ( chalk . grey ( 'File Omitted for ' ) + chalk . bold ( chalk . green ( omitted . chunkName ) ) + chalk . grey ( ' : ' ) + chalk . bold ( chalk . green ( omitted . filename ) ) ) ;
38- delete compilation . assets [ omitted . filename ] ;
39- }
32+ OmitJSforCSSPlugin . prototype . omitFiles = function ( omitted , compilation ) {
33+ if ( this . options . preview ) {
34+ console . log (
35+ chalk . bold ( chalk . red ( 'PREVIEW' ) ) + chalk . grey ( ' File to be omitted for ' ) + chalk . bold ( chalk . green ( omitted . chunkName ) ) + ' : ' + chalk . bold ( chalk . green ( omitted . filename ) )
36+ ) ;
37+ } else {
38+ this . options . verbose &&
39+ console . log ( chalk . grey ( 'File Omitted for ' ) + chalk . bold ( chalk . green ( omitted . chunkName ) ) + chalk . grey ( ' : ' ) + chalk . bold ( chalk . green ( omitted . filename ) ) ) ;
40+ delete compilation . assets [ omitted . filename ] ;
41+ }
4042} ;
4143
4244/**
4345 * @function findOmissibleFiles
4446 * @param {Object } compilation
4547 */
46- OmitJSforCSSPlugin . prototype . findOmissibleFiles = function ( compilation ) {
48+ OmitJSforCSSPlugin . prototype . findOmissibleFiles = function ( compilation ) {
4749 // Every chunk / entry point
48- compilation . chunks . forEach ( ( chunk ) => {
50+ compilation . chunks . forEach ( chunk => {
4951 // Chunks origin files. ex. origin entry point, ![] entry
5052 let resourceOrigin = { } ;
51- let assetTypeCount = { internal : 0 , css : 0 } ;
53+ let assetTypeCount = { internal : 0 , css : 0 } ;
5254
53- chunk . origins . forEach ( ( origin ) => {
54- if ( typeof origin . module . resource === " string" ) {
55+ chunk . origins . forEach ( origin => {
56+ if ( typeof origin . module . resource === ' string' ) {
5557 resourceOrigin [ origin . module . resource ] = true ;
5658 }
5759 } ) ;
5860
5961 // Each entry point will have its own dependencies, based on the files inner deps or the array deps in entry
60- chunk . modules . forEach ( ( module ) => {
61- if ( ! Array . isArray ( module . fileDependencies ) ) {
62- return ;
62+ chunk . modules . forEach ( module => {
63+ if ( ! Array . isArray ( module . fileDependencies ) ) {
64+ return ;
65+ }
66+ module . fileDependencies . forEach ( filepath => {
67+ if ( ! resourceOrigin [ filepath ] && ! / ( \b n o d e _ m o d u l e s \b ) / . test ( filepath ) ) {
68+ / \. ( c s s ) $ / i. test ( filepath ) ? assetTypeCount . css ++ : assetTypeCount . internal ++ ;
6369 }
64- module . fileDependencies . forEach ( ( filepath ) => {
65- if ( ! resourceOrigin [ filepath ] && ! ( / ( \b n o d e _ m o d u l e s \b ) / ) . test ( filepath ) ) {
66- ( / \. ( c s s ) $ / i) . test ( filepath ) ? assetTypeCount . css ++ : assetTypeCount . internal ++ ;
67- }
68- } ) ;
70+ } ) ;
6971 } ) ;
7072
7173 // Get the filenames that will be emitted, generated by the chunk, and omit JS if applicable
72- chunk . files . forEach ( ( filename ) => {
74+ chunk . files . forEach ( filename => {
7375 // If all dependencies of this entry were CSS, then a JS version of this file will be created
7476 // This js file will be empty due to extract-text-webpack-plugin
75- if ( ( assetTypeCount . css > 0 && assetTypeCount . internal === 0 ) && ( ( / \. ( j s ) $ / i) . test ( filename ) || ( / \. ( j s ) .m a p $ / i) . test ( filename ) ) ) {
77+ if ( assetTypeCount . css > 0 && assetTypeCount . internal === 0 && ( / \. ( j s ) $ / i. test ( filename ) || / \. ( j s ) .m a p $ / i. test ( filename ) ) ) {
7678 let omitted = {
77- ' filename' : filename ,
78- ' chunkName' : chunk . name
79+ filename : filename ,
80+ chunkName : chunk . name
7981 } ;
8082
8183 this . cacheOmittedFilename . push ( omitted ) ;
8284 this . omitFiles ( omitted , compilation ) ;
8385 }
8486 } ) ;
85-
8687 } ) ;
8788} ;
8889
@@ -92,15 +93,15 @@ OmitJSforCSSPlugin.prototype.findOmissibleFiles = function(compilation){
9293 */
9394OmitJSforCSSPlugin . prototype . apply = function ( compiler ) {
9495 compiler . plugin ( 'emit' , ( compilation , callback ) => {
95-
96- if ( this . options . cacheOnWatch && this . cacheOmittedFilename . length ) {
97- this . cacheOmittedFilename . forEach ( ( omitted ) => { this . omitFiles ( omitted , compilation ) } ) ;
98- }
99- else {
96+ if ( this . options . cacheOnWatch && this . cacheOmittedFilename . length ) {
97+ this . cacheOmittedFilename . forEach ( omitted => {
98+ this . omitFiles ( omitted , compilation ) ;
99+ } ) ;
100+ } else {
100101 this . findOmissibleFiles ( compilation ) ;
101102 }
102103 callback ( ) ;
103104 } ) ;
104105} ;
105106
106- module . exports = OmitJSforCSSPlugin ;
107+ module . exports = OmitJSforCSSPlugin ;
0 commit comments