Socket
Socket
Sign inDemoInstall

@badrap/routdir

Package Overview
Dependencies
0
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @badrap/routdir

Filesystem based route generation for Webpack + vue-router


Version published
Weekly downloads
3
decreased by-62.5%
Maintainers
2
Install size
27.6 kB
Created
Weekly downloads
 

Readme

Source

@badrap/routdir tests

Filesystem based route generation for Webpack + vue-router.

@badrap/routdir is based on Sapper's pages and layouts features, with some limitations.

This package also works well together with @badrap/preload.

Installation

$ npm i @badrap/routdir

Usage

Setup

@badrap/routdir exports one function that accepts a Webpack context object as returned by require.context.

import routdir from "@badrap/routdir";

// Create a Webpack context from the ./routes directory, and feed it to routdir
// to build the routes.
const routes = routdir(require.context("./routes", true, /\.vue$/));

The context can also be created in "lazy" mode to enable code splitting:

const routes = routdir(require.context("./routes", true, /\.vue$/, "lazy"));

The resulting routes can be passed as-is to vue-router, or do some further modifications to them, such as decorating them with @badrap/preload.

import VueRouter from "vue-router";
import preload from "@badrap/preload";

const router = new VueRouter({
  mode: "history",
  routes: preload(routes),
});

Static Routes

Files with basic alphanumeric names become static routes. For example one.vue and two.vue become routes /one and /two, respectively.

index.vue is a special case that just gets rendered as the root route. For example index.vue and nested/index.vue become routes / and /nested, respectively.

Dynamic Routes

Filenames wrapped in [ and ] become dynamic routes. For example [number].vue becomes route /:number. The route parameter number is then available for the route component as $route.params.number.

In the case a route matches a static route and a dynamic route the static one has the precedence.

Nested Routes & Layouts

Nested directories become nested routes. For example files nested/two.vue and nested/[number].vue become routes /nested/two and /nested/:number, respectively.

A routes under a specific directory (including the root directory) can have an optional common layout, defined by a special file _layout.vue. For example the file nested/_layout.vue would then become a layout for all routes under /nested. The layouts also stack, so _layout.vue and nested/_layout.vue both apply to all routes under /nested.

Example

The directory examples/vue-cli contains an example project demonstrating how to use this package with Vue CLI 3.

License

This library is licensed under the MIT license. See LICENSE.

Keywords

FAQs

Last updated on 25 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc