Skip to content

Instantly share code, notes, and snippets.

@agrberg
Created April 11, 2020 16:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrberg/53f7ccc61f35e3af28f53d19eef59675 to your computer and use it in GitHub Desktop.
Save agrberg/53f7ccc61f35e3af28f53d19eef59675 to your computer and use it in GitHub Desktop.
Getting webp images and autogeneration working in Rails 6
const IMAGE_REGEXP = /\.(?:jpe?g|png)$/; // https://regexper.com/#%2F%5C.%28%3F%3Ajpe%3Fg%7Cpng%29%24%2F
const ImageminWebpWebpackPlugin = require('imagemin-webp-webpack-plugin');
const { environment } = require('@rails/webpacker');
environment.plugins.prepend('ImageminWebpWebpackPlugin', new ImageminWebpWebpackPlugin({
silent: false,
detailedLogs: true,
}));
const manifest = environment.plugins.get('Manifest')
manifest.hooks.customize.tap('ImageminWebpWebpackPlugin', (entry, _original, manifest, _asset) => {
const { key, value } = entry
if (IMAGE_REGEXP.test(key)) {
manifest.set(key.replace(IMAGE_REGEXP, '.webp'), value.replace(IMAGE_REGEXP, '.webp'))
}
});
module.exports = environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment