Add custom file based routing directories in Astro
// astro.config.mjs
import { defineConfig } from 'astro/config';
import pages from 'astro-pages';
export default defineConfig({
// Inject pages inside 'src/routes'
integrations: [ pages('routes') ],
});
// 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)
}
}
}
}