Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

vite-plugin-style-import


English| 中文

A plug-in that imports component library styles on demand

Why only import styles


Because vite itself has imported the component library on demand, only the style is not on demand, so just import the style on demand.

Install (yarn or npm)


node version:>=12.0.0

vite version:>=2.0.0

  1. ``` sh
  2. yarn add vite-plugin-style-import -D

  3. ```

or

  1. ``` sh
  2. npm i vite-plugin-style-import -D

  3. ```

Effect


  1. ``` ts
  2. import { Button } from 'ant-design-vue';

  3.         ↓ ↓ ↓ ↓ ↓ ↓

  4. import { Button } from 'ant-design-vue';
  5. import 'ant-design-vue/es/button/style/index.js';
  6. ```

  1. ``` ts
  2. import { ElButton } from 'element-plus';

  3.         ↓ ↓ ↓ ↓ ↓ ↓

  4. // dev
  5. import { Button } from 'element-plus';
  6. import 'element-plus/lib/theme-chalk/el-button.css`;

  7. // prod
  8. import Button from 'element-plus/lib/el-button';
  9. import 'element-plus/lib/theme-chalk/el-button.css';
  10. ```

Usage


Config plugin in vite.config.ts

  1. ``` ts
  2. import { UserConfigExport } from 'vite'
  3. import {
  4.   createStyleImportPlugin,
  5.   AndDesignVueResolve,
  6.   VantResolve,
  7.   ElementPlusResolve,
  8.   NutuiResolve,
  9.   AntdResolve,
  10. } from 'vite-plugin-style-import'

  11. export default (): UserConfigExport => {
  12.   return {
  13.     // 1. If you are using the ant-design series, you need to configure this
  14.     // 2. Make sure less is installed in the dependency `yarn add less -D`
  15.     css: {
  16.       preprocessorOptions: {
  17.         less: {
  18.           javascriptEnabled: true,
  19.         },
  20.       },
  21.     },
  22.     plugins: [
  23.       createStyleImportPlugin({
  24.         resolves: [
  25.           AndDesignVueResolve(),
  26.           VantResolve(),
  27.           ElementPlusResolve(),
  28.           NutuiResolve(),
  29.           AntdResolve(),
  30.         ],
  31.         libs: [
  32.           // If you don’t have the resolve you need, you can write it directly in the lib, or you can provide us with PR
  33.           {
  34.             libraryName: 'ant-design-vue',
  35.             esModule: true,
  36.             resolveStyle: (name) => {
  37.               return `ant-design-vue/es/${name}/style/index`
  38.             },
  39.           },
  40.         ],
  41.       }),
  42.     ],
  43.   }
  44. }
  45. ```

Options


param type default description
:--- :--- :--- :---
include string、RegExp、(string、RegExp)[]、null、undefined `['*/.js', '*/.ts', '*/.tsx', '*/.jsx']` Code directory and file format to be converted
exclude string、RegExp、(string、RegExp)[]、null、undefined `'node_modules/'` Excluded files/folders
libs Lib[] List of libraries to be imported
resolves Lib[] List of libraries to be imported (built-in by the plugin)

Lib

  1. ``` ts
  2. {
  3.   // Import names that meet this rule will take effect. The default is null, which can be applied to resolveComponent and resolveStyle at the same time
  4.   importTest?: Regexp;

  5.   // Need to imported  library name
  6.   libraryName: string;

  7.   // Custom style file conversion
  8.   resolveStyle: (name: string) => string;

  9.   // Name conversion for library export
  10.   // default: paramCase
  11.   libraryNameChangeCase?: LibraryNameChangeCase;

  12.   // If the style file is not .css suffix. Need to turn on this option
  13.   // default: false
  14.   esModule?: boolean;

  15.   /**
  16.    * There may be some component libraries that are not very standardized.
  17.    * You can turn on this to ignore to determine whether the file exists. Prevent errors when importing non-existent css files.
  18.    * Performance may be slightly reduced after it is turned on, but the impact is not significant
  19.    * default: false
  20.    */
  21.   ensureStyleFile?: boolean;

  22. }

  23. // LibraryNameChangeCase

  24. export type LibraryNameChangeCase = ChangeCaseType | ((name: string) => string);

  25. export type ChangeCaseType =
  26.   | 'camelCase'
  27.   | 'capitalCase'
  28.   | 'constantCase'
  29.   | 'dotCase'
  30.   | 'headerCase'
  31.   | 'noCase'
  32.   | 'paramCase'
  33.   | 'pascalCase'
  34.   | 'pathCase'
  35.   | 'sentenceCase'
  36.   | 'snakeCase';

  37. ```

Example


Run Example

  1. ``` shell
  2. pnpm install
  3. cd packages/playground/basic
  4. pnpm run dev
  5. pnpm run build
  6. ```

Sample project


Vben Admin

License


MIT
Last Updated: 2023-05-23 11:11:51