Skip to content

Add config to exclude files from routing - #2932

Closed
tony19 wants to merge 4 commits into
sveltejs:masterfrom
tony19-contrib:feat/excludes-config
Closed

tony19 wants to merge 4 commits into
sveltejs:masterfrom
tony19-contrib:feat/excludes-config

Conversation

@tony19

@tony19 tony19 commented Nov 26, 2021

Copy link
Copy Markdown
Contributor

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

changeset-bot Bot commented Nov 26, 2021

Copy link
Copy Markdown

🦋 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
Copy Markdown
Member

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

datstarkey added a commit to datstarkey/kit that referenced this pull request Mar 2, 2026
Add write-to-disk + shell-out pipeline for declaration emit, adapted from
the approach used in svelte-check's incremental mode (PR sveltejs#2932).

When `--incremental` or `--tsgo` is passed, svelte-package:
1. Pre-transpiles .svelte files to .ts via svelte2tsx() with manifest caching
2. Generates an overlay tsconfig with declaration emit settings
3. Spawns tsc (or tsgo from @typescript/native-preview) as a child process
4. Post-processes emitted .d.ts files (alias resolution, sourcemap fixup)

The default path (no flags) is completely unchanged.

Benchmarks on a 264-component Svelte library:
- Baseline (emitDts):     ~7.2s every run
- --incremental warm:     ~3.1s (2.3x faster)
- --tsgo cold:            ~3.7s (2x faster)
- --incremental --tsgo:   ~1.6s (4.5x faster)

Also bumps svelte2tsx to ~0.7.51 for rewriteExternalImports and emitJsDoc
support, and adds @typescript/native-preview as an optional peer dependency.

This branch was successfully deployed

1 active deployment
Preview 5933875c Deployed Nov 26, 2021 by vercel[bot]
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