From be3909370eeacfe4197ba7c5f4dd2ba6127d3e37 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Fri, 28 Jul 2023 22:28:10 -0700 Subject: [PATCH] refactor(plugin-infra): split functions (#3451) --- apps/core/src/bootstrap/plugins/setup.ts | 103 +++++++++++++++++ apps/core/src/bootstrap/register-plugins.ts | 116 +------------------- 2 files changed, 107 insertions(+), 112 deletions(-) create mode 100644 apps/core/src/bootstrap/plugins/setup.ts diff --git a/apps/core/src/bootstrap/plugins/setup.ts b/apps/core/src/bootstrap/plugins/setup.ts new file mode 100644 index 0000000000..dd49e41a75 --- /dev/null +++ b/apps/core/src/bootstrap/plugins/setup.ts @@ -0,0 +1,103 @@ +import * as AFFiNEComponent from '@affine/component'; +import * as BlockSuiteBlocksStd from '@blocksuite/blocks/std'; +import * as BlockSuiteGlobalUtils from '@blocksuite/global/utils'; +import * as Icons from '@blocksuite/icons'; +import * as Atom from '@toeverything/plugin-infra/atom'; +import * as Jotai from 'jotai/index'; +import * as JotaiUtils from 'jotai/utils'; +import * as React from 'react'; +import * as ReactJSXRuntime from 'react/jsx-runtime'; +import * as ReactDom from 'react-dom'; +import * as ReactDomClient from 'react-dom/client'; + +const customRequire = (id: string) => { + if (id === '@toeverything/plugin-infra/atom') { + return Atom; + } + if (id === 'react') { + return React; + } + if (id === 'react/jsx-runtime') { + return ReactJSXRuntime; + } + if (id === 'react-dom') { + return ReactDom; + } + if (id === 'react-dom/client') { + return ReactDomClient; + } + if (id === '@blocksuite/icons') { + return Icons; + } + if (id === '@affine/component') { + return AFFiNEComponent; + } + if (id === '@blocksuite/blocks/std') { + return BlockSuiteBlocksStd; + } + if (id === '@blocksuite/global/utils') { + return BlockSuiteGlobalUtils; + } + if (id === 'jotai') { + return Jotai; + } + if (id === 'jotai/utils') { + return JotaiUtils; + } + throw new Error(`Cannot find module '${id}'`); +}; + +export const createGlobalThis = () => { + return { + process: Object.freeze({ + env: { + NODE_ENV: process.env.NODE_ENV, + }, + }), + // UNSAFE: React will read `window` and `document` + window, + document, + navigator, + userAgent: navigator.userAgent, + // todo(himself65): permission control + fetch: function (input: RequestInfo, init?: RequestInit) { + return globalThis.fetch(input, init); + }, + setTimeout: function (callback: () => void, timeout: number) { + return globalThis.setTimeout(callback, timeout); + }, + clearTimeout: function (id: number) { + return globalThis.clearTimeout(id); + }, + // copilot uses these + crypto: globalThis.crypto, + CustomEvent: globalThis.CustomEvent, + Date: globalThis.Date, + Math: globalThis.Math, + URL: globalThis.URL, + URLSearchParams: globalThis.URLSearchParams, + Headers: globalThis.Headers, + TextEncoder: globalThis.TextEncoder, + TextDecoder: globalThis.TextDecoder, + Request: globalThis.Request, + Error: globalThis.Error, + + // fixme: use our own db api + indexedDB: globalThis.indexedDB, + IDBRequest: globalThis.IDBRequest, + IDBDatabase: globalThis.IDBDatabase, + IDBCursorWithValue: globalThis.IDBCursorWithValue, + IDBFactory: globalThis.IDBFactory, + IDBKeyRange: globalThis.IDBKeyRange, + IDBOpenDBRequest: globalThis.IDBOpenDBRequest, + IDBTransaction: globalThis.IDBTransaction, + IDBObjectStore: globalThis.IDBObjectStore, + IDBIndex: globalThis.IDBIndex, + IDBCursor: globalThis.IDBCursor, + IDBVersionChangeEvent: globalThis.IDBVersionChangeEvent, + + exports: {}, + console: globalThis.console, + require: customRequire, + }; +}; diff --git a/apps/core/src/bootstrap/register-plugins.ts b/apps/core/src/bootstrap/register-plugins.ts index 67972b2984..31713e56b1 100644 --- a/apps/core/src/bootstrap/register-plugins.ts +++ b/apps/core/src/bootstrap/register-plugins.ts @@ -1,14 +1,9 @@ /// import 'ses'; -import * as AFFiNEComponent from '@affine/component'; import { DebugLogger } from '@affine/debug'; import { FormatQuickBar } from '@blocksuite/blocks'; -import * as BlockSuiteBlocksStd from '@blocksuite/blocks/std'; import { DisposableGroup } from '@blocksuite/global/utils'; -import * as BlockSuiteGlobalUtils from '@blocksuite/global/utils'; -import * as Icons from '@blocksuite/icons'; -import * as Atom from '@toeverything/plugin-infra/atom'; import { editorItemsAtom, headerItemsAtom, @@ -21,14 +16,11 @@ import type { CallbackMap, PluginContext, } from '@toeverything/plugin-infra/entry'; -import * as Jotai from 'jotai'; import { Provider } from 'jotai/react'; -import * as JotaiUtils from 'jotai/utils'; import type { PropsWithChildren } from 'react'; -import * as React from 'react'; -import * as ReactJSXRuntime from 'react/jsx-runtime'; -import * as ReactDom from 'react-dom'; -import * as ReactDomClient from 'react-dom/client'; +import { createElement } from 'react'; + +import { createGlobalThis } from './plugins/setup'; if (!process.env.COVERAGE) { lockdown({ @@ -50,7 +42,7 @@ const builtinPluginUrl = new Set([ const logger = new DebugLogger('register-plugins'); const PluginProvider = ({ children }: PropsWithChildren) => - React.createElement( + createElement( Provider, { store: rootStore, @@ -58,98 +50,6 @@ const PluginProvider = ({ children }: PropsWithChildren) => children ); -const customRequire = (id: string) => { - if (id === '@toeverything/plugin-infra/atom') { - return Atom; - } - if (id === 'react') { - return React; - } - if (id === 'react/jsx-runtime') { - return ReactJSXRuntime; - } - if (id === 'react-dom') { - return ReactDom; - } - if (id === 'react-dom/client') { - return ReactDomClient; - } - if (id === '@blocksuite/icons') { - return Icons; - } - if (id === '@affine/component') { - return AFFiNEComponent; - } - if (id === '@blocksuite/blocks/std') { - return BlockSuiteBlocksStd; - } - if (id === '@blocksuite/global/utils') { - return BlockSuiteGlobalUtils; - } - if (id === 'jotai') { - return Jotai; - } - if (id === 'jotai/utils') { - return JotaiUtils; - } - throw new Error(`Cannot find module '${id}'`); -}; - -const createGlobalThis = () => { - return { - process: Object.freeze({ - env: { - NODE_ENV: process.env.NODE_ENV, - }, - }), - // UNSAFE: React will read `window` and `document` - window, - document, - navigator, - userAgent: navigator.userAgent, - // todo(himself65): permission control - fetch: function (input: RequestInfo, init?: RequestInit) { - return globalThis.fetch(input, init); - }, - setTimeout: function (callback: () => void, timeout: number) { - return globalThis.setTimeout(callback, timeout); - }, - clearTimeout: function (id: number) { - return globalThis.clearTimeout(id); - }, - // copilot uses these - crypto: globalThis.crypto, - CustomEvent: globalThis.CustomEvent, - Date: globalThis.Date, - Math: globalThis.Math, - URL: globalThis.URL, - URLSearchParams: globalThis.URLSearchParams, - Headers: globalThis.Headers, - TextEncoder: globalThis.TextEncoder, - TextDecoder: globalThis.TextDecoder, - Request: globalThis.Request, - Error: globalThis.Error, - - // fixme: use our own db api - indexedDB: globalThis.indexedDB, - IDBRequest: globalThis.IDBRequest, - IDBDatabase: globalThis.IDBDatabase, - IDBCursorWithValue: globalThis.IDBCursorWithValue, - IDBFactory: globalThis.IDBFactory, - IDBKeyRange: globalThis.IDBKeyRange, - IDBOpenDBRequest: globalThis.IDBOpenDBRequest, - IDBTransaction: globalThis.IDBTransaction, - IDBObjectStore: globalThis.IDBObjectStore, - IDBIndex: globalThis.IDBIndex, - IDBCursor: globalThis.IDBCursor, - IDBVersionChangeEvent: globalThis.IDBVersionChangeEvent, - - exports: {}, - console: globalThis.console, - require: customRequire, - }; -}; - const group = new DisposableGroup(); declare global { @@ -159,14 +59,6 @@ declare global { globalThis.__pluginPackageJson__ = []; -interface PluginLoadedEvent extends CustomEvent<{ plugins: unknown[] }> {} -// add to window -declare global { - interface WindowEventMap { - 'plugin-loaded': PluginLoadedEvent; - } -} - await Promise.all( [...builtinPluginUrl].map(url => { return fetch(`${url}/package.json`)