Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export interface AppConfig {
* 视图组件内默认的组件样式资源 CDN 来源
* 默认值为 'alipay'
*/
componentCDNType?: 'unpkg' | 'jsdelivr' | 'alipay';
componentCDNType?: TComponentCDNType;
/**
* 指定前端是否为 Electron 环境 (Electron Renderer)
* 为兼容老的逻辑,若不兼容则 fallback 到 isElectronRenderer 的判断
Expand Down Expand Up @@ -277,3 +277,5 @@ export function ConfigProvider(props: React.PropsWithChildren<{ value: AppConfig

return React.createElement(extraContextProvider, { children: app });
}

export type TComponentCDNType = 'unpkg' | 'jsdelivr' | 'alipay' | 'npmmirror';
23 changes: 10 additions & 13 deletions packages/extension/src/browser/shadowRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';

import { ComponentContextProvider, IconContext, IIconResourceOptions } from '@opensumi/ide-components';
import { DisposableCollection, LabelService, useInjectable } from '@opensumi/ide-core-browser';
import { DisposableCollection, LabelService, TComponentCDNType, useInjectable } from '@opensumi/ide-core-browser';
import { ExtensionBrowserStyleSheet, localize, URI } from '@opensumi/ide-core-common';
import { getThemeTypeSelector, IIconService, IThemeService, ThemeType } from '@opensumi/ide-theme';

Expand All @@ -19,20 +19,15 @@ function cloneNode<T>(head): T {
return head.cloneNode(true);
}

const CDN_TYPE_MAP: CDNTypeMap = {
type IComponentCDNTypeMap = Record<TComponentCDNType, string>;

const CDN_TYPE_MAP: IComponentCDNTypeMap = {
alipay: 'https://gw.alipayobjects.com/os/lib',
npmmirror: 'https://registry.npmmirror.com',
unpkg: 'https://unpkg.com/browse',
jsdelivr: 'https://cdn.jsdelivr.net/npm',
};

interface CDNTypeMap {
alipay: string;
unpkg: string;
jsdelivr: string;
}

type CDNType = keyof CDNTypeMap;

/**
* 由于经过 clone 以后,实际 Shadow DOM 中 head 与原始 proxiedHead 不是同一份引用
* 插件中可能存在后置动态插入 style 的行为,此时只会获取到 proxiedHead
Expand Down Expand Up @@ -74,9 +69,11 @@ function useMutationObserver(from: HTMLHeadElement, target: HTMLHeadElement) {

const packageName = '@opensumi/ide-components';

function getCdnHref(filePath: string, version: string, cdnType: CDNType = 'alipay') {
function getCdnHref(filePath: string, version: string, cdnType: TComponentCDNType = 'alipay') {
if (cdnType === 'alipay') {
return `${CDN_TYPE_MAP['alipay']}/${packageName.slice(1)}/${version}/${filePath}`;
} else if (cdnType === 'npmmirror') {
return `${CDN_TYPE_MAP['npmmirror']}/${packageName}/${version}/files/${filePath}`;
} else {
return `${CDN_TYPE_MAP[cdnType]}/${packageName}@${version}/${filePath}`;
}
Expand All @@ -101,7 +98,7 @@ const ShadowRoot = ({
extensionId: string;
children: any;
proxiedHead: HTMLHeadElement;
cdnType?: CDNType;
cdnType?: TComponentCDNType;
styleSheet?: ExtensionBrowserStyleSheet;
}) => {
const shadowRootRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -183,7 +180,7 @@ export function getShadowRoot(
props,
id,
proxiedHead,
type?: CDNType,
type?: TComponentCDNType,
extensionBrowserStyleSheet?: ExtensionBrowserStyleSheet,
) {
const Component = panel;
Expand Down