Skip to content

Commit

Permalink
fix: find assets from manifest instead of generating urls
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Sep 14, 2022
1 parent 9ec139a commit ffca5eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/'));
Expand Down
12 changes: 12 additions & 0 deletions src/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
19 changes: 5 additions & 14 deletions tests/Features/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/builds/public/with-image-assets/manifest.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit ffca5eb

Please # to comment.