Skip to content

Commit 9406c70

Browse files
authored
Merge pull request #488 from BeAPI/refactor/assets-name
refactor(assets): align dist filenames with webpack manifest
2 parents 22828f9 + 1b44a96 commit 9406c70

4 files changed

Lines changed: 51 additions & 57 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: 42 additions & 40 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' ) ?: 'app.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,42 @@ 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 ];
217+
return $cache_data[ $cache_key ];
219218
}
220219

221220
/**
222-
* Check if we are on minified environment.
221+
* Point the login page stylesheet to the built CSS in `dist/` when available.
223222
*
224-
* @return bool
225-
* @author Nicolas JUEN
223+
* @param string $stylesheet_path Default path relative to the theme root (from `wp_login_page_theme_css`).
224+
*
225+
* @return string Path relative to the theme root.
226226
*/
227-
public function is_minified(): bool {
228-
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false );
229-
}
227+
public function login_stylesheet_uri( string $stylesheet_path = '' ): string {
228+
$file = $this->get_min_file( 'login' );
230229

231-
/**
232-
* Change login CSS URL
233-
* @return string
234-
*/
235-
public function login_stylesheet_uri(): string {
236-
return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css';
230+
if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) {
231+
return 'dist/' . $file;
232+
}
233+
234+
if ( file_exists( \get_theme_file_path( '/dist/login.css' ) ) ) {
235+
return 'dist/login.css';
236+
}
237+
238+
return $stylesheet_path;
237239
}
238240
}

inc/Services/Editor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function boot( Service_Container $container ): void {
6161
* editor style
6262
*/
6363
private function style(): void {
64-
$file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.css' ) : 'editor.css';
64+
$file = $this->assets->get_min_file( 'editor.css' ) ?: 'editor.css';
6565

6666
/**
6767
* Do not enqueue a inexistant file on admin
@@ -77,7 +77,7 @@ private function style(): void {
7777
* Editor script
7878
*/
7979
public function admin_editor_script(): void {
80-
$file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.js' ) : 'editor.js';
80+
$file = $this->assets->get_min_file( 'editor.js' ) ?: 'editor.js';
8181
$filepath = 'dist/' . $file;
8282

8383
if ( ! file_exists( get_theme_file_path( $filepath ) ) ) {

0 commit comments

Comments
 (0)