Skip to content

Commit

Permalink
feat(idux): update idux resolver to support v2 version
Browse files Browse the repository at this point in the history
  • Loading branch information
李志超44657 authored and sallerli1 committed Jan 5, 2024
1 parent aeaf51d commit 189da5f
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions src/core/resolvers/idux.ts
Original file line number Diff line number Diff line change
@@ -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 @@ -36,8 +38,8 @@ export interface IduxResolverOptions {
importStyle?: 'css' | 'less'
/**
* 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 189da5f

Please sign in to comment.