Skip to content

Commit 85ad981

Browse files
author
apetkov225
committed
Replace baked-in hot reload with enqueue_block_editor_assets
1 parent c8066d4 commit 85ad981

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "2crt/uup-assets-loader",
3-
"version": "v1.0.4",
3+
"version": "v1.0.5",
44
"license": "MIT",
55
"autoload": {
66
"psr-4": {

src/ViteAssetsLoader.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ViteAssetsLoader {
4545
*/
4646
private $enqueued_editor_styles = [];
4747

48+
/**
49+
* The enqueued editor scripts: [ 'script-handle' => 'resources/js/editor.js', ... ]
50+
*/
51+
private $enqueued_editor_scripts = [];
52+
4853
/**
4954
* Initialize the asset loader
5055
*
@@ -66,7 +71,7 @@ public function __construct( string $manifest_path, string $manifest_url ) {
6671
// All <script> tags enqueued through this class should have
6772
// type="module" attribute, even in production.
6873
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
69-
if ( isset( $this->enqueued_scripts[ $handle ] ) ) {
74+
if ( isset( $this->enqueued_scripts[ $handle ] ) || isset( $this->enqueued_editor_scripts[ $handle ] ) ) {
7075
return preg_replace( '/^<script /i', '<script type="module" ', $tag );
7176
}
7277
return $tag;
@@ -112,8 +117,6 @@ protected function apply_hot_file_configuration( $manifest_path ) {
112117
add_action( 'wp_head', [ $this, 'load_vite_client_scripts' ] );
113118
add_action( 'admin_head', [ $this, 'load_vite_client_scripts' ] );
114119

115-
add_action( 'admin_print_scripts', [ $this, 'load_customizer_auto_reload_script' ] );
116-
117120
return true;
118121
}
119122

@@ -191,6 +194,23 @@ public function push_assets_to_wp_queue() {
191194
* @return void
192195
*/
193196
public function push_assets_to_wp_editor_queue() {
197+
foreach ( $this->enqueued_editor_scripts as $handle => $path ) {
198+
$url = $this->make_asset_url( $path );
199+
if ( is_null( $url ) ) {
200+
$this->add_admin_bar_message( "Missing script: $handle" );
201+
continue;
202+
}
203+
wp_enqueue_script(
204+
$handle,
205+
$url,
206+
[],
207+
// This null is intentional: it prevents `?ver=X.X.X`
208+
// arguments in the URL. This would cause problems
209+
// with the Vite dev server
210+
null,
211+
[ 'in_footer' => true ]
212+
);
213+
}
194214
foreach ( $this->enqueued_editor_styles as $handle => $path ) {
195215
$url = $this->make_asset_url( $path );
196216
if ( is_null( $url ) ) {
@@ -384,4 +404,16 @@ public function enqueue_style( $handle, $path ) {
384404
public function enqueue_editor_style( $handle, $path ) {
385405
$this->enqueued_editor_styles[ $handle ] = $path;
386406
}
407+
408+
/**
409+
* Enqueue a javascript-ish editor file built with the vite dev process.
410+
*
411+
* @param string $handle The wp_enqueue_script handle
412+
* @param string $path Path to the file in the resources directory
413+
*
414+
* @return void
415+
*/
416+
public function enqueue_editor_script( $handle, $path ) {
417+
$this->enqueued_editor_scripts[ $handle ] = $path;
418+
}
387419
}

0 commit comments

Comments
 (0)