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

@tips-services/sveltekit-lib-routes

v0.0.2

Published

Import rutes from sveltekit library

Downloads

66

Readme

sveltekit-lib-routes

Motivation

SvelteKit's filesystem-based routing and its approach to SvelteKit packages allow developers to expose src/lib for components and utilities. However, packages that need to provide pre-configured routes, such as those handling authentication, face limitations in this system.

Current status

While the community has discussed solutions, no official proposal has been adopted

  • https://stackoverflow.com/questions/71728342/creating-sveltekit-library-with-reusable-routes
  • https://github.com/sveltejs/kit/issues/8896
  • https://github.com/sveltejs/kit/discussions/5880

Better than nothing proposal

This project introduces a Vite plugin that allows developers to copy routes from a package library's src/routes to a user's src/routes directory, offering a workaround for the current limitations.

For Package Developers

When creating a package with reusable routes:

  • Define routes in src/routes
/account
└── +page.svelte
/users
├── +layout.svelte
├── +page.svelte
├── /[userId]
│   └── +page.svelte
└── /add
    └── +page.svelte
/auth
├── /login
│   ├── +page.svelte
│   └── +page.server.ts
├── /logout
│   ├── +page.svelte
│   └── +page.server.ts
└── /callback
    ├── +page.svelte
    └── +page.server.ts
  • Expose routes through src/lib/routes.ts
export const routes = {
  users: "src/routes/users",
  account: "src/routes/account",
  auth: "src/routes/auth",
};
{
  "name": "lib-routes-users-example",
  "...": "...",
  "exports": {
    "...": {},
    // export src/lib/routes.ts
    "./routes": {
      "types": "./dist/routes.d.ts",
      "default": "./dist/routes.js"
    }
  },
  // sveltekit lib only adds lib to files,
  // include src/routes in published package
  "files": ["...", "src/routes"]
}
  • Publish the package

For Consumers of the Package

To use the routes provided by a package

  • Install the vite plugin in vite.config.ts
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { svelteKitLibRoutes } from "sveltekit-lib-routes";

export default defineConfig({
  plugins: [sveltekit(), svelteKitLibRoutes()],
});
  • Install the Package containing the routes (ex. lib-routes-users-example).
  • Create a Route Directory (ex. src/routes/users) and add _sveltekit-lib-route.js
import { routes } from "lib-routes-users-example/routes";

export default {
  from: "lib-routes-users-example/routes",
  import: routes.users,
  to: import.meta.dirname,
};
  • Route handling
    • The plugin will copy the specified routes from the package into the user's src/routes directory.
    • Any existing files, except _sveltekit-lib-route.js, will be deleted during this process.

Drawbacks

  • Component Imports: Routes exposed by package libraries cannot use $lib/* as it refers to the user's $lib directory, not the package's.
  • File Stability: Large or frequently changing files in the src/routes of a package can lead to excessive updates in the user's project.
  • Customizability: Users cannot easily customize routes or components within them, as changes will be overwritten by the Vite plugin.
  • URL Path Mapping: Within routes, paths should avoid hardcoding. Use $page.url.pathname from src/routes/$route/+layout.svelte get the /$route path. If you use this in a child route (ex. src/routes/$route/$subroute/+page.svelte), $page.url.pathname would reflect the full nested path, such as /$route/$subroute.