From 5f5a2e37c707ccf4e77b289c95266b1881511925 Mon Sep 17 00:00:00 2001 From: Milan Ricoul Date: Fri, 24 Apr 2026 16:06:19 +0200 Subject: [PATCH 1/3] refactor(assets): align dist filenames with webpack manifest - Use stable output names in production (app.js, [name].css) and same MiniCssExtract + WebpackManifestPlugin in all modes. - Register theme JS/CSS from assets.json only; remove is_minified() and SCRIPT_DEBUG-based branching. - Fix get_asset_data() static cache to key by the requested filename. - Update PHPDoc and inline comments to match the new flow. --- config/plugins.js | 20 ++++-------- config/webpack.prod.js | 2 +- inc/Services/Assets.php | 67 +++++++++++++++++------------------------ 3 files changed, 35 insertions(+), 54 deletions(-) diff --git a/config/plugins.js b/config/plugins.js index dd5858ee..f4ee382f 100644 --- a/config/plugins.js +++ b/config/plugins.js @@ -52,6 +52,12 @@ module.exports = { defaultImageFormat: 'jpg', // Generated image format (jpg, png, webp, avif) silence: true, // Suppress console output }), + new MiniCssExtractPlugin({ + filename: '[name].css', + }), + new WebpackManifestPlugin({ + fileName: 'assets.json', + }), ] if (mode === 'production') { @@ -61,20 +67,6 @@ module.exports = { generateStatsFile: true, }) ) - plugins.push( - new WebpackManifestPlugin({ - fileName: 'assets.json', - }), - new MiniCssExtractPlugin({ - filename: '[name].[contenthash:8].min.css', - }) - ) - } else { - plugins.push( - new MiniCssExtractPlugin({ - filename: '[name].css', - }) - ) } return plugins diff --git a/config/webpack.prod.js b/config/webpack.prod.js index 4c0cd0fd..79b06500 100644 --- a/config/webpack.prod.js +++ b/config/webpack.prod.js @@ -8,7 +8,7 @@ module.exports = merge(common, { mode: mode, stats: 'minimal', output: { - filename: '[name]-min.js', + filename: '[name].js', }, optimization: { concatenateModules: true, diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index 1933699b..267f88b3 100644 --- a/inc/Services/Assets.php +++ b/inc/Services/Assets.php @@ -55,13 +55,11 @@ public function register_assets(): void { if ( is_admin() ) { return; } - $theme = wp_get_theme(); - // Do not add a versioning query param in assets URLs if minified - $version = $this->is_minified() ? null : $theme->get( 'Version' ); + $theme = wp_get_theme(); - // Js - $file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js'; + // JavaScript + $file = $this->get_min_file( 'js' ); $asset_data = $this->get_asset_data( $file ); $this->assets_tools->register_script( 'scripts', @@ -80,15 +78,15 @@ public function register_assets(): void { ), ); - // CSS - wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version ); + // Styles + wp_register_style( 'theme-style', get_stylesheet_uri(), [], $theme->get( 'Version' ) ); } /** * Enqueue the scripts */ public function enqueue_scripts(): void { - // JS + // JavaScript $this->assets_tools->enqueue_script( 'scripts' ); } @@ -96,24 +94,23 @@ public function enqueue_scripts(): void { * Enqueue the styles */ public function enqueue_styles(): void { - // CSS + // Styles $this->assets_tools->enqueue_style( 'theme-style' ); } /** - * The stylesheet uri based on the dev or not constant + * Point the theme stylesheet to the built CSS in `dist/` when `assets.json` is present. * - * @param string $stylesheet_uri + * @param string $stylesheet_uri Default theme stylesheet URI. * * @return string * @author Nicolas Juen */ public function stylesheet_uri( string $stylesheet_uri ): string { - if ( $this->is_minified() ) { - $file = $this->get_min_file( 'css' ); - if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) { - return \get_theme_file_uri( '/dist/' . $file ); - } + $file = $this->get_min_file( 'css' ); + + if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) { + return \get_theme_file_uri( '/dist/' . $file ); } if ( file_exists( \get_theme_file_path( '/dist/app.css' ) ) ) { @@ -124,9 +121,9 @@ public function stylesheet_uri( string $stylesheet_uri ): string { } /** - * Return JS/CSS .min file based on assets.json + * Return the compiled asset filename for a type from `assets.json`. * - * @param string $type + * @param string $type Asset type key (e.g. `js`, `css`, `editor.js`). * * @return string */ @@ -185,7 +182,7 @@ public function get_min_file( string $type ): string { * Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of * dependencies use by the asset and a hash representing the current version of the asset. * - * @param string $file The asset name including its extension, eg: app.js, app-min.js + * @param string $file The asset name including its extension, e.g. `app.js`. * * @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys. */ @@ -202,30 +199,22 @@ public function get_asset_data( string $file ): array { return $empty_asset_data; } - if ( isset( $cache_data[ $file ] ) ) { - return $cache_data[ $file ]; + $cache_key = $file; + + if ( isset( $cache_data[ $cache_key ] ) ) { + return $cache_data[ $cache_key ]; } - $filename = strtok( $file, '.' ); - $file = sprintf( '/dist/%s.asset.php', $filename ); - if ( ! file_exists( \get_theme_file_path( $file ) ) ) { - $cache_data[ $file ] = $empty_asset_data; - return $cache_data[ $file ]; + $filename = strtok( $file, '.' ); + $asset_php = sprintf( '/dist/%s.asset.php', $filename ); + if ( ! file_exists( \get_theme_file_path( $asset_php ) ) ) { + $cache_data[ $cache_key ] = $empty_asset_data; + return $cache_data[ $cache_key ]; } - $cache_data[ $file ] = require \get_theme_file_path( $file ); + $cache_data[ $cache_key ] = require \get_theme_file_path( $asset_php ); - return $cache_data[ $file ]; - } - - /** - * Check if we are on minified environment. - * - * @return bool - * @author Nicolas JUEN - */ - public function is_minified(): bool { - return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ); + return $cache_data[ $cache_key ]; } /** @@ -233,6 +222,6 @@ public function is_minified(): bool { * @return string */ public function login_stylesheet_uri(): string { - return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css'; + return 'dist/' . $this->get_min_file( 'login' ); } } From f1b06967a614d52f980454e3054062ea779f46c1 Mon Sep 17 00:00:00 2001 From: mricoul Date: Fri, 24 Apr 2026 16:21:30 +0200 Subject: [PATCH 2/3] fix(Editor): remove is_minified function condition --- inc/Services/Editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Services/Editor.php b/inc/Services/Editor.php index 85e3729d..bba9385d 100644 --- a/inc/Services/Editor.php +++ b/inc/Services/Editor.php @@ -61,7 +61,7 @@ public function boot( Service_Container $container ): void { * editor style */ private function style(): void { - $file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.css' ) : 'editor.css'; + $file = $this->assets->get_min_file( 'editor.css' ); /** * Do not enqueue a inexistant file on admin From cb08f43f315d74b5beb66266268b491b8da6c848 Mon Sep 17 00:00:00 2001 From: mricoul Date: Fri, 24 Apr 2026 16:39:20 +0200 Subject: [PATCH 3/3] fix(Editor): remove is_minified function condition --- inc/Services/Editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Services/Editor.php b/inc/Services/Editor.php index bba9385d..a552c7a0 100644 --- a/inc/Services/Editor.php +++ b/inc/Services/Editor.php @@ -77,7 +77,7 @@ private function style(): void { * Editor script */ public function admin_editor_script(): void { - $file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.js' ) : 'editor.js'; + $file = $this->assets->get_min_file( 'editor.js' ); $filepath = 'dist/' . $file; if ( ! file_exists( get_theme_file_path( $filepath ) ) ) {