Last active
August 15, 2024 07:53
-
-
Save DavidTheProgrammer/d352f7c39eaa12e813f368fae08d4527 to your computer and use it in GitHub Desktop.
NuxtJS 3 - Custom module to ignore files in /pages directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {defineNuxtModule, extendPages} from "@nuxt/kit"; | |
import type {NuxtPage} from "@nuxt/schema"; | |
/** | |
* A function that filters out pages that should be ignored | |
* It should return the valid list of pages to replace the original list completely | |
* @param pages The list of pages to filter | |
* @returns The list of pages to use | |
*/ | |
type PageFilterFunction = (pages: NuxtPage[]) => NuxtPage[]; | |
/** | |
* Object with the functions to filter out pages that should be ignored | |
*/ | |
const filterFunctions: Record<string, PageFilterFunction> = { | |
// You can remove the functions below and add your own. | |
// or keep them and add more. | |
ignoreComponents: pages => pages.filter(page => !page.path.includes("components")), | |
ignoreTsFiles: pages => pages.filter(page => !page.file?.endsWith(".ts")) | |
} | |
/** | |
* Call all the configured functions and filter out the pages that should be ignored in nuxt module | |
*/ | |
export default defineNuxtModule({ | |
setup(_) { | |
extendPages((pages: NuxtPage[]) => { | |
const validPages = Object.values(filterFunctions).reduce((acc, fn) => fn(acc), pages); | |
pages.splice(0, pages.length, ...validPages); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place this file in the
<project-root>/modules
directory so Nuxt can pick it up automatically. You can name it whatever you want, it doesn't matter. Add your function in thefilterFunctions
Record and it'll automatically be applied.