1
+ import { resolveModule } from 'local-pkg'
2
+ import { compare } from 'compare-versions'
1
3
import type { ComponentResolver } from '../../types'
2
- import { kebabCase } from '../utils'
4
+ import { getPkgVersion , kebabCase } from '../utils'
3
5
4
6
const specialComponents : Record < string , string > = {
5
7
CdkVirtualScroll : 'scroll' ,
@@ -37,7 +39,7 @@ export interface IduxResolverOptions {
37
39
/**
38
40
* theme for import style
39
41
*
40
- * @default 'default'
42
+ * @default 'default' for 1.x version
41
43
*/
42
44
importStyleTheme ?: string
43
45
@@ -47,6 +49,13 @@ export interface IduxResolverOptions {
47
49
* @default '@idux'
48
50
*/
49
51
scope ?: string
52
+
53
+ /**
54
+ * specify idux version to load style
55
+ *
56
+ * @default installed version
57
+ */
58
+ version ?: string
50
59
}
51
60
52
61
/**
@@ -57,15 +66,18 @@ export interface IduxResolverOptions {
57
66
export function IduxResolver ( options : IduxResolverOptions = { } ) : ComponentResolver {
58
67
return {
59
68
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
+
62
72
if ( exclude . includes ( name ) )
63
73
return
64
74
65
75
const packageName = getPackageName ( name )
66
76
if ( ! packageName )
67
77
return
68
78
79
+ const resolvedVersion = await getPkgVersion ( `${ scope } /${ packageName } ` , '2.0.0' )
80
+
69
81
let dirname = specialComponents [ name ]
70
82
if ( ! dirname ) {
71
83
const nameIndex = packageName === 'pro' ? 2 : 1
@@ -74,9 +86,7 @@ export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolv
74
86
75
87
const path = `${ scope } /${ packageName } /${ dirname } `
76
88
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 )
80
90
81
91
return { name, from : path , sideEffects }
82
92
} ,
@@ -95,3 +105,32 @@ function getPackageName(name: string) {
95
105
96
106
return packageName
97
107
}
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