Skip to content

Commit 4898ab1

Browse files
committed
feat (config): update Webpack image sizes plugin
Introduces a new Webpack plugin to automatically generate `image-locations.json` and `image-sizes.json` based on JSON size definitions and TPL templates. This allows for dynamic image size management and srcset generation, improving image optimization and reducing manual configuration. The plugin supports default image generation and format conversion using Sharp.
1 parent 9312888 commit 4898ab1

File tree

10 files changed

+829
-590
lines changed

10 files changed

+829
-590
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"width": 500,
4+
"height": 500,
5+
"crop": true
6+
},
7+
{
8+
"width": 1000,
9+
"height": 1000,
10+
"crop": true
11+
}
12+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%%data-location%%
2+
<figure>
3+
<img %%src%% %%srcset%% %%attributes%% />
4+
<figcaption>%%caption%%</figcaption>
5+
</figure>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%%data-location%%
2+
<img %%src%% %%srcset%% %%attributes%% />

config/WebpackImageSizesPlugin.js

Lines changed: 0 additions & 266 deletions
This file was deleted.

config/plugins.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const WebpackBar = require('webpackbar')
99
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin')
1010
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
1111

12-
const WebpackImageSizesPlugin = require('./WebpackImageSizesPlugin')
12+
const WebpackImageSizesPlugin = require('./webpack-image-sizes-plugin')
1313

1414
module.exports = {
1515
get: function (mode) {
@@ -33,7 +33,15 @@ module.exports = {
3333
}),
3434
new DependencyExtractionWebpackPlugin(),
3535
new WebpackImageSizesPlugin({
36-
watch: mode !== 'production',
36+
confImgPath: 'assets/conf-img', // Path to the conf-img folder
37+
sizesSubdir: 'sizes', // Subdirectory containing the sizes JSON files
38+
tplSubdir: 'tpl', // Subdirectory containing TPL templates
39+
outputImageLocations: 'image-locations.json', // Output locations file name
40+
outputImageSizes: 'image-sizes.json', // Output sizes file name
41+
generateDefaultImages: true, // Generate default images
42+
defaultImageSource: 'src/img/static/default.jpg', // Source image for generation
43+
defaultImagesOutputDir: 'dist/images', // Default images output directory
44+
defaultImageFormat: 'jpg', // Generated image format (jpg, png, webp, avif)
3745
}),
3846
]
3947

0 commit comments

Comments
 (0)