diff --git a/src/Generators/Statements/InertiaPageGenerator.php b/src/Generators/Statements/InertiaPageGenerator.php index 0d6c3b43..a3006de7 100644 --- a/src/Generators/Statements/InertiaPageGenerator.php +++ b/src/Generators/Statements/InertiaPageGenerator.php @@ -58,20 +58,25 @@ public function output(Tree $tree): array protected function getAdapter(): ?array { $packagePath = base_path('package.json'); - if (!$this->filesystem->exists($packagePath)) { return null; } $contents = $this->filesystem->get($packagePath); + if (!preg_match('/@inertiajs\/(vue3|react|svelte)/i', $contents, $matches)) { + return null; + } - if (preg_match('/@inertiajs\/(vue3|react|svelte)/i', $contents, $matches)) { - $adapterKey = strtolower($matches[1]); + $adapterKey = strtolower($matches[1]); + if (!isset($this->adapters[$adapterKey])) { + return null; + } - return $this->adapters[$adapterKey] ?? null; + if ($adapterKey === 'react' && ($this->filesystem->exists(base_path('tsconfig.json')) || preg_match('/"typescript"/i', $contents))) { + return array_replace($this->adapters[$adapterKey], ['extension' => '.tsx']); } - return null; + return $this->adapters[$adapterKey]; } protected function getStatementPath(string $view): string diff --git a/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php b/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php index 32eda257..40107c30 100644 --- a/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php +++ b/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php @@ -147,6 +147,7 @@ public static function inertiaAdaptersDataProvider(): array return [ ['vue', '"@inertiajs/vue3": "^2.0.0"', 'resources/js/Pages/Customer/Show.vue', '.vue'], ['react', '"@inertiajs/react": "^2.0.0"', 'resources/js/Pages/Customer/Show.jsx', '.jsx'], + ['react', '"@inertiajs/react": "^2.0.0", "typescript": "^5.0.0"', 'resources/js/Pages/Customer/Show.tsx', '.tsx'], ['svelte', '"@inertiajs/svelte": "^2.0.0"', 'resources/js/Pages/Customer/Show.svelte', '.svelte'], ]; } diff --git a/tests/fixtures/inertia-pages/customer-show.tsx b/tests/fixtures/inertia-pages/customer-show.tsx new file mode 100644 index 00000000..436d5a04 --- /dev/null +++ b/tests/fixtures/inertia-pages/customer-show.tsx @@ -0,0 +1,10 @@ +import { Head } from '@inertiajs/react' + +export default function Show({ customer, customers }) { + return ( +