Skip to content

Instantly share code, notes, and snippets.

@danlapteacru
Last active July 23, 2021 12:32
Show Gist options
  • Save danlapteacru/f6591cfd1f48976a4285fefeeffd1c2e to your computer and use it in GitHub Desktop.
Save danlapteacru/f6591cfd1f48976a4285fefeeffd1c2e to your computer and use it in GitHub Desktop.
WordPress Laravel Mix Asset Class
<?php
declare(strict_types=1);
/**
* Class LaravelMixAsset
*/
class LaravelMixAsset
{
/**
* @var string
*/
protected string $path;
/**
* @var string
*/
protected string $manifest_directory = '/dist';
/**
* @var null|string
*/
protected ?string $manifest_path;
/**
* @var null|array
*/
protected ?array $manifest;
/**
* LaravelMixAsset constructor.
* @param string $path
*/
public function __construct(string $path)
{
$this->path = static::before($path, '?');
$this->manifest_path = $this->getManifestPath();
$this->manifest = $this->getManifest();
}
/**
* @return string
*/
protected function getManifestPath(): string
{
if (empty($this->manifest_path)) {
return get_theme_file_path($this->manifest_directory . '/mix-manifest.json');
}
return $this->manifest_path ?? '';
}
/**
* @return array
*/
protected function getManifest(): array
{
if (empty($this->manifest)) {
return json_decode(file_get_contents($this->manifest_path), true);
}
return $this->manifest ?? [];
}
/**
* Get asset URL
*
* @return string
*/
public function uri(): string
{
if (!file_exists($this->manifest_path)) {
return get_theme_file_uri($this->path);
}
$path = str_replace($this->manifest_directory, '', $this->path);
$path = '/' . ltrim($path, '/');
if (!array_key_exists($path, $this->manifest)) {
return get_theme_file_uri($path);
}
$path = $this->manifest[$path];
$path = ltrim($path, '/');
$mixHotUri = $this->getMixHotUri($path);
if (! empty($mixHotUri)) {
return $mixHotUri;
}
return get_stylesheet_directory_uri() . trailingslashit($this->manifest_directory) . $path;
}
/**
* Get asset dist path
*
* @return string
*/
public function path(): string
{
if (! file_exists($this->manifest_path)) {
return get_theme_file_path($this->path);
}
$path = str_replace($this->manifest_directory, '', $this->path);
$path = ltrim($path, '/');
return get_stylesheet_directory() . trailingslashit($this->manifest_directory) . $path;
}
/**
* Check if asset exists
*
* @return bool
*/
public function exists(): bool
{
return file_exists($this->path());
}
/**
* Get the asset contents
*
* @return string
*/
public function contents(): string
{
if (!$this->exists()) {
return '';
}
return file_get_contents($this->path());
}
/**
* Get the returned value of the asset
*
* @return mixed|false
*/
public function get()
{
if (!$this->exists()) {
return false;
}
return include $this->path();
}
/**
* Get the portion of a string before the first occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function before(string $subject, string $search): string
{
if ($search === '') {
return $subject;
}
$result = strstr($subject, $search, true);
return $result === false ? $subject : $result;
}
/**
* Return the remainder of a string after the first occurrence of a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
public static function after(string $subject, string $search): string
{
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}
/**
* Get the URI to a Mix hot module replacement server.
*
* @link https://laravel-mix.com/docs/hot-module-replacement
*
* @param string $path
* @return string
*/
protected function getMixHotUri(string $path): string
{
if (file_exists($path . '/hot')) {
$url = rtrim(rtrim(file_get_contents($path . '/hot')), '/');
return static::after($url, ':');
}
return '';
}
}
<?php
declare(strict_types=1);
include_once __DIR__ . '/includes/LaravelMixAsset.php';
/**
* @param string $path
* @return LaravelMixAsset
*/
function asset(string $path): LaravelMixAsset
{
return new LaravelMixAsset($path);
}
/**
* Enqueue styles
*/
add_action('wp_enqueue_scripts', function (): void {
wp_enqueue_script('site/vendor.js', asset('scripts/vendor.js')->uri(), ['jquery'], null, true);
wp_enqueue_script('site/app.js', asset('scripts/app.js')->uri(), ['jquery'], null, true);
wp_add_inline_script('site/vendor.js', asset('scripts/manifest.js')->contents(), 'before');
wp_enqueue_style('site/app.css', asset('styles/app.css')->uri());
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment