From ffca5eb0fcb1889b56b41d6c45c5d01680ddc079 Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Wed, 14 Sep 2022 14:43:36 +0200 Subject: [PATCH] fix: find assets from manifest instead of generating urls --- src/Configuration.php | 2 +- src/Manifest.php | 12 ++++++++++++ tests/Features/ConfigurationTest.php | 19 +++++-------------- .../public/with-image-assets/manifest.json | 11 +++++++++++ 4 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 tests/Fixtures/builds/public/with-image-assets/manifest.json diff --git a/src/Configuration.php b/src/Configuration.php index ae3fd88..58e417c 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -192,7 +192,7 @@ public function getEntryUrl(string $entryName): string public function getAssetUrl(string $path): string { if ($this->shouldUseManifest()) { - return asset(sprintf('/%s/%s', trim($this->config('build_path'), '/\\'), ltrim($path, '/'))); + return $this->getManifest()->getChunk($path)->getAssetUrl(); } return sprintf('%s/%s', rtrim($this->config('dev_server.url'), '/'), ltrim($path, '/')); diff --git a/src/Manifest.php b/src/Manifest.php index 080abff..149a5d6 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -61,6 +61,18 @@ public function getEntry(string $name): Chunk return $entry; } + /** + * Gets a chunk for the given name. + */ + public function getChunk(string $name): Chunk + { + if (!$chunk = $this->chunks->first(fn (array $chunk) => data_get($chunk, 'src') === $name)) { + throw NoSuchEntrypointException::inManifest($name, static::guessConfigName($this->getPath())); + } + + return Chunk::fromArray($this, $chunk); + } + /** * Gets every entry. */ diff --git a/tests/Features/ConfigurationTest.php b/tests/Features/ConfigurationTest.php index a5b3cb0..f63b43f 100644 --- a/tests/Features/ConfigurationTest.php +++ b/tests/Features/ConfigurationTest.php @@ -148,27 +148,18 @@ }); it('returns a valid asset URL in production', function () { + set_fixtures_path('builds'); set_env('production'); - set_vite_config('default', ['build_path' => '/with/slashes/']); - expect(vite()->getAssetUrl('/my-custom-asset.txt'))->toContain('http://localhost/with/slashes/my-custom-asset.txt'); - - set_vite_config('default', ['build_path' => '/with/leading/slash']); - expect(vite()->getAssetUrl('/my-custom-asset.txt'))->toContain('http://localhost/with/leading/slash/my-custom-asset.txt'); - - set_vite_config('default', ['build_path' => 'with/trailing/slash/']); - expect(vite()->getAssetUrl('/my-custom-asset.txt'))->toContain('http://localhost/with/trailing/slash/my-custom-asset.txt'); - - set_vite_config('default', ['build_path' => 'build']); - expect(vite()->getAssetUrl('/my-custom-asset.txt'))->toContain('http://localhost/build/my-custom-asset.txt'); - expect(vite()->getAssetUrl('my-custom/asset.txt'))->toContain('http://localhost/build/my-custom/asset.txt'); + set_vite_config('default', ['build_path' => '/with-image-assets']); + expect(vite()->getAssetUrl('resources/images/background.png'))->toContain('http://localhost/with-image-assets/assets/background.bbe601e4.png'); $property = new ReflectionProperty(UrlGenerator::class, 'assetRoot'); $property->setAccessible(true); $property->setValue(app('url'), 'https://s3.us-west-2.amazonaws.com/12345678'); - expect(vite()->getAssetUrl('/my-custom-asset.txt')) - ->toContain('https://s3.us-west-2.amazonaws.com/12345678/build/my-custom-asset'); + expect(vite()->getAssetUrl('resources/images/background.png')) + ->toContain('https://s3.us-west-2.amazonaws.com/12345678/with-image-assets/assets/background.bbe601e4.png'); }); it('respects the mode override in production', function () { diff --git a/tests/Fixtures/builds/public/with-image-assets/manifest.json b/tests/Fixtures/builds/public/with-image-assets/manifest.json new file mode 100644 index 0000000..e704a85 --- /dev/null +++ b/tests/Fixtures/builds/public/with-image-assets/manifest.json @@ -0,0 +1,11 @@ +{ + "resources/images/background.png": { + "file": "assets/background.bbe601e4.png", + "src": "resources/images/background.png" + }, + "resources/scripts/entry.ts": { + "file": "entry.2615a355.js", + "src": "resources/scripts/entry.ts", + "isEntry": true + } +}