For AI agents: the complete documentation index is available at https://rsbuild.rs/zh/llms.txt, the full documentation bundle is available at https://rsbuild.rs/zh/llms-full.txt, and this page is available as Markdown at https://rsbuild.rs/zh/plugins/list/plugin-svelte.md.
close
  • 简体中文
  • Svelte 插件

    Svelte 插件提供了对 Svelte 组件(.svelte 文件)的支持,插件内部集成了 svelte-loader。

    快速开始

    安装插件

    执行以下命令安装插件:

    npm
    yarn
    pnpm
    bun
    deno
    npm add @rsbuild/plugin-svelte -D
    Tip

    @rsbuild/plugin-svelte v2 不再支持 Rsbuild 1.x 和 Svelte 4.x。如果你的项目仍在使用这些版本,请安装 @rsbuild/plugin-svelte@1。

    注册插件

    在 Rsbuild 配置中注册插件:

    rsbuild.config.ts
    import { pluginSvelte } from '@rsbuild/plugin-svelte';
    
    export default {
      plugins: [pluginSvelte()],
    };

    注册后,即可在代码中引入 *.svelte 单文件组件。

    选项

    如果你需要自定义 Svelte 的编译行为,可以使用以下配置项。

    svelteLoaderOptions

    传递给 svelte-loader 的选项,请查阅 svelte-loader 文档 来了解具体用法。

    • 类型: SvelteLoaderOptions
    • 默认值:
    const defaultOptions = {
      compilerOptions: {
        dev: isDev,
      },
      preprocess: require('svelte-preprocess')(),
      emitCss: isProd && !rsbuildConfig.output.injectStyles,
      hotReload: isDev && rsbuildConfig.dev.hmr,
    };
    • 示例:
    pluginSvelte({
      svelteLoaderOptions: {
        preprocess: null,
      },
    });

    preprocessOptions

    传递给 svelte-preprocess 的选项,请查阅 svelte-preprocess 文档 来了解具体用法。

    • 类型: AutoPreprocessOptions
    • 默认值: undefined
    interface AutoPreprocessOptions {
      globalStyle: { ... },
      replace: { ... },
      typescript: { ... },
      scss: { ... },
      sass: { ... },
      less: { ... },
      stylus: { ... },
      babel: { ... },
      postcss: { ... },
      coffeescript: { ... },
      pug: { ... },
    }
    • 示例:
    pluginSvelte({
      preprocessOptions: {
        aliases: [
          ['potato', 'potatoLanguage'],
          ['pot', 'potatoLanguage'],
        ],
        /** Add a custom language preprocessor */
        potatoLanguage({ content, filename, attributes }) {
          const { code, map } = require('potato-language').render(content);
          return { code, map };
        },
      },
    });

    注意事项

    目前 svelte-loader 暂不支持 Svelte v5 热更新,详见 svelte-loader - Hot Reload。

    Less/Sass 中的别名处理

    Svelte 预处理器不会自动使用 Rsbuild 的路径别名。要在样式导入中使用 @/,需要配置 Sass importer 或 Less 插件。

    以下示例将 @/ 映射到配置文件同级的 src 目录,支持在 SCSS 中使用 @use '@/styles/variables.scss' as *,以及在 Less 中使用 @import '@/styles/variables.less':

    rsbuild.config.mjs
    import path from 'node:path';
    import { pluginSvelte } from '@rsbuild/plugin-svelte';
    
    const srcPath = path.join(import.meta.dirname, 'src');
    
    export default {
      plugins: [
        pluginSvelte({
          preprocessOptions: {
            scss: {
              importer(url) {
                return url.startsWith('@/')
                  ? { file: path.join(srcPath, url.slice(2)) }
                  : null;
              },
            },
            less: {
              plugins: [
                {
                  install(less, pluginManager) {
                    class AliasFileManager extends less.FileManager {
                      supports(filename) {
                        return filename.startsWith('@/');
                      }
    
                      loadFile(filename, ...args) {
                        return super.loadFile(
                          path.join(srcPath, filename.slice(2)),
                          ...args,
                        );
                      }
                    }
    
                    pluginManager.addFileManager(new AliasFileManager());
                  },
                },
              ],
            },
          },
        }),
      ],
    };