# unplugin-element-plus [![Unit Test](https://github.com/element-plus/unplugin-element-plus/actions/workflows/unit-test.yml/badge.svg)](https://github.com/element-plus/unplugin-element-plus/actions/workflows/unit-test.yml) [English](README.md) | [中文](README.zh-CN.md) 这个仓库是用于 `Element Plus` 相关的 [unplugin](https://github.com/unjs/unplugin) 插件工具。感谢 [@antfu](https://github.com/antfu)。 ###### 功能 - 💚 为 Element Plus 按需引入样式。 - 🌎 替换默认语言。 - ⚡️ 使用 unplugin 以支持 Vite, Webpack, Vue CLI, Rollup, esbuild 等。 ## 安装 ```bash npm i unplugin-element-plus -D ```
Vite
```ts // vite.config.ts import ElementPlus from 'unplugin-element-plus/vite' export default { plugins: [ ElementPlus({ // options }), ], } ```
Rollup
```ts // rollup.config.js import ElementPlus from 'unplugin-element-plus/rollup' export default { plugins: [ ElementPlus({ // options }), ], } ```
esbuild
```ts // esbuild.config.js import { build } from 'esbuild' build({ plugins: [ require('unplugin-element-plus/esbuild')({ // options }), ], }) ```
Webpack
```ts // webpack.config.js module.exports = { /* ... */ plugins: [ require('unplugin-element-plus/webpack')({ // options }), ], } ```
Vue CLI
```ts // vue.config.js module.exports = { configureWebpack: { plugins: [ require('unplugin-element-plus/webpack')({ // options }), ], }, } ```
## 使用 插件会自动转换: ```javascript import { ElButton } from 'element-plus' // ↓ ↓ ↓ ↓ ↓ ↓ import { ElButton } from 'element-plus' import 'element-plus/es/components/button/style/css' ``` ## 选项 ### `useSource` ```ts type UseSource = boolean ``` ```javascript // useSource: false import { ElButton } from 'element-plus' // ↓ ↓ ↓ ↓ ↓ ↓ import { ElButton } from 'element-plus' import 'element-plus/es/components/button/style/css' // useSource: true import { ElButton } from 'element-plus' // ↓ ↓ ↓ ↓ ↓ ↓ import { ElButton } from 'element-plus' import 'element-plus/es/components/button/style/index' ``` ### `lib` 一般这个是用不到的,不过作为一个通用选项,还是暴露了出来,如果有用到这个的结构一定要和 ElementPlus 的输出包结构一致,详见 [unpkg.com](https://unpkg.com/element-plus) ```ts type Lib = string ``` default: 'element-plus' ```javascript // lib: 'other-lib' import { ElButton } from 'other-lib' // ↓ ↓ ↓ ↓ ↓ ↓ import { ElButton } from 'other-lib' import 'other-lib/es/components/button/style/css' ``` ### format ```ts type Format = 'esm' | 'cjs' ``` default: 'esm' `esm` 对应 `[lib]/es/components/*` `cjs` 对应 `[lib]/lib/components/*` - `/es` 对应 ES Module 输出 - `/lib` 对应 CommonJS 的输出 使用该选项来选择使用哪一个包。 ```javascript // format: 'cjs' import { ElButton } from 'element-plus' // ↓ ↓ ↓ ↓ ↓ ↓ import { ElButton } from 'element-plus' import 'element-plus/lib/components/button/style/css' ``` ### prefix ```ts type Prefix = string ``` ```javascript // prefix = Al import { AlButton } from 'xx-lib' ``` ### `defaultLocale` 替换默认语言,你可以 [在这](https://github.com/element-plus/element-plus/tree/dev/packages/locale/lang) 查看所有语言列表。 ## 其他插件 - [unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)