Please note that this is for Sage 9 (no Laravel mixes used):
webpack.config.js
:
module: {
rules: [
{
test: /\.(jpe?g|png)$/i,
use: [{
loader: 'responsive-loader-webp',
options: {
adapter: require('responsive-loader-webp/sharp'),
quality: 100, // (default)
name: '[path][name]-[width].[ext]',
},
}],
},
webpack.config.optimize.js
:
plugins: [
new ImageminPlugin({
cacheFolder: resolve('./.cache'),
test: '!images/layout/header*', // excludes header image (responsive-loader)
optipng: {
optimizationLevel: 7,
},
gifsicle: { optimizationLevel: 3 },
pngquant: {
quality: '65-90',
speed: 1,
},
svgo: {
plugins: [
{ removeUnknownsAndDefaults: false },
{ cleanupIDs: false },
{ removeViewBox: false },
],
},
plugins: [imageminMozjpeg({
quality: 97, // (high quality default)
})],
disable: (config.enabled.watcher),
}),
new ImageminPlugin({
cacheFolder: resolve('./.cache'),
test: 'images/layout/header*', // only header image from responsive-loader (sharp)
plugins: [
imageminMozjpeg({
quality: 92, // (acceptable quality for header image)
}),
],
disable: (config.enabled.watcher),
}),
Install the npm sharp
package and the maintained responsive-images
fork with new package name responsive-images-loader
:
$ npm install --save-dev sharp responsive-images-loader
Then you have to trigger that loader. Currently I have to do this using a separate SCSS file and a loop.
It would be much better of course if webpack could use the blade.php template files for this.
Then you have to generate the img markup. Honestly, this is still tedious…