From 24c60c0a61ad0d21961799230131dcd764fa206d Mon Sep 17 00:00:00 2001 From: mricoul Date: Wed, 11 Sep 2024 11:40:56 +0200 Subject: [PATCH] feat (Svg): add hash to filename --- config/loaders.js | 2 +- config/plugins.js | 6 +++--- inc/Services/Svg.php | 14 +++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/loaders.js b/config/loaders.js index 6f2e53da..218c9ca6 100644 --- a/config/loaders.js +++ b/config/loaders.js @@ -112,7 +112,7 @@ module.exports = { options: { extract: true, publicPath: 'icons/', - spriteFilename: (svgPath) => `${/icons([\\|/])(.*?)\1/gm.exec(svgPath)[2]}.svg`, + spriteFilename: (svgPath) => `${/icons([\\|/])(.*?)\1/gm.exec(svgPath)[2]}.[hash].svg`, symbolId: (filePath) => `icon-${path.basename(filePath).slice(0, -4)}`, }, }, diff --git a/config/plugins.js b/config/plugins.js index 275aba60..671707d7 100644 --- a/config/plugins.js +++ b/config/plugins.js @@ -37,6 +37,9 @@ module.exports = { new WebpackImageSizesPlugin({ watch: mode !== 'production', }), + new WebpackManifestPlugin({ + fileName: 'assets.json', + }), ] if (mode === 'production') { @@ -47,9 +50,6 @@ module.exports = { }) ) plugins.push( - new WebpackManifestPlugin({ - fileName: 'assets.json', - }), new MiniCssExtractPlugin({ filename: '[name].[contenthash:8].min.css', }) diff --git a/inc/Services/Svg.php b/inc/Services/Svg.php index 73207e9e..a77df3f0 100644 --- a/inc/Services/Svg.php +++ b/inc/Services/Svg.php @@ -2,6 +2,7 @@ namespace BEA\Theme\Framework\Services; +use BEA\Theme\Framework\Framework; use BEA\Theme\Framework\Service; use BEA\Theme\Framework\Service_Container; @@ -11,11 +12,16 @@ * @package BEA\Theme\Framework */ class Svg implements Service { + /** + * @var Assets; + */ + private $assets; /** * @param Service_Container $container */ public function register( Service_Container $container ): void { + $this->assets = Framework::get_container()->get_service( 'assets' ); add_filter( 'wp_kses_allowed_html', [ $this, 'allow_svg_tag' ] ); } @@ -54,12 +60,18 @@ public function get_the_icon( string $icon_class, array $additionnal_classes = [ $icon_class = substr( $icon_class, strpos( $icon_class, '/' ) + 1 ); } + $sprite_path = $this->assets->get_min_file( 'icons/' . $sprite_name . '.svg' ); + + if ( ! file_exists( \get_theme_file_path( '/dist/' . $sprite_path ) ) ) { + return ''; + } + $icon_slug = strpos( $icon_class, 'icon-' ) === 0 ? $icon_class : sprintf( 'icon-%s', $icon_class ); $classes = [ 'icon', $icon_slug ]; $classes = array_merge( $classes, $additionnal_classes ); $classes = array_map( 'sanitize_html_class', $classes ); - return sprintf( '', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/icons/%s.svg', $sprite_name ) ), $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + return sprintf( '', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/%s', $sprite_path ) ), $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /**