Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config to exclude files from routing #2932

Closed
wants to merge 4 commits into from

Conversation

tony19
Copy link
Contributor

@tony19 tony19 commented Nov 26, 2021

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

Problem

The following directory layout creates an endpoint for post.spec.js and post.md, and the only way to opt out is to make them private modules with an underscore/dot prefix:

src
└── routes
    └── posts
        ├── post.md  👈 converted to endpoint!
        ├── post.spec.js  👈 converted to endpoint!
        └── post.svelte

Some developers might require the exact layout/naming shown, where:

  1. The files are colocated
  2. The files have the same basename

Solution

This PR adds kit.excludes config, which accepts an array of String, RegExp, or Function.

  • String - The basename of the file/directory is compared against the string, and it's excluded if matches.
  • RegExp - The basename of the file/directory is tested against the regular expression, and it's excluded if matches.
  • ({ filepath, basename }) => boolean - The file/directory is excluded if the function returns true.

The default would be [^_.] to match the current behavior. This also would enable users to override the current behavior, allowing for underscore/dot-prefixed files to be included in routing.

Example config:

// svelte.config.js
export default {
	kit: {
		excludes: [
			/^[_.]/,
			/\.(test|spec)\.js$/,
			'README.md',
			({ basename }) => basename.startsWith('@'),
			({ filepath }) => filepath.includes('demo')
		]
	}
};

Closes #2930

@changeset-bot
Copy link

changeset-bot bot commented Nov 26, 2021

🦋 Changeset detected

Latest commit: 5933875

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

Thank you — am picking this up in #3576 (I couldn't push to this branch for some reason), so I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Config to exclude files from routing
2 participants