mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 09:21:24 +08:00
refactor(plugin-infra): split functions (#3451)
This commit is contained in:
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,14 +1,9 @@
|
|||||||
/// <reference types="@types/webpack-env" />
|
/// <reference types="@types/webpack-env" />
|
||||||
import 'ses';
|
import 'ses';
|
||||||
|
|
||||||
import * as AFFiNEComponent from '@affine/component';
|
|
||||||
import { DebugLogger } from '@affine/debug';
|
import { DebugLogger } from '@affine/debug';
|
||||||
import { FormatQuickBar } from '@blocksuite/blocks';
|
import { FormatQuickBar } from '@blocksuite/blocks';
|
||||||
import * as BlockSuiteBlocksStd from '@blocksuite/blocks/std';
|
|
||||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
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 {
|
import {
|
||||||
editorItemsAtom,
|
editorItemsAtom,
|
||||||
headerItemsAtom,
|
headerItemsAtom,
|
||||||
@@ -21,14 +16,11 @@ import type {
|
|||||||
CallbackMap,
|
CallbackMap,
|
||||||
PluginContext,
|
PluginContext,
|
||||||
} from '@toeverything/plugin-infra/entry';
|
} from '@toeverything/plugin-infra/entry';
|
||||||
import * as Jotai from 'jotai';
|
|
||||||
import { Provider } from 'jotai/react';
|
import { Provider } from 'jotai/react';
|
||||||
import * as JotaiUtils from 'jotai/utils';
|
|
||||||
import type { PropsWithChildren } from 'react';
|
import type { PropsWithChildren } from 'react';
|
||||||
import * as React from 'react';
|
import { createElement } from 'react';
|
||||||
import * as ReactJSXRuntime from 'react/jsx-runtime';
|
|
||||||
import * as ReactDom from 'react-dom';
|
import { createGlobalThis } from './plugins/setup';
|
||||||
import * as ReactDomClient from 'react-dom/client';
|
|
||||||
|
|
||||||
if (!process.env.COVERAGE) {
|
if (!process.env.COVERAGE) {
|
||||||
lockdown({
|
lockdown({
|
||||||
@@ -50,7 +42,7 @@ const builtinPluginUrl = new Set([
|
|||||||
const logger = new DebugLogger('register-plugins');
|
const logger = new DebugLogger('register-plugins');
|
||||||
|
|
||||||
const PluginProvider = ({ children }: PropsWithChildren) =>
|
const PluginProvider = ({ children }: PropsWithChildren) =>
|
||||||
React.createElement(
|
createElement(
|
||||||
Provider,
|
Provider,
|
||||||
{
|
{
|
||||||
store: rootStore,
|
store: rootStore,
|
||||||
@@ -58,98 +50,6 @@ const PluginProvider = ({ children }: PropsWithChildren) =>
|
|||||||
children
|
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();
|
const group = new DisposableGroup();
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
@@ -159,14 +59,6 @@ declare global {
|
|||||||
|
|
||||||
globalThis.__pluginPackageJson__ = [];
|
globalThis.__pluginPackageJson__ = [];
|
||||||
|
|
||||||
interface PluginLoadedEvent extends CustomEvent<{ plugins: unknown[] }> {}
|
|
||||||
// add to window
|
|
||||||
declare global {
|
|
||||||
interface WindowEventMap {
|
|
||||||
'plugin-loaded': PluginLoadedEvent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
[...builtinPluginUrl].map(url => {
|
[...builtinPluginUrl].map(url => {
|
||||||
return fetch(`${url}/package.json`)
|
return fetch(`${url}/package.json`)
|
||||||
|
|||||||
Reference in New Issue
Block a user