Skip to content

Commit e5cea97

Browse files
committed
fix (theme): prevent webpack image sizes plugin to called too many times
1 parent cf0ec83 commit e5cea97

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

config/webpack-image-sizes-plugin.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,38 @@ class WebpackImageSizesPlugin {
5454
* @memberof WebpackImageSizesPlugin
5555
*/
5656
apply(compiler) {
57+
const context = compiler.context
58+
const confImgPath = path.resolve(context, this.options.confImgPath)
59+
const sizesPath = path.join(confImgPath, this.options.sizesSubdir)
60+
const tplPath = path.join(confImgPath, this.options.tplSubdir)
61+
5762
// Execute on first build and each rebuild
5863
const runGeneration = async (compilation, callback) => {
5964
try {
60-
const confImgPath = path.resolve(compiler.context, this.options.confImgPath)
61-
const sizesPath = path.join(confImgPath, this.options.sizesSubdir)
62-
const tplPath = path.join(confImgPath, this.options.tplSubdir)
65+
let hasChanges = false
66+
67+
// Check if there are any changes in the conf-img directory
68+
// Assumes that no modified files means the start of the build (yarn start || yarn build)
69+
if (WebpackImageSizesPlugin.hasBeenBuiltOnce && compilation.modifiedFiles) {
70+
for (const filePath of compilation.modifiedFiles) {
71+
console.log(this.options.confImgPath, filePath, filePath.includes(this.options.confImgPath))
72+
if (filePath.includes(this.options.confImgPath)) {
73+
hasChanges = true
74+
}
75+
}
76+
}
77+
78+
if (WebpackImageSizesPlugin.hasBeenBuiltOnce && !hasChanges) {
79+
console.log(`✅ No changes detected in ${this.options.confImgPath}`)
80+
81+
if (callback) {
82+
callback()
83+
}
84+
85+
return
86+
}
87+
88+
WebpackImageSizesPlugin.hasBeenBuiltOnce = true
6389

6490
console.log('🔧 Starting WebpackImageSizesPlugin generation...')
6591

@@ -83,7 +109,7 @@ class WebpackImageSizesPlugin {
83109

84110
// Generate default images if option is enabled
85111
if (this.options.generateDefaultImages) {
86-
await this.generateDefaultImages(compiler.context, imageSizes)
112+
await this.generateDefaultImages(context, imageSizes)
87113
}
88114

89115
if (callback) {
@@ -101,17 +127,13 @@ class WebpackImageSizesPlugin {
101127
compiler.hooks.emit.tapAsync('WebpackImageSizesPlugin', runGeneration)
102128

103129
// Hook for rebuilds in watch mode
104-
compiler.hooks.watchRun.tapAsync('WebpackImageSizesPlugin', (compiler, callback) => {
130+
compiler.hooks.watchRun.tapAsync('WebpackImageSizesPlugin', (compilation, callback) => {
105131
console.log('👀 Watch mode: checking for conf-img changes...')
106-
runGeneration(null, callback)
132+
runGeneration(compilation, callback)
107133
})
108134

109135
// Add directories to watch
110136
compiler.hooks.afterEnvironment.tap('WebpackImageSizesPlugin', () => {
111-
const confImgPath = path.resolve(compiler.context, this.options.confImgPath)
112-
const sizesPath = path.join(confImgPath, this.options.sizesSubdir)
113-
const tplPath = path.join(confImgPath, this.options.tplSubdir)
114-
115137
// Add directories to watched dependencies
116138
compiler.hooks.compilation.tap('WebpackImageSizesPlugin', (compilation) => {
117139
// Watch configuration directories
@@ -506,4 +528,12 @@ class WebpackImageSizesPlugin {
506528
}
507529
}
508530

531+
// ----
532+
// static properties
533+
// ----
534+
WebpackImageSizesPlugin.hasBeenBuiltOnce = false
535+
536+
// ----
537+
// export
538+
// ----
509539
module.exports = WebpackImageSizesPlugin

src/scss/03-base/_fonts.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* ...
2626
*/
2727

28-
@use "node_modules/@fontsource/poppins/scss/mixins" as Poppins;
28+
@use "../../../node_modules/@fontsource/poppins/scss/mixins" as Poppins;
2929

3030
@include Poppins.faces($weights: (300, 400, 500, 700), $styles: normal);
3131
@include Poppins.faces($weights: (300, 400, 500, 700), $styles: italic);

0 commit comments

Comments
 (0)