22
33namespace App \Providers ;
44
5+ use Illuminate \Support \Facades \Vite ;
56use Roots \Acorn \Sage \SageServiceProvider ;
67
7- use function Roots \bundle ;
8-
98class AssetsServiceProvider extends SageServiceProvider
109{
1110 public function register (): void
1211 {
1312 add_action ('wp_enqueue_scripts ' , function (): void {
14- bundle ('app ' )->enqueue ();
15-
1613 remove_action ('wp_body_open ' , 'wp_global_styles_render_svg_filters ' );
1714 }, 100 );
1815
19- add_action ('enqueue_block_editor_assets ' , function (): void {
20- bundle ('editor ' )->enqueue ();
21- }, 100 );
16+ add_filter ('block_editor_settings_all ' , function ($ settings ) {
17+ $ style = Vite::asset ('resources/css/editor.css ' );
18+
19+ $ settings ['styles ' ][] = [
20+ 'css ' => "@import url(' {$ style }') " ,
21+ ];
22+
23+ return $ settings ;
24+ });
2225
23- /**
24- * Use theme.json from the build directory
25- *
26- * @param string $path
27- * @param string $file
28- * @return string
29- */
30- add_filter ('theme_file_path ' , function (string $ path , string $ file ): string {
31- if ($ file === 'theme.json ' ) {
32- return public_path () . '/dist/theme.json ' ;
26+ add_filter ('admin_head ' , function () {
27+ if (! get_current_screen ()?->is_block_editor()) {
28+ return ;
3329 }
3430
35- return $ path ;
31+ $ dependencies = json_decode (Vite::content ('editor.deps.json ' ));
32+
33+ foreach ($ dependencies as $ dependency ) {
34+ if (! wp_script_is ($ dependency )) {
35+ wp_enqueue_script ($ dependency );
36+ }
37+ }
38+
39+ echo Vite::withEntryPoints ([
40+ 'resources/js/editor.ts ' ,
41+ ])->toHtml ();
42+ });
43+
44+ add_filter ('theme_file_path ' , function ($ path , $ file ) {
45+ return $ file === 'theme.json '
46+ ? public_path ('build/assets/theme.json ' )
47+ : $ path ;
3648 }, 10 , 2 );
3749
3850 add_filter ('wp_theme_json_data_default ' , function (\WP_Theme_JSON_Data $ themeJson ): \WP_Theme_JSON_Data {
39- $ themeJsonFile = public_path ('/dist /theme.json ' );
51+ $ themeJsonFile = public_path ('/build/assets /theme.json ' );
4052 if (!file_exists ($ themeJsonFile )) {
4153 return $ themeJson ;
4254 }
@@ -46,8 +58,7 @@ public function register(): void
4658 return $ themeJson ;
4759 }
4860
49- $ mergedData = array_merge ($ themeJson ->get_data (), $ decodedData );
50- return new \WP_Theme_JSON_Data ($ mergedData , 'default ' );
61+ return new \WP_Theme_JSON_Data ($ decodedData , 'default ' );
5162 }, 100 );
5263 }
5364}
0 commit comments