Skip to content

Commit

Permalink
feat(idux): update idux resolver to support v2 version (#722)
Browse files Browse the repository at this point in the history
Co-authored-by: 李志超44657 <44657@sangfor.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed May 1, 2024
1 parent 858ce68 commit c145885
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/core/resolvers/idux.ts
@@ -1,5 +1,7 @@
import { resolveModule } from 'local-pkg'
import { compare } from 'compare-versions'
import type { ComponentResolver } from '../../types'
import { kebabCase } from '../utils'
import { getPkgVersion, kebabCase } from '../utils'

const specialComponents: Record<string, string> = {
CdkVirtualScroll: 'scroll',
Expand Down Expand Up @@ -37,7 +39,7 @@ export interface IduxResolverOptions {
/**
* theme for import style
*
* @default 'default'
* @default 'default' for 1.x version
*/
importStyleTheme?: string

Expand All @@ -47,6 +49,13 @@ export interface IduxResolverOptions {
* @default '@idux'
*/
scope?: string

/**
* specify idux version to load style
*
* @default installed version
*/
version?: string
}

/**
Expand All @@ -57,15 +66,18 @@ export interface IduxResolverOptions {
export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolver {
return {
type: 'component',
resolve: (name: string) => {
const { importStyle, importStyleTheme = 'default', exclude = [], scope = '@idux' } = options
resolve: async (name: string) => {
const { importStyle, importStyleTheme, exclude = [], scope = '@idux' } = options

if (exclude.includes(name))
return

const packageName = getPackageName(name)
if (!packageName)
return

const resolvedVersion = await getPkgVersion(`${scope}/${packageName}`, '2.0.0')

let dirname = specialComponents[name]
if (!dirname) {
const nameIndex = packageName === 'pro' ? 2 : 1
Expand All @@ -74,9 +86,7 @@ export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolv

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

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

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

return packageName
}

function getSideEffects(version: string, path: string, importStyle?: 'css' | 'less', importStyleTheme?: string): string | string[] | undefined {
if (!importStyle)
return

if (compare(version, '2.0.0-beta.0', '<'))
return getLegacySideEffects(path, importStyle, importStyleTheme)

const styleRoot = `${path}/style`
const themeRoot = `${path}/theme`

const styleImport = `${styleRoot}/${importStyle === 'css' ? 'index_css' : 'index'}`
if (!resolveModule(styleImport))
return

const themeImport = `${themeRoot}/${importStyleTheme}.css`
if (!importStyleTheme || !resolveModule(themeImport))
return styleImport

return [styleImport, `${themeRoot}/${importStyleTheme}`]
}

function getLegacySideEffects(path: string, importStyle: 'css' | 'less', importStyleTheme: string = 'default'): string | undefined {
const styleImport = `${path}/style/themes/${importStyle === 'css' ? `${importStyleTheme}_css` : importStyleTheme}`
if (!resolveModule(styleImport))
return

return styleImport
}

0 comments on commit c145885

Please sign in to comment.