From c145885171b084acb7b838ce6d916217345c6cb3 Mon Sep 17 00:00:00 2001 From: saller Date: Wed, 1 May 2024 14:50:38 +0800 Subject: [PATCH] feat(idux): update idux resolver to support v2 version (#722) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李志超44657 <44657@sangfor.com> Co-authored-by: Anthony Fu --- src/core/resolvers/idux.ts | 53 +++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/core/resolvers/idux.ts b/src/core/resolvers/idux.ts index bff2034c..db0398eb 100644 --- a/src/core/resolvers/idux.ts +++ b/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 = { CdkVirtualScroll: 'scroll', @@ -37,7 +39,7 @@ export interface IduxResolverOptions { /** * theme for import style * - * @default 'default' + * @default 'default' for 1.x version */ importStyleTheme?: string @@ -47,6 +49,13 @@ export interface IduxResolverOptions { * @default '@idux' */ scope?: string + + /** + * specify idux version to load style + * + * @default installed version + */ + version?: string } /** @@ -57,8 +66,9 @@ 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 @@ -66,6 +76,8 @@ export function IduxResolver(options: IduxResolverOptions = {}): ComponentResolv if (!packageName) return + const resolvedVersion = await getPkgVersion(`${scope}/${packageName}`, '2.0.0') + let dirname = specialComponents[name] if (!dirname) { const nameIndex = packageName === 'pro' ? 2 : 1 @@ -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 } }, @@ -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 +}