Created
April 11, 2020 16:40
-
-
Save agrberg/53f7ccc61f35e3af28f53d19eef59675 to your computer and use it in GitHub Desktop.
Getting webp images and autogeneration working in Rails 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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