How do I exclude files from svelte-kit build?

Viewed 1705

If I run npm run build with SvelteKit it seems to include all files from the src folder. Is it possible to exclude a certain file type (eg. *test.js)?

Example

  1. Select demo app with npm init svelte@next my-app

  2. Add the following code to src/routes/todos/foo.test.js

    describe('foo', () => {
      it('temp', () => {
        expect(true).toBe(false)
      })
    })
    
  3. npm run build

  4. npm run preview

Result: describe is not defined

Workaround

Move tests outside of src

1 Answers

UPDATE: SvelteKit 1.0.0-beta now requires pages/endpoints to follow a specific naming pattern, so explicit file exclusion should no longer be needed.

SvelteKit specially handles files in the routes/ directory with the following filenames (note the leading + in each filename):

All other files are ignored and can be colocated in the routes/ directory.

If, for some reason, you need to have a file that has a special name shown above, it's currently not possible to exclude that file from special processing.


Original outdated answer:

SvelteKit 1.0.0-beta supports a routes configuration that enables file exclusion from the src/routes directory. The config value is a function that receives a file path as an argument, and returns true to use the file as a route.

For example, the following routes config excludes *.test.js files from routes:

// sveltekit.config.js
⋮
const config = {
  kit: {
    ⋮
    routes: filepath => {
      return ![
        // exclude *test.js files
        /\.test\.js$/,

        // original default config
        /(?:(?:^_|\/_)|(?:^\.|\/\.)(?!well-known))/,
      ].some(regex => regex.test(filepath))
    },
  },
}

demo

Related