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
yarn add vite-plugin-style-import -D
or
npm i vite-plugin-style-import -D
Effect
import { Button } from 'ant-design-vue';
↓ ↓ ↓ ↓ ↓ ↓
import { Button } from 'ant-design-vue';
import 'ant-design-vue/es/button/style/index.js';
import { ElButton } from 'element-plus';
↓ ↓ ↓ ↓ ↓ ↓
import { Button } from 'element-plus';
import 'element-plus/lib/theme-chalk/el-button.css`;
// prod
import Button from 'element-plus/lib/el-button';
import 'element-plus/lib/theme-chalk/el-button.css';
Usage
- Config plugin in vite.config.ts
import { UserConfigExport } from 'vite'
import styleImport from 'vite-plugin-style-import'
export default (): UserConfigExport => {
return {
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
},
},
},
plugins: [
styleImport({
libs: [
{
libraryName: 'ant-design-vue',
esModule: true,
resolveStyle: (name) => {
return `ant-design-vue/es/${name}/style/index`
},
},
{
libraryName: 'antd',
esModule: true,
resolveStyle: (name) => {
return `antd/es/${name}/style/index`
},
},
{
libraryName: 'vant',
esModule: true,
resolveStyle: (name) => {
return `vant/es/${name}/style`
},
},
{
libraryName: 'element-plus',
base: 'element-plus/lib/theme-chalk/base.css',
resolveStyle: (name) => {
return `element-plus/lib/theme-chalk/${name}.css`
},
resolveComponent: (name) => {
return `element-plus/lib/${name}`
},
},
],
}),
],
}
}
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 |
Lib
{
importTest?: Regexp;
libraryName: string;
resolveStyle: (name: string) => string;
libraryNameChangeCase?: LibraryNameChangeCase;
esModule?: boolean;
ensureStyleFile?: boolean;
resolveComponent?: (name: string) => string;
transformComponentImportName?: (name: string) => string;
}
export type LibraryNameChangeCase = ChangeCaseType | ((name: string) => string);
export type ChangeCaseType =
| 'camelCase'
| 'capitalCase'
| 'constantCase'
| 'dotCase'
| 'headerCase'
| 'noCase'
| 'paramCase'
| 'pascalCase'
| 'pathCase'
| 'sentenceCase'
| 'snakeCase';
Example
Run Example
cd ./example
yarn install
yarn serve
Sample project
Vben Admin
License
MIT