Skip to content

Commit 52c46ba

Browse files
authored
Merge pull request #278 from BeAPI/feature/wordpress-dependency-extraction
Intégration du plugin d'extraction des dépendances
2 parents 9e63a91 + 843cbed commit 52c46ba

File tree

13 files changed

+1861
-858
lines changed

13 files changed

+1861
-858
lines changed

.yarn/releases/yarn-3.2.1.cjs

Lines changed: 0 additions & 786 deletions
This file was deleted.

.yarn/releases/yarn-3.2.3.cjs

Lines changed: 783 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
nodeLinker: pnp
22

3-
yarnPath: .yarn/releases/yarn-3.2.1.cjs
3+
yarnPath: .yarn/releases/yarn-3.2.3.cjs

config/loaders.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = {
5151
options: {
5252
loader: 'js',
5353
target: 'es2016',
54+
legalComments: 'inline',
5455
},
5556
},
5657
},

config/plugins.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin')
77
const StyleLintPlugin = require('stylelint-webpack-plugin')
88
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin')
99
const WebpackBar = require('webpackbar')
10+
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin')
1011

1112
const browsersyncConfig = require('./browsersync.config')
1213

@@ -30,6 +31,7 @@ module.exports = {
3031
new WebpackBar({
3132
color: '#ffe600',
3233
}),
34+
new DependencyExtractionWebpackPlugin({ injectPolyfill: true }),
3335
]
3436

