【问题标题】:How do I exclude files from svelte-kit build?如何从 svelte-kit 构建中排除文件?
【发布时间】:2021-11-20 15:12:40
【问题描述】:

如果我使用 SvelteKit 运行 npm run build,它似乎包含 src 文件夹中的所有文件。是否可以排除某种文件类型(例如*test.js)?

示例

  1. 使用npm init svelte@next my-app选择演示应用

  2. 将以下代码添加到src/routes/todos/foo.test.js

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

  4. npm run preview

结果:describe is not defined

解决方法

将测试移出src

【问题讨论】:

    标签: sveltekit vite


    【解决方案1】:

    SvelteKit 1.0.0 支持routes configuration,它可以从src/routes 目录中排除文件。 config 值是一个函数,它接收文件路径作为参数,并返回true 以将文件用作路由。

    例如,以下routes 配置从路由中排除*.test.js 文件:

    // 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

    【讨论】:

    • @David SvelteKit 1.0.0 添加了kit.routes 以启用从src/routes 排除文件。查看更新的演示。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2014-07-23
    • 2014-10-05
    • 2018-09-22
    • 2019-01-29
    相关资源
    最近更新 更多