Skip to content

Commit c145885

Browse files
sallerli1李志超44657antfu
authored
feat(idux): update idux resolver to support v2 version (#722)
Co-authored-by: 李志超44657 <44657@sangfor.com> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 858ce68 commit c145885

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

src/core/resolvers/idux.ts

+46-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { resolveModule } from 'local-pkg'
2+
import { compare } from 'compare-versions'
13
import type { ComponentResolver } from '../../types'
2-
import { kebabCase } from '../utils'
4+
import { getPkgVersion, kebabCase } from '../utils'
35

46
const specialComponents: Record<string, string> = {
57
CdkVirtualScroll: 'scroll',
@@ -37,7 +39,7 @@ export interface IduxResolverOptions {
3739
/**
3840
* theme for import style
3941
*
40-
* @default 'default'
42+
* @default 'default' for 1.x version
4143
*/
4244
importStyleTheme?: string
4345

@@ -47,6 +49,13 @@ export interface IduxResolverOptions {
4749
* @default '@idux'
4850
*/
4951
scope?: string
52+
53+
/**
54+
* specify idux version to load style
55+
*
56+
* @default installed version
57+
*/
58+
version?: string
5059
}
5160

5261
/**
@@ -57,15 +66,18 @@ export interface IduxResolverOptions {
5766
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
5867
return {
5968
type: 'component',
60-
resolve: (name: string) => {
61-
const { importStyle, importStyleTheme = 'default', exclude = [], scope = '@idux' } = options
69+
resolve: async (name: string) => {
70+
const { importStyle, importStyleTheme, exclude = [], scope = '@idux' } = options
71+
6272
if (exclude.includes(name))
6373
return
6474

6575
const packageName = getPackageName(name)
6676
if (!packageName)
6777
return
6878

79+
const resolvedVersion = await getPkgVersion(`${scope}/${packageName}`, '2.0.0')
80+
6981
let dirname = specialComponents[name]
7082
if (!dirname) {
7183
const nameIndex = packageName === 'pro' ? 2 : 1
@@ -74,9 +86,7 @@ export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolv
7486

7587
const path = `${scope}/${packageName}/${dirname}`
7688

77-
let sideEffects: string | undefined
78-
if (packageName !== 'cdk' && importStyle)
79-
sideEffects = `${path}/style/themes/${importStyle === 'css' ? `${importStyleTheme}_css` : importStyleTheme}`
89+
const sideEffects = packageName === 'cdk' ? undefined : getSideEffects(resolvedVersion, path, importStyle, importStyleTheme)
8090

8191
return { name, from: path, sideEffects }
8292
},
@@ -95,3 +105,32 @@ function getPackageName(name: string) {
95105

96106
return packageName
97107
}
108+
109+
function getSideEffects(version: string, path: string, importStyle?: 'css' | 'less', importStyleTheme?: string): string | string[] | undefined {
110+
if (!importStyle)
111+
return
112+
113+
if (compare(version, '2.0.0-beta.0', '<'))
114+
return getLegacySideEffects(path, importStyle, importStyleTheme)
115+
116+
const styleRoot = `${path}/style`
117+
const themeRoot = `${path}/theme`
118+
119+
const styleImport = `${styleRoot}/${importStyle === 'css' ? 'index_css' : 'index'}`
120+
if (!resolveModule(styleImport))
121+
return
122+
123+
const themeImport = `${themeRoot}/${importStyleTheme}.css`
124+
if (!importStyleTheme || !resolveModule(themeImport))
125+
return styleImport
126+
127+
return [styleImport, `${themeRoot}/${importStyleTheme}`]
128+
}
129+
130+
function getLegacySideEffects(path: string, importStyle: 'css' | 'less', importStyleTheme: string = 'default'): string | undefined {
131+
const styleImport = `${path}/style/themes/${importStyle === 'css' ? `${importStyleTheme}_css` : importStyleTheme}`
132+
if (!resolveModule(styleImport))
133+
return
134+
135+
return styleImport
136+
}

0 commit comments

Comments
 (0)