npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iksaku/laravel-vite-router

v0.1.1

Published

Generate Laravel routes for Vite environments

Downloads

3

Readme

Laravel Vite Router

An easy way to use Laravel routes in your Vite app, similar to Ziggy but with less setup.

Installation

Install the vite plugin using npm: npm add @iksaku/laravel-vite-router --save-dev

Or using yarn: yarn add -D @iksaku/laravel-vite-router

Then add the plugin to your vite.config.js file:

import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import router from '@iksaku/laravel-vite-router'

export default defineConfig({
    plugins: [
        laravel(),
        router()
    ]
})

Finally, import our virtual:laravel/routes module from your app entrypoint:

// resources/js/app.js
import { createInertiaApp } from '@inertiajs/inertia-svelte'
import 'virtual:laravel/routes'

createInertiaApp({ ... })

This module will make the route() function globally available for your JavaScript code, be it using plain JavaScript or a framework like Svelte, Vue or React.

Usage

route(name: string, params: Record<string, any>) => string

The route() function takes two arguments: the name of the route and an optional object of parameters, mimicking the route() function from Laravel, and returns the URL for that route.

// routes/web.php
Route::get('/users/{user}', fn (User $user) => $user->name)->name('users.show');
route('users.show', { user: 1 }) // /users/1

Warning

We do not support passing an array of parameters to the route() function yet. If you need this, please open an issue or a PR.

This function plays really well with InertiaJS as you can use it to generate the URLs for your Inertia links:

<script>
    import { inertia } from '@inertiajs/inertia-svelte'

    export let users
</script>

<div>
    <h1>Users</h1>
    <ul>
        {#each users as user}
            <li>
                <a use:inertia href={route('users.show', { user: user.id })}>
                    {user.name}
                </a>
            </li>
        {/each}
    </ul>
</div>

Configuration

It is possible to only include a certain subset of routes in your JavaScript bundle. This can be done by passing a only property to the plugin options:

import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import router from '@iksaku/laravel-vite-router'

export default defineConfig({
    plugins: [
        laravel(),
        router({
            only: [
                'api.*', // Filter can be compared against the route name
                '/dashboard/*', // As well as to be compared against the route path 
            ]
        })
    ]
})

It is also possible to exclude certain routes from the JavaScript bundle by passing an except property to the plugin options:

import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import router from '@iksaku/laravel-vite-router'

export default defineConfig({
    plugins: [
        laravel(),
        router({
            except: [
                'api.internal.*', // Again, filter can be compared against the route name
                '/supersecretsettings/*', // And compared against the route path too 
            ]
        })
    ]
})

Note

The only and except properties are mutually exclusive. If you pass both, only the only property will be used.