@douyinfe/semi-vite-plugin

中文文档
2.105.0发布于 2026/11/12MITTypeScriptCommonJS近 30 天下载 3059

A vite plugin for Semi Design to custom theme, replace prefix and so on.

原始使用文档
文档目录13 节
  1. Introduction
  2. Usage
  3. Install
  4. Custom theme
  5. Replace prefix of CSS selector
  6. Wrap output styles with CSS layer
  7. API
  8. semiTheming(options)
  9. How it works
  10. FAQ
  11. Why are classnames still semi-* in vite dev ?
  12. Vite 8 prints a deprecation warning about optimizeDeps.esbu…
  13. Related

A vite plugin for Semi Design to custom theme, replace prefix and so on.

Introduction

The plugin is designed for Semi Design with Vite, providing two major abilities:

  • Custom theme
  • Replace prefix of CSS selector

Note: The plugin detects Semi related dependencies by package path. It supports both @douyinfe/semi-ui and version-suffixed packages like @douyinfe/semi-ui-19 (also for semi-icons).

Usage

Install

Install @douyinfe/semi-vite-plugin as a development dependency:

npm install --save-dev @douyinfe/semi-vite-plugin
# or
yarn add --dev @douyinfe/semi-vite-plugin
# or
pnpm add -D @douyinfe/semi-vite-plugin

Custom theme

Semi Design uses Scss variables to extract thousands of Design Tokens. You can replace those tokens through this plugin to achieve theme customization. More info

You can custom theme through three ways:

  • npm package generated by Semi DSM
  • Local Scss file in your project
  • Pass key-value pair parameters to plugin

Priority from low to high.

Through npm package

After finishing the customization on Semi Design System, Semi DSM will generate an npm package for you, then use it like this:

// vite.config.ts
import { defineConfig } from 'vite';
import semiTheming from '@douyinfe/semi-vite-plugin';

export default defineConfig({
    plugins: [
        semiTheming({
            theme: '@semi-bot/semi-theme-yours',
        }),
    ],
});

Through local Scss file

You can check which tokens can be customized on the Semi Website.

Step 1: add a local file

// local.scss
$font-size-small: 16px;

Step 2: config vite

// vite.config.ts
import path from 'path';
import { defineConfig } from 'vite';
import semiTheming from '@douyinfe/semi-vite-plugin';

export default defineConfig({
    plugins: [
        semiTheming({
            include: path.resolve(__dirname, 'local.scss'),
        }),
    ],
});

Through parameters

// vite.config.ts
import { defineConfig } from 'vite';
import semiTheming from '@douyinfe/semi-vite-plugin';

export default defineConfig({
    plugins: [
        semiTheming({
            variables: {
                '$font-size-small': '16px',
            },
        }),
    ],
});

Replace prefix of CSS selector

The CSS selectors used by Semi Design are prefixed with semi by default (e.g. .semi-button). You can replace the prefix through this plugin:

// vite.config.ts
import { defineConfig } from 'vite';
import semiTheming from '@douyinfe/semi-vite-plugin';

export default defineConfig({
    plugins: [
        semiTheming({
            prefixCls: 'custom',
        }),
    ],
});

Then you get the replaced CSS selectors (e.g. .custom-button).

Wrap output styles with CSS layer

// vite.config.ts
import { defineConfig } from 'vite';
import semiTheming from '@douyinfe/semi-vite-plugin';

export default defineConfig({
    plugins: [
        semiTheming({
            cssLayer: true,
        }),
    ],
});

API

semiTheming(options)

PropertyTypeDefaultDescription
themestring | { name?: string }-Custom theme package name generated by Semi DSM.
prefixClsstringsemiPrefix of CSS class names.
variablesRecord<string, string | number>{}Key-value pair of Scss variables.
includestring-Absolute path of an extra Scss file to inject after theme variables.
cssLayerbooleanfalseWhen true, wrap the final compiled CSS with @layer semi { ... }.
omitCssbooleanfalseComment out .css imports inside semi packages. Useful when the integration framework can't import global CSS from node_modules directly.

How it works

  1. CSS → SCSS rewrite: Intercepts requests to @douyinfe/semi-(ui|icons|foundation)/lib/**/*.css, switches the suffix to .scss and reads the original Sass source.
  2. Theme injection: Injects the theme index.scss / global.scss / animation.scss / local.scss / user-provided variables and include files, plus $prefix and (optional) @layer semi { ... } wrapper.
  3. Sass compilation: Compiles the assembled Sass source with sass.compileString, resolving ~package/... imports against the node_modules tree of the original .scss file.
  4. Prefix patch: For files matching @douyinfe/semi-*/.../env.js, rewrites BASE_CLASS_PREFIX constant to the configured prefixCls. To make the same replacement work for Vite's dev-mode dep pre-bundle (.vite/deps), an esbuild plugin is registered via optimizeDeps.esbuildOptions.plugins to rewrite the env modules at pre-bundle time as well.
  5. omitCss: Optionally comments out .css imports inside Semi packages so they don't reach the bundler.

FAQ

Why are classnames still semi-* in vite dev?

Vite pre-bundles node_modules packages into .vite/deps using esbuild (or Rolldown in Vite 8+). When prefixCls is set, this plugin injects an esbuild plugin to rewrite the relevant env.js files inside the pre-bundle. If you previously started vite once without the plugin (or with a different prefixCls), you may have a stale .vite/deps cache. Stop the dev server and run rm -rf node_modules/.vite to force a clean re-bundle.

Vite 8 prints a deprecation warning about optimizeDeps.esbuildOptions

Vite 8 switched to Rolldown for dep pre-bundling and now prefers optimizeDeps.rolldownOptions. The plugin still uses optimizeDeps.esbuildOptions for broad compatibility with Vite ≥3; Rolldown keeps an esbuild-compatible shim, so the warning is harmless and the prefix rewrite still works correctly.

版本与发布记录

@douyinfe/semi-vite-plugin 共发布 12 个版本,首次发布于 2026/05/20;过去 12 个月发布了 12 个版本。

版本发布日期距今
2.105.0latest2026/11/121 天前
2.104.02026/10/091 个月前
2.103.02026/09/012 个月前
2.102.02026/07/313 个月前
2.101.12026/07/203 个月前
2.101.02026/07/024 个月前

关于 @douyinfe/semi-vite-plugin 的常见问题

@douyinfe/semi-vite-plugin 是什么?

A vite plugin for Semi Design to custom theme, replace prefix and so on.

如何安装 @douyinfe/semi-vite-plugin?

在项目目录运行 npm install @douyinfe/semi-vite-plugin,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(>=3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@douyinfe/semi-vite-plugin 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@douyinfe/semi-vite-plugin 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@douyinfe/semi-vite-plugin 可以商用吗?

@douyinfe/semi-vite-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@douyinfe/semi-vite-plugin 的下载量有多少?

根据 npm 官方下载统计,@douyinfe/semi-vite-plugin 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,059 次,日均约 102 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@douyinfe/semi-vite-plugin 还在维护吗?

最新版本 2.105.0 发布于 2026/11/12(1 天前);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2026/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。