Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit d2b6480

Browse files
committed
Added functionality to register Vite-compiled scripts.
1 parent a902ba8 commit d2b6480

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

includes/class-asset-enqueue.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public static function init() {
3232
self::$instance = new self();
3333
}
3434

35+
/**
36+
* Get the singleton instance.
37+
*
38+
* @return Asset_Enqueue|null The singleton instance or null if uninitialized.
39+
*/
40+
public static function get_instance(): Asset_Enqueue|null {
41+
return self::$instance;
42+
}
43+
3544
/**
3645
* An array of Manifest objects.
3746
*
@@ -160,4 +169,36 @@ private function get_manifest_style( Manifest $manifest, string $entrypoint ) {
160169

161170
return $styles[0];
162171
}
172+
173+
/**
174+
* Registers a script for enqueuing.
175+
*
176+
* @param string $handle The scripts handle name.
177+
* @param string $path The path to the script relative to the theme root directory (No proceeding forward slash).
178+
* @param array $dependencies (Optional) The scripts dependencies. Defaults to an empty array.
179+
* @param bool|array $in_footer (Optional) Whether to enqueue the script in the footer. Defaults to true.
180+
*/
181+
public function register_vite_script( string $handle, string $path, array $dependencies = array(), $in_footer = true ) {
182+
foreach ( $this->manifests as $manifest ) {
183+
$entry_point = $manifest->getEntrypoint( $path );
184+
if ( empty( $entry_point ) ) {
185+
continue;
186+
}
187+
if ( empty( $entry_point['url'] ) ) {
188+
continue;
189+
}
190+
if ( empty( $entry_point['hash'] ) ) {
191+
continue;
192+
}
193+
194+
wp_register_script(
195+
$handle,
196+
$entry_point['url'],
197+
$dependencies,
198+
$entry_point['hash'],
199+
$in_footer
200+
);
201+
break;
202+
}
203+
}
163204
}

theme-template/includes/register-scripts.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
* @creode-wordpress-theme-version :THEME_PLUGIN_VERSION
77
*/
88

9+
use Creode_Theme\Asset_Enqueue;
10+
911
/**
1012
* Enqueues front-end scripts.
1113
*/
1214
add_action(
1315
'wp_enqueue_scripts',
1416
function () {
17+
$asset_enqueue = Asset_Enqueue::get_instance();
18+
19+
$asset_enqueue->register_vite_script( 'example_script', 'js/example-script.js' );
20+
wp_enqueue_script( 'example_script' );
1521
}
1622
);

0 commit comments

Comments
 (0)