Skip to content

Commit

Permalink
🎨 Improve the Asset Bundle class (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Dec 11, 2023
1 parent 49fbe13 commit 0b3c66e
Showing 1 changed file with 64 additions and 17 deletions.
81 changes: 64 additions & 17 deletions src/Roots/Acorn/Assets/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,48 @@

class Bundle implements BundleContract
{
use Conditional;
use Enqueuable;
use Conditional, Enqueuable;

/**
* The bundle ID.
*
* @var string
*/
protected $id;

/**
* The bundle path.
*
* @var string
*/
protected $path;

/**
* The bundle URI.
*
* @var string
*/
protected $uri;

/**
* The bundle runtime.
*
* @var string|null
*/
protected $runtime;

/**
* The bundle contents.
*
* @var array
*/
protected $bundle;

/**
* The bundle runtimes.
*
* @var array
*/
protected static $runtimes = [];

/**
Expand Down Expand Up @@ -125,10 +154,9 @@ public function runtimeSource()
/**
* Get the bundle URL.
*
* @param string $path
* @return string
*/
protected function getUrl($path)
protected function getUrl(string $path)
{
if (parse_url($path, PHP_URL_HOST)) {
return $path;
Expand All @@ -148,20 +176,39 @@ protected function getUrl($path)
protected function setRuntime()
{
if (Arr::isAssoc($this->bundle['js'])) {
$this->runtime = $this->bundle['js']['runtime'] ?? $this->bundle['js']["runtime~{$this->id}"] ?? null;
$this->runtime = $this->bundle['js']['runtime']
?? $this->bundle['js']["runtime~{$this->id}"]
?? null;

unset($this->bundle['js']['runtime'], $this->bundle['js']["runtime~{$this->id}"]);
} elseif (isset($this->bundle['js'][0]) && strpos($this->bundle['js'][0], 'runtime') === 0) {
$this->runtime = $this->bundle['js'][0];
unset($this->bundle['js'][0]);
} elseif (isset($this->bundle['js'][0]) && strpos($this->bundle['js'][0], 'js/runtime') === 0) {
$this->runtime = $this->bundle['js'][0];
unset($this->bundle['js'][0]);
} elseif (isset($this->bundle['mjs'][0]) && strpos($this->bundle['mjs'][0], 'runtime') === 0) {
$this->runtime = $this->bundle['mjs'][0];
unset($this->bundle['mjs'][0]);
} elseif (isset($this->bundle['mjs'][0]) && strpos($this->bundle['mjs'][0], 'js/runtime') === 0) {
$this->runtime = $this->bundle['mjs'][0];
unset($this->bundle['mjs'][0]);

return;
}

$this->runtime = $this->getBundleRuntime() ?? $this->getBundleRuntime('mjs');
}

/**
* Retrieve the runtime in a bundle.
*
* @return string|null
*/
protected function getBundleRuntime(string $type = 'js')
{
if (! $this->bundle[$type]) {
return null;
}

foreach ($this->bundle[$type] as $key => $value) {
if (! str_contains($value, 'runtime')) {
continue;
}

unset($this->bundle[$type][$key]);

return $value;
}

return null;
}
}

0 comments on commit 0b3c66e

Please sign in to comment.