Skip to content

Commit

Permalink
Avoid duplicating assets when they have params
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Mar 9, 2022
1 parent 466603f commit 7674516
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function dependsOn($dependency)
$collection = $this->collection->get($this->lastAddedType);

foreach ($collection as $path => $item) {
if ($path === $this->lastAddedAsset) {
if (self::cleanAsset($path) === self::cleanAsset($this->lastAddedAsset)) {
$collection[$path] = array(
'namespace' => $item['namespace'],
'dependency' => $dependency
Expand Down Expand Up @@ -171,22 +171,44 @@ protected function addAsset($assets, $namespace = null)

$type = ($this->isCss($assets)) ? 'css' : 'js';
$collection = $this->collection->get($type);

if (! in_array($assets, $collection)) {
$existingAssets = array_map(function ($asset) {
return self::cleanAsset($asset);
}, array_keys($collection));

if (! in_array(self::cleanAsset($assets), $existingAssets)) {
$collection[$assets] = array(
'namespace' => $namespace,
'dependency' => array()
);

$this->collection->put($type, $collection);

$this->lastAddedType = $type;
$this->lastAddedAsset = $assets;
}

$this->lastAddedType = $type;
$this->lastAddedAsset = $assets;

return $this;
}

/**
* Remove `?` anf `#` parts from asset
*
* @param string $asset
* @return string
*/
private static function cleanAsset($asset)
{
$asset = (string) $asset;

$result = strstr($asset, '?', true);
$asset = $result === false ? $asset : $result;

$result = strstr($asset, '#', true);
$asset = $result === false ? $asset : $result;

return $asset;
}

/**
* Parse a bonsai.json file and add the assets to the collection.
*
Expand Down

0 comments on commit 7674516

Please sign in to comment.