@@ -70,12 +70,38 @@ class WebpackImageSizesPlugin {
7070 * @memberof WebpackImageSizesPlugin
7171 */
7272 apply ( compiler ) {
73+ const context = compiler . context
74+ const confImgPath = path . resolve ( context , this . options . confImgPath )
75+ const sizesPath = path . join ( confImgPath , this . options . sizesSubdir )
76+ const tplPath = path . join ( confImgPath , this . options . tplSubdir )
77+
7378 // Execute on first build and each rebuild
7479 const runGeneration = async ( compilation , callback ) => {
7580 try {
76- const confImgPath = path . resolve ( compiler . context , this . options . confImgPath )
77- const sizesPath = path . join ( confImgPath , this . options . sizesSubdir )
78- const tplPath = path . join ( confImgPath , this . options . tplSubdir )
81+ let hasChanges = false
82+
83+ // Check if there are any changes in the conf-img directory
84+ // Assumes that no modified files means the start of the build (yarn start || yarn build)
85+ if ( WebpackImageSizesPlugin . hasBeenBuiltOnce && compilation . modifiedFiles ) {
86+ for ( const filePath of compilation . modifiedFiles ) {
87+ console . log ( this . options . confImgPath , filePath , filePath . includes ( this . options . confImgPath ) )
88+ if ( filePath . includes ( this . options . confImgPath ) ) {
89+ hasChanges = true
90+ }
91+ }
92+ }
93+
94+ if ( WebpackImageSizesPlugin . hasBeenBuiltOnce && ! hasChanges ) {
95+ console . log ( `✅ No changes detected in ${ this . options . confImgPath } ` )
96+
97+ if ( callback ) {
98+ callback ( )
99+ }
100+
101+ return
102+ }
103+
104+ WebpackImageSizesPlugin . hasBeenBuiltOnce = true
79105
80106 this . log ( 'log' , '🔧 Starting WebpackImageSizesPlugin generation...' )
81107
@@ -99,7 +125,7 @@ class WebpackImageSizesPlugin {
99125
100126 // Generate default images if option is enabled
101127 if ( this . options . generateDefaultImages ) {
102- await this . generateDefaultImages ( compiler . context , imageSizes )
128+ await this . generateDefaultImages ( context , imageSizes )
103129 }
104130
105131 if ( callback ) {
@@ -117,17 +143,13 @@ class WebpackImageSizesPlugin {
117143 compiler . hooks . emit . tapAsync ( 'WebpackImageSizesPlugin' , runGeneration )
118144
119145 // Hook for rebuilds in watch mode
120- compiler . hooks . watchRun . tapAsync ( 'WebpackImageSizesPlugin' , ( compiler , callback ) => {
146+ compiler . hooks . watchRun . tapAsync ( 'WebpackImageSizesPlugin' , ( compilation , callback ) => {
121147 this . log ( 'log' , '👀 Watch mode: checking for conf-img changes...' )
122- runGeneration ( null , callback )
148+ runGeneration ( compilation , callback )
123149 } )
124150
125151 // Add directories to watch
126152 compiler . hooks . afterEnvironment . tap ( 'WebpackImageSizesPlugin' , ( ) => {
127- const confImgPath = path . resolve ( compiler . context , this . options . confImgPath )
128- const sizesPath = path . join ( confImgPath , this . options . sizesSubdir )
129- const tplPath = path . join ( confImgPath , this . options . tplSubdir )
130-
131153 // Add directories to watched dependencies
132154 compiler . hooks . compilation . tap ( 'WebpackImageSizesPlugin' , ( compilation ) => {
133155 // Watch configuration directories
@@ -523,4 +545,12 @@ class WebpackImageSizesPlugin {
523545 }
524546}
525547
548+ // ----
549+ // static properties
550+ // ----
551+ WebpackImageSizesPlugin . hasBeenBuiltOnce = false
552+
553+ // ----
554+ // export
555+ // ----
526556module . exports = WebpackImageSizesPlugin
0 commit comments