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

astro-pages

v0.3.0

Published

Add custom file based routing directories in Astro

Downloads

346

Readme

astro-pages

npm version readme

Add custom file based routing directories in Astro

Why astro-pages?

Astro does not have an option to change the location of the page folder, you can't have multiple page folders (ex: inside a package), and ignoring pages normally requires you to prefix the file name with an underscore _.

Features

  • Add file based routing anywhere inside your project or package
  • Use glob patterns to match routes
  • Supports negative glob patterns to ignore routes
  • Override/transform route patterns
  • Use standalone inside an integration

Using

Install package:

npm i astro-pages  

Add to Astro config:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import pages from 'astro-pages';

export default defineConfig({
  // Inject pages inside 'src/routes'
  integrations: [ pages('routes') ],
});

Examples

Create multiple page directories

// Create 'page' directories at 'src/routes' and `routes/`
pages(
  'routes', 
  {
    cwd: '../',
    dir: 'routes'
  }
)

Customize base directory for relative paths

pages(
  {
    // Resolves to 'routes' dir at the root of your project
    dir: 'routes',
    cwd: '../', // project root
  },
),

Ignoring routes

pages(
  {
    dir: 'routes',
    // Ignore all routes inside 'src/custom/ignore'
    glob: '["**.{astro,ts,js}", "!**/ignore/**"]'
  },
),

Transform route pattern

pages(
  {
    dir: 'routes',
    // Transform page patterns, add base path to routes
    pattern: ({ pattern }) => '/base' + pattern 
  },
),

Debug with verbose logging

pages(
  {
    dir: 'routes',
    // Log injetced pages and warnings
    log: "verbose"
  },
),

Use it all together

pages(
  {
    dir: 'routes',
    glob: '["**.{astro,ts,js}", "!**/ignore/**"]'
    pattern: ({ pattern }) => '/base' + pattern 
    log: "verbose"
  },
  // ...
),

Standalone inside inside an integration:

// package/index.ts
import type { AstroIntegration } from 'astro';
import { addPageDir } from 'astro-pages';

export default function(options): AstroIntegration {  
  return {
    name: 'astro-pages',
    hooks: {
      'astro:config:setup': ({ config, logger, injectRoute }) => {

        const pageConfig = {
          cwd: import.meta.url,
          dir: 'pages',
          config,
          logger
        }

        const { 
          pages,
          injectPages 
        } = addPageDir(pageConfig)

        // Injects pages inside 'package/pages'
        injectPages(injectRoute)

      }
    }
  }
}

Option Reference

dir

Type: string

The page directory, relative dirs are resolved relative to srcDir (/src by default).

Note: Directories located outside the root of your project may cause problems since they are not loaded by Astro

cwd

Type: string

Default: Astro config srcDir

Directory that the dir option is resolved to if it is relative. If the cwd option itself relative it is resolved relative to the srcDir in Astro config (/src by default).

glob

Type: string | string[]

Default: "**.{astro,ts,js}"

A glob pattern (or array of glob patterns) matching page files inside your page directory. Supports negative patterns to ignore specific paths.

Note: Only glob files that Astro supports! (.astro, .ts, and .js by default)

pattern

Type: function

(route: {
  dir: string;
  entrypoint: string;
  ext: string;
  pattern: string;
}) => string

Function used to transform the pattern/path pages are located at

log

Type: "verbose" | "minimal" | boolean | null | undefined

Default: true

  • "verbose": Log all warnings and page injections
  • "minimal" | true: Log addition of a page directory
  • false | null | undefined: No logging