Skip to content

Detect TypeScript in InertiaPageGenerator #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2025

Conversation

mondesirm
Copy link
Contributor

I'm using React with TypeScript and I've been renaming .jsx to .tsx files a lot so this would help a lot when dealing with several views.

I tried to do something for Vue and Svelte as well, but I couldn't manage to properly mock the additional Filesystem calls in InertiaPageGeneratorTest : I kept encountering this confusing message.

@mondesirm
Copy link
Contributor Author

The following changes worked for me (I commented them):

InertiaPageGenerator.php

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)) {
        $adapterKey = strtolower($matches[1]);

        // Moved logic to new method
        if ($adapterKey === 'react' && $this->usesTypeScript()) {
            $adapterKey .= 'ts';
        }

        return $this->adapters[$adapterKey] ?? null;
    }

    return null;
}
    protected function populateStub(string $stub, InertiaStatement $inertiaStatement): string
    {
        $data = $inertiaStatement->data();
        $props = $this->adapter['framework'] === 'vue' ? json_encode($data) : '{ ' . implode(', ', $data) . ' }';
        $componentName = $this->adapter['framework'] === 'react' ? Str::afterLast($inertiaStatement->view(), '/') : null;

        // Vue and Svelte just needs a lang attribute in the script tag
        if (in_array($this->adapter['framework'], ['vue', 'svelte']) && $this->usesTypeScript()) {
            $stub = Str::replaceFirst('>', ' lang="ts">', $stub);
        }

        return str_replace([
            '{{ componentName }}',
            '{{ props }}',
            '{{ view }}',
        ], [
            $componentName,
            $props,
            str_replace('/', ' ', $inertiaStatement->view()),
        ], $stub);
    }
    // Checks for package and config file
    public function usesTypeScript(): bool
    {
        $packagePath = base_path('package.json');
        $tsConfigPath = base_path('tsconfig.json');
        $contents = $this->filesystem->get($packagePath);

        return $this->filesystem->exists($tsConfigPath) || preg_match('/"typescript"/i', $contents);
    }

@jasonmccreary
Copy link
Collaborator

@mondesirm, sorry for the delay here. I admittedly don't do a lot of JS. So I'm curious if Typescript extensions may be needed for the other frontends (e.g. Svelte, Vue, etc)?

@mondesirm
Copy link
Contributor Author

@jasonmccreary Among those 3 JS frameworks, only React uses a different extension for their views (.tsx). Vue and Svelte only need the lang=ts present (already handled in the second code block).

I didn't feel necessary to include them but Angular uses .ts by default and Next uses .tsx as well like React.

I figured this change would really be time-saving for JS devs who decide to adopt Laravel's starter kits.

@jasonmccreary
Copy link
Collaborator

@mondesirm, thanks. Merged.

@jasonmccreary jasonmccreary merged commit 715964a into laravel-shift:master Jul 15, 2025
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants