Skip to content

Commit 5f5a2e3

Browse files
committed
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.
1 parent 5f2bb00 commit 5f5a2e3

3 files changed

Lines changed: 35 additions & 54 deletions

File tree

config/plugins.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ module.exports = {
5252
defaultImageFormat: 'jpg', // Generated image format (jpg, png, webp, avif)
5353
silence: true, // Suppress console output
5454
}),
55+
new MiniCssExtractPlugin({
56+
filename: '[name].css',
57+
}),
58+
new WebpackManifestPlugin({
59+
fileName: 'assets.json',
60+
}),
5561
]
5662

5763
if (mode === 'production') {
@@ -61,20 +67,6 @@ module.exports = {
6167
generateStatsFile: true,
6268
})
6369
)
64-
plugins.push(
65-
new WebpackManifestPlugin({
66-
fileName: 'assets.json',
67-
}),
68-
new MiniCssExtractPlugin({
69-
filename: '[name].[contenthash:8].min.css',
70-
})
71-
)
72-
} else {
73-
plugins.push(
74-
new MiniCssExtractPlugin({
75-
filename: '[name].css',
76-
})
77-
)
7870
}
7971

8072
return plugins

config/webpack.prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = merge(common, {
88
mode: mode,
99
stats: 'minimal',
1010
output: {
11-
filename: '[name]-min.js',
11+
filename: '[name].js',
1212
},
1313
optimization: {
1414
concatenateModules: true,

inc/Services/Assets.php

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ public function register_assets(): void {
5555
if ( is_admin() ) {
5656
return;
5757
}
58-
$theme = wp_get_theme();
5958

60-
// Do not add a versioning query param in assets URLs if minified
61-
$version = $this->is_minified() ? null : $theme->get( 'Version' );
59+
$theme = wp_get_theme();
6260

63-
// Js
64-
$file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js';
61+
// JavaScript
62+
$file = $this->get_min_file( 'js' );
6563
$asset_data = $this->get_asset_data( $file );
6664
$this->assets_tools->register_script(
6765
'scripts',
@@ -80,40 +78,39 @@ public function register_assets(): void {
8078
),
8179
);
8280

83-
// CSS
84-
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version );
81+
// Styles
82+
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $theme->get( 'Version' ) );
8583
}
8684

8785
/**
8886
* Enqueue the scripts
8987
*/
9088
public function enqueue_scripts(): void {
91-
// JS
89+
// JavaScript
9290
$this->assets_tools->enqueue_script( 'scripts' );
9391
}
9492

9593
/**
9694
* Enqueue the styles
9795
*/
9896
public function enqueue_styles(): void {
99-
// CSS
97+
// Styles
10098
$this->assets_tools->enqueue_style( 'theme-style' );
10199
}
102100

103101
/**
104-
* The stylesheet uri based on the dev or not constant
102+
* Point the theme stylesheet to the built CSS in `dist/` when `assets.json` is present.
105103
*
106-
* @param string $stylesheet_uri
104+
* @param string $stylesheet_uri Default theme stylesheet URI.
107105
*
108106
* @return string
109107
* @author Nicolas Juen
110108
*/
111109
public function stylesheet_uri( string $stylesheet_uri ): string {
112-
if ( $this->is_minified() ) {
113-
$file = $this->get_min_file( 'css' );
114-
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
115-
return \get_theme_file_uri( '/dist/' . $file );
116-
}
110+
$file = $this->get_min_file( 'css' );
111+
112+
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
113+
return \get_theme_file_uri( '/dist/' . $file );
117114
}
118115

119116
if ( file_exists( \get_theme_file_path( '/dist/app.css' ) ) ) {
@@ -124,9 +121,9 @@ public function stylesheet_uri( string $stylesheet_uri ): string {
124121
}
125122

126123
/**
127-
* Return JS/CSS .min file based on assets.json
124+
* Return the compiled asset filename for a type from `assets.json`.
128125
*
129-
* @param string $type
126+
* @param string $type Asset type key (e.g. `js`, `css`, `editor.js`).
130127
*
131128
* @return string
132129
*/
@@ -185,7 +182,7 @@ public function get_min_file( string $type ): string {
185182
* Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of
186183
* dependencies use by the asset and a hash representing the current version of the asset.
187184
*
188-
* @param string $file The asset name including its extension, eg: app.js, app-min.js
185+
* @param string $file The asset name including its extension, e.g. `app.js`.
189186
*
190187
* @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys.
191188
*/
@@ -202,37 +199,29 @@ public function get_asset_data( string $file ): array {
202199
return $empty_asset_data;
203200
}
204201

205-
if ( isset( $cache_data[ $file ] ) ) {
206-
return $cache_data[ $file ];
202+
$cache_key = $file;
203+
204+
if ( isset( $cache_data[ $cache_key ] ) ) {
205+
return $cache_data[ $cache_key ];
207206
}
208207

209-
$filename = strtok( $file, '.' );
210-
$file = sprintf( '/dist/%s.asset.php', $filename );
211-
if ( ! file_exists( \get_theme_file_path( $file ) ) ) {
212-
$cache_data[ $file ] = $empty_asset_data;
213-
return $cache_data[ $file ];
208+
$filename = strtok( $file, '.' );
209+
$asset_php = sprintf( '/dist/%s.asset.php', $filename );
210+
if ( ! file_exists( \get_theme_file_path( $asset_php ) ) ) {
211+
$cache_data[ $cache_key ] = $empty_asset_data;
212+
return $cache_data[ $cache_key ];
214213
}
215214

216-
$cache_data[ $file ] = require \get_theme_file_path( $file );
215+
$cache_data[ $cache_key ] = require \get_theme_file_path( $asset_php );
217216

218-
return $cache_data[ $file ];
219-
}
220-
221-
/**
222-
* Check if we are on minified environment.
223-
*
224-
* @return bool
225-
* @author Nicolas JUEN
226-
*/
227-
public function is_minified(): bool {
228-
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false );
217+
return $cache_data[ $cache_key ];
229218
}
230219

231220
/**
232221
* Change login CSS URL
233222
* @return string
234223
*/
235224
public function login_stylesheet_uri(): string {
236-
return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css';
225+
return 'dist/' . $this->get_min_file( 'login' );
237226
}
238227
}

0 commit comments

Comments
 (0)