|
8 | 8 |
|
9 | 9 | use Illuminate\Support\Facades\File; |
10 | 10 | use Illuminate\Support\Facades\Vite; |
11 | | -use Illuminate\Support\Str; |
12 | 11 |
|
13 | 12 | /** |
14 | | - * Inject the Vite assets into the head. |
| 13 | + * Inject styles into the block editor. |
15 | 14 | * |
16 | | - * @return void |
| 15 | + * @return array |
17 | 16 | */ |
18 | | -add_filter('wp_head', function () { |
19 | | - echo Str::wrap(app('assets.vite')([ |
20 | | - 'resources/css/app.css', |
21 | | - 'resources/js/app.js', |
22 | | - ]), "\n"); |
| 17 | +add_filter('block_editor_settings_all', function ($settings) { |
| 18 | + $style = Vite::asset('resources/css/editor.css'); |
| 19 | + |
| 20 | + $settings['styles'][] = [ |
| 21 | + 'css' => Vite::isRunningHot() |
| 22 | + ? "@import url('{$style}')" |
| 23 | + : Vite::content('resources/css/editor.css'), |
| 24 | + ]; |
| 25 | + |
| 26 | + return $settings; |
23 | 27 | }); |
24 | 28 |
|
25 | 29 | /** |
26 | | - * Inject assets into the block editor. |
| 30 | + * Inject scripts into the block editor. |
27 | 31 | * |
28 | 32 | * @return void |
29 | 33 | */ |
30 | 34 | add_filter('admin_head', function () { |
31 | | - $screen = get_current_screen(); |
32 | | - |
33 | | - if (! $screen?->is_block_editor()) { |
| 35 | + if (! get_current_screen()?->is_block_editor()) { |
34 | 36 | return; |
35 | 37 | } |
36 | 38 |
|
37 | | - $dependencies = File::json(public_path('build/editor.deps.json')) ?? []; |
| 39 | + $dependencies = json_decode(Vite::content('_editor.deps.json')); |
38 | 40 |
|
39 | 41 | foreach ($dependencies as $dependency) { |
40 | 42 | if (! wp_script_is($dependency)) { |
41 | 43 | wp_enqueue_script($dependency); |
42 | 44 | } |
43 | 45 | } |
44 | 46 |
|
45 | | - echo Str::wrap(app('assets.vite')([ |
46 | | - 'resources/css/editor.css', |
| 47 | + echo Vite::withEntryPoints([ |
47 | 48 | 'resources/js/editor.js', |
48 | | - ]), "\n"); |
| 49 | + ])->toHtml(); |
49 | 50 | }); |
50 | 51 |
|
51 | 52 | /** |
52 | | - * Use theme.json from the build directory |
| 53 | + * Add Vite's HMR client to the block editor. |
53 | 54 | * |
54 | | - * @param string $path |
55 | | - * @param string $file |
56 | | - * @return string |
| 55 | + * @return void |
57 | 56 | */ |
58 | | -add_filter('theme_file_path', function (string $path, string $file): string { |
59 | | - if ($file === 'theme.json') { |
60 | | - return public_path().'/build/assets/theme.json'; |
| 57 | +add_action('enqueue_block_assets', function () { |
| 58 | + if (! is_admin() || ! get_current_screen()?->is_block_editor()) { |
| 59 | + return; |
61 | 60 | } |
62 | 61 |
|
63 | | - return $path; |
| 62 | + if (! Vite::isRunningHot()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $script = sprintf( |
| 67 | + <<<'JS' |
| 68 | + window.__vite_client_url = '%s'; |
| 69 | +
|
| 70 | + window.self !== window.top && document.head.appendChild( |
| 71 | + Object.assign(document.createElement('script'), { type: 'module', src: '%s' }) |
| 72 | + ); |
| 73 | + JS, |
| 74 | + untrailingslashit(Vite::asset('')), |
| 75 | + Vite::asset('@vite/client') |
| 76 | + ); |
| 77 | + |
| 78 | + wp_add_inline_script('wp-blocks', $script); |
| 79 | +}); |
| 80 | + |
| 81 | +/** |
| 82 | + * Use the generated theme.json file. |
| 83 | + * |
| 84 | + * @return string |
| 85 | + */ |
| 86 | +add_filter('theme_file_path', function ($path, $file) { |
| 87 | + return $file === 'theme.json' |
| 88 | + ? public_path('build/assets/theme.json') |
| 89 | + : $path; |
64 | 90 | }, 10, 2); |
65 | 91 |
|
66 | 92 | /** |
|
0 commit comments