Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 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 @@ -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
}