Re-enabled mix-manifest.json for hashing assets

This commit is contained in:
Chris Kankiewicz
2020-02-06 23:28:37 -07:00
parent d2f5960877
commit 63b989d6b7
4 changed files with 38 additions and 5 deletions

View File

@@ -2,6 +2,8 @@
namespace App\ViewFunctions;
use Tightenco\Collect\Support\Collection;
class Asset extends ViewFunction
{
/** @const Constant description */
@@ -19,6 +21,30 @@ class Asset extends ViewFunction
*/
public function __invoke(string $path): string
{
$path = '/' . ltrim($path, '/');
if ($this->mixManifest()->has($path)) {
$path = $this->mixManifest()->get($path);
}
return self::ASSET_PATH . ltrim($path, '/');
}
/**
* Return the mix manifest collection.
*
* @return \Tightenco\Collect\Support\Collection
*/
protected function mixManifest(): Collection
{
$mixManifest = $this->container->get('base_path') . '/' . self::ASSET_PATH . 'mix-manifest.json';
if (! is_file($mixManifest)) {
return new Collection();
}
return Collection::make(
json_decode(file_get_contents($mixManifest), true) ?? []
);
}
}

View File

@@ -9,10 +9,12 @@ class AssetTest extends TestCase
{
public function test_it_can_return_an_asset_path(): void
{
$this->container->set('base_path', $this->filePath('.'));
$asset = new Asset($this->container, $this->config);
$this->assertEquals('app/assets/css/app.css', $asset('css/app.css'));
$this->assertEquals('app/assets/js/app.js', $asset('js/app.js'));
$this->assertEquals('app/assets/app.css?id=417c7a9bc03852aafb27', $asset('app.css'));
$this->assertEquals('app/assets/app.js?id=6753a7269276c7b52692', $asset('app.js'));
$this->assertEquals('app/assets/images/icon.png', $asset('images/icon.png'));
}
public function test_it_can_return_an_asset_with_a_subdirectory(): void
@@ -21,7 +23,8 @@ class AssetTest extends TestCase
$asset = new Asset($this->container, $this->config);
$this->assertEquals('app/assets/css/app.css', $asset('css/app.css'));
$this->assertEquals('app/assets/js/app.js', $asset('js/app.js'));
$this->assertEquals('app/assets/app.css?id=417c7a9bc03852aafb27', $asset('app.css'));
$this->assertEquals('app/assets/app.js?id=6753a7269276c7b52692', $asset('app.js'));
$this->assertEquals('app/assets/images/icon.png', $asset('images/icon.png'));
}
}

View File

@@ -0,0 +1,4 @@
{
"/app.css": "/app.css?id=417c7a9bc03852aafb27",
"/app.js": "/app.js?id=6753a7269276c7b52692"
}

View File

@@ -2,7 +2,7 @@ let mix = require('laravel-mix');
let tailwindcss = require('tailwindcss');
require('laravel-mix-purgecss');
mix.setPublicPath('.');
mix.setPublicPath('app/assets');
mix.webpackConfig({
watchOptions: { ignored: ['node_modules', 'app/vendor'] }