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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mondesirm
Copy link

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
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);
    }

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.

1 participant