Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,12 @@ protected function writeBarrelFile(SplFileInfo $dir, array $nonBarrelPaths = [])
}

foreach ($dirs as $d) {
$imports->addWildcard("./{$d}", TypeScript::safeMethod($d, 'Method'), default: true);
// A subdirectory named "index" holds its barrel at "index/index.ts",
// but every resolver prefers the sibling "index.ts" for "./index", so
// it has to be named in full or this barrel imports itself.
$from = $d === 'index' ? './index/index' : "./{$d}";

$imports->addWildcard($from, TypeScript::safeMethod($d, 'Method'), default: true);
$object->key(TypeScript::safeMethod($d, 'Method'))->rawKey();
}

Expand Down
12 changes: 11 additions & 1 deletion src/Converters/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function convert(Collection $routes): array

$inFile = in_array($firstSubDir, array_column($this->exports[$path], 'originalMethod'));

$resultImports->addDefault('./'.$firstSubDir, $safeFirstSubDirMethod = TypeScript::safeMethod($firstSubDir, 'Method'), safe: $inFile);
$resultImports->addDefault($this->subDirImportPath($firstSubDir), $safeFirstSubDirMethod = TypeScript::safeMethod($firstSubDir, 'Method'), safe: $inFile);

$subDirImportNames->push($safeFirstSubDirMethod);

Expand Down Expand Up @@ -202,6 +202,16 @@ protected function writeNamedRoutesFile(Collection $routes): void
->each($this->writeNamedFile(...));
}

/**
* A subdirectory named "index" holds its barrel at "index/index.ts", but
* every resolver prefers the sibling "index.ts" for "./index", so it has
* to be named in full or the barrel silently imports itself.
*/
protected function subDirImportPath(string $subDir): string
{
return $subDir === 'index' ? './index/index' : './'.$subDir;
}

protected function writeNamedFile(Collection $routes, string $namespace): void
{
$path = join_paths($this->baseDir, str_replace('.', DIRECTORY_SEPARATOR, $namespace), 'index').'.ts';
Expand Down
12 changes: 12 additions & 0 deletions tests/IndexNamedRoute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, it } from "vitest";
import albums from "../workbench/resources/js/wayfinder/routes/albums";
import photos from "../workbench/resources/js/wayfinder/routes/photos";

it("can handle a route name that is both a leaf and a prefix of 'index'", () => {
expect(photos.index().url).toBe("/photos");
expect(photos.index.window().url).toBe("/photos/window");
});

it("can handle an 'index' prefix with no leaf route of its own", () => {
expect(albums.index.recent().url).toBe("/albums/recent");
});
5 changes: 5 additions & 0 deletions workbench/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,8 @@
Route::get('secrets', [SecretsController::class, 'index'])->name('secrets.index');
Route::get('secrets/reveal', [SecretsController::class, 'reveal'])->name('secrets.reveal');
Route::get('secrets/resource', [SecretsController::class, 'resource'])->name('secrets.resource');

Route::get('/photos', fn () => 'ok')->name('photos.index');
Route::get('/photos/window', fn () => 'ok')->name('photos.index.window');

Route::get('/albums/recent', fn () => 'ok')->name('albums.index.recent');