Skip to content

Commit c837367

Browse files
authored
Merge pull request #463 from BeAPI/fix/webpack-perf
webpack image sizes plugin
2 parents e47a307 + fe18efa commit c837367

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

config/webpack-image-sizes-plugin.js

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ class WebpackImageSizesPlugin {
3535
*/
3636
constructor(options = {}) {
3737
this.options = {
38-
confImgPath: options.confImgPath || 'assets/conf-img',
39-
sizesSubdir: options.sizesSubdir || 'sizes',
40-
tplSubdir: options.tplSubdir || 'tpl',
41-
outputImageLocations: options.outputImageLocations || 'image-locations.json',
42-
outputImageSizes: options.outputImageSizes || 'image-sizes.json',
43-
generateDefaultImages: options.generateDefaultImages || false,
44-
defaultImageSource: options.defaultImageSource || 'src/img/static/default.jpg',
45-
defaultImagesOutputDir: options.defaultImagesOutputDir || 'dist/images',
46-
defaultImageFormat: options.defaultImageFormat || 'jpg',
47-
silence: options.silence || false,
38+
confImgPath: 'assets/conf-img',
39+
sizesSubdir: 'sizes',
40+
tplSubdir: 'tpl',
41+
outputImageLocations: 'image-locations.json',
42+
outputImageSizes: 'image-sizes.json',
43+
generateDefaultImages: false,
44+
defaultImageSource: 'src/img/static/default.jpg',
45+
defaultImagesOutputDir: 'dist/images',
46+
defaultImageFormat: 'jpg',
47+
silence: false,
4848
...options,
4949
}
50+
51+
this.hasBeenBuiltOnce = false
5052
}
5153

5254
/**
@@ -70,12 +72,37 @@ class WebpackImageSizesPlugin {
7072
* @memberof WebpackImageSizesPlugin
7173
*/
7274
apply(compiler) {
75+
const { context } = compiler
76+
const confImgPath = path.resolve(context, this.options.confImgPath)
77+
const sizesPath = path.join(confImgPath, this.options.sizesSubdir)
78+
const tplPath = path.join(confImgPath, this.options.tplSubdir)
79+
7380
// Execute on first build and each rebuild
7481
const runGeneration = async (compilation, callback) => {
7582
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)
83+
let hasChanges = false
84+
85+
// Check if there are any changes in the conf-img directory
86+
// Assumes that no modified files means the start of the build (yarn start || yarn build)
87+
if (this.hasBeenBuiltOnce && compilation.modifiedFiles) {
88+
for (const filePath of compilation.modifiedFiles) {
89+
if (filePath.includes(this.options.confImgPath)) {
90+
hasChanges = true
91+
}
92+
}
93+
}
94+
95+
if (this.hasBeenBuiltOnce && !hasChanges) {
96+
this.log('log', `✅ No changes detected in ${this.options.confImgPath}`)
97+
98+
if (callback) {
99+
callback()
100+
}
101+
102+
return
103+
}
104+
105+
this.hasBeenBuiltOnce = true
79106

80107
this.log('log', '🔧 Starting WebpackImageSizesPlugin generation...')
81108

@@ -99,7 +126,7 @@ class WebpackImageSizesPlugin {
99126

100127
// Generate default images if option is enabled
101128
if (this.options.generateDefaultImages) {
102-
await this.generateDefaultImages(compiler.context, imageSizes)
129+
await this.generateDefaultImages(context, imageSizes)
103130
}
104131

105132
if (callback) {
@@ -119,27 +146,20 @@ class WebpackImageSizesPlugin {
119146
// Hook for rebuilds in watch mode
120147
compiler.hooks.watchRun.tapAsync('WebpackImageSizesPlugin', (compiler, callback) => {
121148
this.log('log', '👀 Watch mode: checking for conf-img changes...')
122-
runGeneration(null, callback)
149+
runGeneration(compiler, callback)
123150
})
124151

125152
// Add directories to watch
126-
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-
131-
// Add directories to watched dependencies
132-
compiler.hooks.compilation.tap('WebpackImageSizesPlugin', (compilation) => {
133-
// Watch configuration directories
134-
if (fs.existsSync(sizesPath)) {
135-
compilation.contextDependencies.add(sizesPath)
136-
this.log('log', '📁 Added sizes directory to watch dependencies')
137-
}
138-
if (fs.existsSync(tplPath)) {
139-
compilation.contextDependencies.add(tplPath)
140-
this.log('log', '📁 Added tpl directory to watch dependencies')
141-
}
142-
})
153+
compiler.hooks.compilation.tap('WebpackImageSizesPlugin', (compilation) => {
154+
// Watch configuration directories
155+
if (fs.existsSync(sizesPath)) {
156+
compilation.contextDependencies.add(sizesPath)
157+
this.log('log', '📁 Added sizes directory to watch dependencies')
158+
}
159+
if (fs.existsSync(tplPath)) {
160+
compilation.contextDependencies.add(tplPath)
161+
this.log('log', '📁 Added tpl directory to watch dependencies')
162+
}
143163
})
144164
}
145165

@@ -523,4 +543,7 @@ class WebpackImageSizesPlugin {
523543
}
524544
}
525545

546+
// ----
547+
// export
548+
// ----
526549
module.exports = WebpackImageSizesPlugin

0 commit comments

Comments
 (0)