From 4c38484f3370acd69055d3f90b2fcf4e7d7d211f Mon Sep 17 00:00:00 2001 From: "zhili.wzl" Date: Mon, 20 Dec 2021 16:20:09 +0800 Subject: [PATCH] feat: support setNpmCDNRegistry --- formily/antd/playground/main.tsx | 7 ++++++- .../components/ReactionsSetter/declarations.ts | 4 ++-- .../src/components/ReactionsSetter/index.tsx | 2 +- .../src/components/MonacoInput/format.ts | 4 ++-- packages/react-settings-form/src/index.ts | 1 + packages/react-settings-form/src/registry.ts | 16 ++++++++++++++++ .../react-settings-form/src/shared/loadScript.ts | 3 ++- 7 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 packages/react-settings-form/src/registry.ts diff --git a/formily/antd/playground/main.tsx b/formily/antd/playground/main.tsx index ceef4f3a0..6614fba9f 100644 --- a/formily/antd/playground/main.tsx +++ b/formily/antd/playground/main.tsx @@ -18,7 +18,10 @@ import { SettingsPanel, ComponentTreeWidget, } from '@designable/react' -import { SettingsForm } from '@designable/react-settings-form' +import { + SettingsForm, + setNpmCDNRegistry, +} from '@designable/react-settings-form' import { createDesigner, GlobalRegistry, @@ -63,6 +66,8 @@ import { FormGrid, } from '../src' +setNpmCDNRegistry('//unpkg.com') + GlobalRegistry.registerDesignerLocales({ 'zh-CN': { sources: { diff --git a/formily/setters/src/components/ReactionsSetter/declarations.ts b/formily/setters/src/components/ReactionsSetter/declarations.ts index c1698d49d..2e551e439 100644 --- a/formily/setters/src/components/ReactionsSetter/declarations.ts +++ b/formily/setters/src/components/ReactionsSetter/declarations.ts @@ -1,4 +1,4 @@ -import { MonacoInput } from '@designable/react-settings-form' +import { MonacoInput, getNpmCDNRegistry } from '@designable/react-settings-form' export interface IDependency { name: string @@ -10,7 +10,7 @@ const loadDependencies = async (deps: IDependency[]) => { deps.map(async ({ name, path }) => ({ name, path, - library: await fetch(`//cdn.jsdelivr.net/npm/${name}/${path}`).then( + library: await fetch(`${getNpmCDNRegistry()}/${name}/${path}`).then( (res) => res.text() ), })) diff --git a/formily/setters/src/components/ReactionsSetter/index.tsx b/formily/setters/src/components/ReactionsSetter/index.tsx index de3ccd596..b5dccc3c5 100644 --- a/formily/setters/src/components/ReactionsSetter/index.tsx +++ b/formily/setters/src/components/ReactionsSetter/index.tsx @@ -320,7 +320,7 @@ export const ReactionsSetter: React.FC = (props) => { const property = field .query('.property') .get('inputValues') - property[0] = property[0] ?? 'value' + property[0] = property[0] || 'value' field.query('.source').take((source) => { if (isVoidField(source)) return if (source.value) { diff --git a/packages/react-settings-form/src/components/MonacoInput/format.ts b/packages/react-settings-form/src/components/MonacoInput/format.ts index 7a8023aae..2d646967e 100644 --- a/packages/react-settings-form/src/components/MonacoInput/format.ts +++ b/packages/react-settings-form/src/components/MonacoInput/format.ts @@ -1,5 +1,5 @@ import { parse } from '@babel/parser' - +import { getNpmCDNRegistry } from '../../registry' interface IPrettierModule { default: { format( @@ -13,7 +13,7 @@ interface IPrettierModule { } const prettier: Promise = new Function( - 'return import("https://cdn.jsdelivr.net/npm/prettier@2.x/esm/standalone.mjs")' + `return import("${getNpmCDNRegistry()}/prettier@2.x/esm/standalone.mjs")` )() export const format = async (language: string, source: string) => { diff --git a/packages/react-settings-form/src/index.ts b/packages/react-settings-form/src/index.ts index 5aba7bfd4..c247a6550 100644 --- a/packages/react-settings-form/src/index.ts +++ b/packages/react-settings-form/src/index.ts @@ -1,4 +1,5 @@ import './locales' +export * from './registry' export * from './components' export * from './SchemaField' export * from './SettingsForm' diff --git a/packages/react-settings-form/src/registry.ts b/packages/react-settings-form/src/registry.ts new file mode 100644 index 000000000..8be5e21a0 --- /dev/null +++ b/packages/react-settings-form/src/registry.ts @@ -0,0 +1,16 @@ +import loader from '@monaco-editor/loader' + +const Registry = { + cdn: '//cdn.jsdelivr.net/npm', +} + +export const setNpmCDNRegistry = (registry: string) => { + Registry.cdn = registry + loader.config({ + paths: { + vs: `${registry}/monaco-editor@0.30.1/min/vs`, + }, + }) +} + +export const getNpmCDNRegistry = () => String(Registry.cdn).replace(/\/$/, '') diff --git a/packages/react-settings-form/src/shared/loadScript.ts b/packages/react-settings-form/src/shared/loadScript.ts index 8158a2561..f2088d353 100644 --- a/packages/react-settings-form/src/shared/loadScript.ts +++ b/packages/react-settings-form/src/shared/loadScript.ts @@ -1,3 +1,4 @@ +import { getNpmCDNRegistry } from '../registry' export interface ILoadScriptProps { package: string entry: string @@ -7,7 +8,7 @@ export interface ILoadScriptProps { export const loadScript = async (props: ILoadScriptProps) => { const options: ILoadScriptProps = { - base: '//cdn.jsdelivr.net/npm', + base: getNpmCDNRegistry(), ...props, } if (window[props.root]) return window[options.root]