|
1 |
| -import { ComponentResolver } from '../types' |
2 |
| - |
| 1 | +import compareVersions from 'compare-versions' |
| 2 | +import { ComponentResolver, SideEffectsInfo } from '../types' |
| 3 | +import { getPkgVersion, kebabCase } from '../utils' |
3 | 4 | export interface ElementPlusResolverOptions {
|
4 | 5 | /**
|
5 |
| - * import style along with components |
| 6 | + * import style css or sass with components |
| 7 | + * |
| 8 | + * @default 'css' |
| 9 | + */ |
| 10 | + importStyle?: boolean | 'css' | 'sass' |
| 11 | + |
| 12 | + /** |
| 13 | + * specify element-plus version to load style |
6 | 14 | *
|
7 |
| - * @default true |
| 15 | + * @default installed version |
8 | 16 | */
|
9 |
| - importStyle?: boolean |
| 17 | + version?: string |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @deprecated |
| 22 | + * @param partialName |
| 23 | + * @param options |
| 24 | + * |
| 25 | + * @returns |
| 26 | + */ |
| 27 | +function getSideEffectsLagency( |
| 28 | + partialName: string, |
| 29 | + options: ElementPlusResolverOptions, |
| 30 | +): SideEffectsInfo | undefined { |
| 31 | + const { importStyle = 'css' } = options |
| 32 | + if (!importStyle) |
| 33 | + return |
| 34 | + |
| 35 | + if (importStyle === 'sass') { |
| 36 | + return [ |
| 37 | + 'element-plus/packages/theme-chalk/src/base.scss', |
| 38 | + `element-plus/packages/theme-chalk/src/${partialName}.scss`, |
| 39 | + ] |
| 40 | + } |
| 41 | + else if (importStyle === true || importStyle === 'css') { |
| 42 | + return [ |
| 43 | + 'element-plus/lib/theme-chalk/base.css', |
| 44 | + `element-plus/lib/theme-chalk/el-${partialName}.css`, |
| 45 | + ] |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function getSideEffects(dirName: string, options: ElementPlusResolverOptions): SideEffectsInfo | undefined { |
| 50 | + const { importStyle = 'css' } = options |
| 51 | + |
| 52 | + if (importStyle === 'sass') |
| 53 | + return `element-plus/es/components/${dirName}/style` |
| 54 | + |
| 55 | + else if (importStyle === true || importStyle === 'css') |
| 56 | + return `element-plus/es/components/${dirName}/style/css` |
10 | 57 | }
|
11 | 58 |
|
12 | 59 | /**
|
13 | 60 | * Resolver for Element Plus
|
14 | 61 | *
|
15 | 62 | * See https://github.com/antfu/vite-plugin-components/pull/28 for more details
|
| 63 | + * See https://github.com/antfu/vite-plugin-components/issues/117 for more details |
| 64 | + * |
| 65 | + * @author @develar @nabaonan |
| 66 | + * @link https://element-plus.org/#/en-US for element-plus |
16 | 67 | *
|
17 |
| - * @author @develar |
18 |
| - * @link https://element-plus.org/#/en-US |
19 | 68 | */
|
20 |
| -export function ElementPlusResolver(options: ElementPlusResolverOptions = {}): ComponentResolver { |
| 69 | +export function ElementPlusResolver( |
| 70 | + options: ElementPlusResolverOptions = {}, |
| 71 | +): ComponentResolver { |
21 | 72 | return (name: string) => {
|
22 |
| - const { importStyle = true } = options |
23 |
| - |
24 |
| - if (name.startsWith('El')) { |
25 |
| - const partialName = name[2].toLowerCase() + name.substring(3).replace(/[A-Z]/g, l => `-${l.toLowerCase()}`) |
26 |
| - return { |
27 |
| - path: `element-plus/es/el-${partialName}`, |
28 |
| - sideEffects: importStyle ? `element-plus/packages/theme-chalk/src/${partialName}.scss` : undefined, |
| 73 | + if (name.match(/^El[A-Z]/)) { |
| 74 | + let { version } = options |
| 75 | + if (!version) |
| 76 | + version = getPkgVersion('element-plus', '1.0.2') |
| 77 | + let sideEffects |
| 78 | + const partialName = kebabCase(name.slice(2))// ElTableColumn->table-column |
| 79 | + if (compareVersions(version, '1.1.0-beta.1') >= 0) { |
| 80 | + // >=1.1.0-beta.1 |
| 81 | + sideEffects = getSideEffects(partialName, options) |
| 82 | + return { |
| 83 | + importName: name, |
| 84 | + path: 'element-plus/es', |
| 85 | + sideEffects, |
| 86 | + } |
| 87 | + } |
| 88 | + else if (compareVersions(version, '1.0.2-beta.28') >= 0) { |
| 89 | + // >=1.0.2-beta.28 |
| 90 | + sideEffects = getSideEffectsLagency(partialName, options) |
| 91 | + return { |
| 92 | + path: `element-plus/es/el-${partialName}`, |
| 93 | + sideEffects, |
| 94 | + } |
| 95 | + } |
| 96 | + else { |
| 97 | + // for 1.0.1 |
| 98 | + sideEffects = getSideEffectsLagency(partialName, options) |
| 99 | + return { |
| 100 | + path: `element-plus/lib/el-${partialName}`, |
| 101 | + sideEffects, |
| 102 | + } |
29 | 103 | }
|
30 | 104 | }
|
31 | 105 | }
|
|
0 commit comments