3537
if (mode === 'production') {

config/webpack.common.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const entries = require('./entries')
3+
const TerserPlugin = require('terser-webpack-plugin')
34

45
module.exports = {
56
entry: entries,
@@ -9,6 +10,25 @@ module.exports = {
910
publicPath: '',
1011
assetModuleFilename: 'assets/[hash][ext][query]',
1112
},
13+
optimization: {
14+
minimizer: [
15+
new TerserPlugin({
16+
parallel: true,
17+
terserOptions: {
18+
format: {
19+
comments: /translators:/i,
20+
},
21+
compress: {
22+
passes: 2,
23+
},
24+
mangle: {
25+
reserved: ['__', '_n', '_nx', '_x'],
26+
},
27+
},
28+
extractComments: false,
29+
}),
30+
],
31+
},
1232
externals: {
1333
jquery: 'window.jQuery',
1434
},

config/webpack.prod.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ module.exports = merge(common, {
88
mode: mode,
99
stats: 'minimal',
1010
output: {
11-
filename: '[name].[chunkhash:8].min.js',
11+
filename: '[name]-min.js',
12+
},
13+
optimization: {
14+
concatenateModules: true,
1215
},
1316
plugins: plugins.get(mode),
1417
module: {

inc/Services/Assets.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Assets implements Service {
1717

1818
/**
19-
* @var \BEA\Theme\Framework\Tools\Assets
19+
* @var Assets_Tools
2020
*/
2121
private $assets_tools;
2222

@@ -57,16 +57,19 @@ public function register_assets(): void {
5757
}
5858
$theme = wp_get_theme();
5959

60-
// Js theme
61-
// Theme js dependencies
62-
$scripts_dependencies = [ 'jquery' ];
63-
64-
// Async and footer
65-
$file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js';
66-
67-
// Do not add version if minified
60+
// Do not add a versioning query param in assets URLs if minified
6861
$version = $this->is_minified() ? null : $theme->get( 'Version' );
69-
$this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $version, true );
62+
63+
// Js
64+
$file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js';
65+
$asset_data = $this->get_asset_data( $file );
66+
$this->assets_tools->register_script(
67+
'scripts',
68+
'dist/' . $file,
69+
array_merge( [ 'jquery' ], $asset_data['dependencies'] ), // ensure jQuery dependency is set even if not declared explicitly in the JS
70+
$asset_data['version'],
71+
true
72+
);
7073

7174
// CSS
7275
wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version );
@@ -167,13 +170,52 @@ public function get_min_file( string $type ): string {
167170
return $file;
168171
}
169172

173+
/**
174+
* Retrieve data for a compiled asset file.
175+
*
176+
* Asset data are produced by the webpack dependencies extraction plugin. They contain for each asset the list of
177+
* dependencies use by the asset and a hash representing the current version of the asset.
178+
*
179+
* @param string $file The asset name including its extension, eg: app.js, app-min.js
180+
*
181+
* @return array{dependencies: string[], version:string} The asset data if available or an array with the default keys.
182+
*/
183+
public function get_asset_data( string $file ): array {
184+
static $cache_data;
185+
186+
$empty_asset_data = [
187+
'dependencies' => [],
188+
'version' => '',
189+
];
190+
191+
$file = trim( $file );
192+
if ( empty( $file ) ) {
193+
return $empty_asset_data;
194+
}
195+
196+
if ( isset( $cache_data[ $file ] ) ) {
197+
return $cache_data[ $file ];
198+
}
199+
200+
$filename = strtok( $file, '.' );
201+
$file = sprintf( '/dist/%s.asset.php', $filename );
202+
if ( ! file_exists( \get_theme_file_path( $file ) ) ) {
203+
$cache_data[ $file ] = $empty_asset_data;
204+
return $cache_data[ $file ];
205+
}
206+
207+
$cache_data[ $file ] = require \get_theme_file_path( $file );
208+
209+
return $cache_data[ $file ];
210+
}
211+
170212
/**
171213
* Check if we are on minified environment.
172214
*
173215
* @return bool
174216
* @author Nicolas JUEN
175217
*/
176-
private function is_minified(): bool {
218+
public function is_minified(): bool {
177219
return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false );
178220
}
179221

inc/Services/Editor.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
43
namespace BEA\Theme\Framework\Services;
54

65
use BEA\Theme\Framework\Framework;
76
use BEA\Theme\Framework\Service;
87
use BEA\Theme\Framework\Service_Container;
8+
use BEA\Theme\Framework\Tools\Assets as Assets_Tools;
99

1010
class Editor implements Service {
1111
/**
12-
* @var \BEA\Theme\Framework\Tools\Assets $assets_tools
12+
* @var Assets_Tools $assets_tools
1313
*/
1414
private $assets_tools;
1515

@@ -22,7 +22,7 @@ class Editor implements Service {
2222
* @param Service_Container $container
2323
*/
2424
public function register( Service_Container $container ): void {
25-
$this->assets_tools = new \BEA\Theme\Framework\Tools\Assets();
25+
$this->assets_tools = new Assets_Tools();
2626
$this->assets = Framework::get_container()->get_service( 'assets' );
2727
}
2828

@@ -142,19 +142,7 @@ private function after_theme_setup(): void {
142142
* editor style
143143
*/
144144
private function style(): void {
145-
/**
146-
* Default file
147-
**/
148-
$file = 'editor.css';
149-
150-
/**
151-
* @var Assets $assets
152-
**/
153-
$assets = Framework::get_container()->get_service( 'assets' );
154-
155-
if ( ( ! defined( 'SCRIPT_DEBUG' ) || false === SCRIPT_DEBUG ) && false !== $assets ) {
156-
$file = $assets->get_min_file( 'editor.css' );
157-
}
145+
$file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.css' ) : 'editor.css';
158146

159147
/**
160148
* Do not enqueue a inexistant file on admin
@@ -170,27 +158,19 @@ private function style(): void {
170158
* Editor script
171159
*/
172160
public function admin_editor_script(): void {
173-
/**
174-
* @var Assets $assets
175-
**/
176-
$assets = Framework::get_container()->get_service( 'assets' );
177-
178-
$theme = wp_get_theme();
179-
$file = ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) ? $assets->get_min_file( 'editor.js' ) : 'editor.js';
161+
$file = $this->assets->is_minified() ? $this->assets->get_min_file( 'editor.js' ) : 'editor.js';
180162
$filepath = 'dist/' . $file;
181163

182164
if ( ! file_exists( get_theme_file_path( $filepath ) ) ) {
183165
return;
184166
}
185167

168+
$asset_data = $this->assets->get_asset_data( $file );
186169
$this->assets_tools->register_script(
187170
'theme-admin-editor-script',
188171
$filepath,
189-
[
190-
'wp-blocks',
191-
'wp-dom',
192-
],
193-
$theme->get( 'Version' ),
172+
$asset_data['dependencies'],
173+
$asset_data['version'],
194174
true
195175
);
196176
$this->assets_tools->enqueue_script( 'theme-admin-editor-script' );

inc/Tools/Assets.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace BEA\Theme\Framework\Tools;
44

5-
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
6-
75
/**
86
* Class Assets
97
*

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"what-input": "^5.2.10"
2020
},
2121
"devDependencies": {
22-
"@wordpress/stylelint-config": "^20.0.2",
22+
"@wordpress/blocks": "^11.16.0",
23+
"@wordpress/dependency-extraction-webpack-plugin": "^4.0.0",
24+
"@wordpress/dom-ready": "^3.17.0",
25+
"@wordpress/hooks": "^3.17.0",
26+
"@wordpress/stylelint-config": "^21.0.0",
2327
"browser-sync": "^2.27.10",
2428
"browser-sync-webpack-plugin": "^2.3.0",
2529
"canvas": "^2.9.2",
@@ -54,6 +58,7 @@
5458
"stylelint-webpack-plugin": "3.3.0",
5559
"svg-sprite-loader": "^6.0.10",
5660
"svgo-loader": "^3.0.0",
61+
"terser-webpack-plugin": "^5.3.6",
5762
"webpack": "^5.35.0",
5863
"webpack-bundle-analyzer": "^4.4.1",
5964
"webpack-cli": "^4.6.0",
@@ -65,5 +70,5 @@
6570
"node": "16.15.0",
6671
"yarn": "1.22.19"
6772
},
68-
"packageManager": "yarn@3.2.1"
73+
"packageManager": "yarn@3.2.3"
6974
}

src/js/editor.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import lazySizes from 'lazysizes'
22
import 'lazysizes/plugins/native-loading/ls.native-loading'
33
import 'lazysizes/plugins/object-fit/ls.object-fit'
4+
import domReady from '@wordpress/dom-ready'
5+
import { addFilter } from '@wordpress/hooks'
6+
import { unregisterBlockStyle, getBlockVariations, unregisterBlockVariation } from '@wordpress/blocks'
47

58
/**
69
* LazySizes configuration
@@ -11,25 +14,23 @@ lazySizes.cfg.nativeLoading = {
1114
}
1215

1316
// Native Gutenberg
14-
if (typeof wp !== 'undefined') {
15-
wp.domReady(() => {
16-
wp.blocks.unregisterBlockStyle('core/separator', ['wide', 'dots'])
17-
// whitelist core embeds
18-
const allowedEmbedVariants = ['youtube', 'vimeo', 'dailymotion']
19-
wp.blocks.getBlockVariations('core/embed').forEach((variant) => {
20-
if (!allowedEmbedVariants.includes(variant.name)) {
21-
wp.blocks.unregisterBlockVariation('core/embed', variant.name)
22-
}
23-
})
17+
domReady(() => {
18+
unregisterBlockStyle('core/separator', ['wide', 'dots'])
19+
// whitelist core embeds
20+
const allowedEmbedVariants = ['youtube', 'vimeo', 'dailymotion']
21+
getBlockVariations('core/embed').forEach((variant) => {
22+
if (!allowedEmbedVariants.includes(variant.name)) {
23+
unregisterBlockVariation('core/embed', variant.name)
24+
}
2425
})
25-
}
26+
})
2627

2728
// ACF Blocks
2829
if (window.acf) {
2930
// Do stuff
3031
}
3132

32-
wp.hooks.addFilter('blocks.registerBlockType', 'beapi-framework', function (settings, name) {
33+
addFilter('blocks.registerBlockType', 'beapi-framework', function (settings, name) {
3334
if (name === 'core/list') {
3435
// compact preview for block list
3536
settings.example.attributes.values = '<li><a>Lorem ipsum</a></li><li><a>Dolor sit amet</a></li>'

0 commit comments

Comments
 (0)