From 63b989d6b7b3fd657fe392e8d24574416e1de6ca Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Thu, 6 Feb 2020 23:28:37 -0700 Subject: [PATCH] Re-enabled mix-manifest.json for hashing assets --- app/src/ViewFunctions/Asset.php | 26 +++++++++++++++++++++++ tests/ViewFunctions/AssetTest.php | 11 ++++++---- tests/_files/app/assets/mix-manifest.json | 4 ++++ webpack.mix.js | 2 +- 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 tests/_files/app/assets/mix-manifest.json diff --git a/app/src/ViewFunctions/Asset.php b/app/src/ViewFunctions/Asset.php index 6dca4d6..5d31957 100644 --- a/app/src/ViewFunctions/Asset.php +++ b/app/src/ViewFunctions/Asset.php @@ -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) ?? [] + ); + } } diff --git a/tests/ViewFunctions/AssetTest.php b/tests/ViewFunctions/AssetTest.php index 2be34d9..9dca6b6 100644 --- a/tests/ViewFunctions/AssetTest.php +++ b/tests/ViewFunctions/AssetTest.php @@ -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')); } } diff --git a/tests/_files/app/assets/mix-manifest.json b/tests/_files/app/assets/mix-manifest.json new file mode 100644 index 0000000..64b8086 --- /dev/null +++ b/tests/_files/app/assets/mix-manifest.json @@ -0,0 +1,4 @@ +{ + "/app.css": "/app.css?id=417c7a9bc03852aafb27", + "/app.js": "/app.js?id=6753a7269276c7b52692" +} diff --git a/webpack.mix.js b/webpack.mix.js index 41f5c9f..3adb28e 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -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'] }