Skip to content

Instantly share code, notes, and snippets.

@DavidTheProgrammer
Last active August 15, 2024 07:53
Show Gist options
  • Save DavidTheProgrammer/d352f7c39eaa12e813f368fae08d4527 to your computer and use it in GitHub Desktop.
Save DavidTheProgrammer/d352f7c39eaa12e813f368fae08d4527 to your computer and use it in GitHub Desktop.
NuxtJS 3 - Custom module to ignore files in /pages directory
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);
});
}
});
@DavidTheProgrammer
Copy link
Author

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 the filterFunctions Record and it'll automatically be applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment