vite-plugin-imp
- A vite plugin for import ui library component style automatic.
- It can also import library like lodash on demand.
import { forEach } from 'lodash'
forEach([1,2], console.log)
to
import forEach from 'lodash/forEach'
forEach([1,2], console.log)
import { Progress } from 'vant'
to
import { Progress } from 'vant'
import 'vant/es/progress/style/index.js'
install
npm i vite-plugin-imp -D
or
yarn add vite-plugin-imp -D
Usage
import { defineConfig } from 'vite'
import vitePluginImp from 'vite-plugin-imp'
export default defineConfig({
plugins: [vitePluginImp(config)]
})
config interface is ImpConfig:
export interface LibItem {
libName: string
style: (name: string) => string | string[] | boolean
libDirectory?: string
camel2DashComponentName?: boolean
replaceOldImport?: boolean
}
interface ImpConfig {
libList: libItem[]
exclude?: string[]
ignoreStylePathNotFound?: boolean
transpileDependencies?: boolean | Array<string | RegExp>
}
More libraries usage
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vitePluginImp from 'vite-plugin-imp'
export default defineConfig({
plugins: [
vitePluginImp({
libList: [
{
libName: 'lodash',
libDirectory: '',
camel2DashComponentName: false
},
{
libName: 'antd',
style(name) {
return `antd/es/${name}/style/index.js`
}
},
]
})
]
})
Built-in Support popular library
You should put these built-in supported libraries in your dependencies
field in package.json.
If your project is using libraries that mentioned above, you just need use it like:
export default defineConfig({
plugins: [
vitePluginImp()
]
})
If you want to support a library built-in, feel free to open a issue.
just use the component like below, component style will be auto injected.
import { defineComponent } from 'vue'
import { Progress } from 'vant'
import { ElButton } from 'element-plus'
export default defineComponent({
setup() {
return () => {
return (
<div>
<Progress percentage={3} />
<ElButton>element-plus button</ElButton>
</div>
)
}
}
})
You can set camel2DashComponentName to false to disable transfer from camel to dash:
vitePluginImp({
libList: [
{
libName: 'custom-lib',
camel2DashComponentName: false,
style: (name) => {
return`custom-lib/lib/${name}/index.css`
}
}
]
})
plugin V1.x (No more updates) Usage
const vitePluginImpCreator = require('vite-plugin-imp')
const vitePluginImp = vitePluginImpCreator({
optimize: true,
libList: [
{
libraryName: 'vant',
style: (name) => {
return `vant/es/${name}/index.css`
}
},
{
libraryName: 'element-plus',
style: (name) => {
return`element-plus/lib/theme-chalk/${name}.css`
}
}
]
})
module.exports = {
plugins: [vitePluginImp]
}