fix: plugin infra (#3398)

This commit is contained in:
Alex Yang 2023-07-26 22:36:29 -07:00 committed by GitHub
parent 1e72d3c270
commit 780fffb88f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 93 additions and 52 deletions

View File

@ -111,6 +111,7 @@ export const createConfiguration: (
: 'eval-cheap-module-source-map', : 'eval-cheap-module-source-map',
resolve: { resolve: {
symlinks: true,
extensionAlias: { extensionAlias: {
'.js': ['.js', '.tsx', '.ts'], '.js': ['.js', '.tsx', '.ts'],
'.mjs': ['.mjs', '.mts'], '.mjs': ['.mjs', '.mts'],

View File

@ -1,4 +1,4 @@
import { currentPageIdAtom } from '@toeverything/plugin-infra/manager'; import { currentPageIdAtom } from '@toeverything/plugin-infra/atom';
import { atom } from 'jotai/vanilla'; import { atom } from 'jotai/vanilla';
import { pageSettingFamily } from './index'; import { pageSettingFamily } from './index';

View File

@ -17,7 +17,7 @@ import {
} from '@affine/workspace/migration'; } from '@affine/workspace/migration';
import { createIndexedDBDownloadProvider } from '@affine/workspace/providers'; import { createIndexedDBDownloadProvider } from '@affine/workspace/providers';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import { rootStore } from '@toeverything/plugin-infra/manager'; import { rootStore } from '@toeverything/plugin-infra/atom';
import { WorkspaceAdapters } from '../adapters/workspace'; import { WorkspaceAdapters } from '../adapters/workspace';

View File

@ -6,18 +6,18 @@ 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 BlockSuiteGlobalUtils from '@blocksuite/global/utils';
import * as Icons from '@blocksuite/icons'; import * as Icons from '@blocksuite/icons';
import type { import * as Manager from '@toeverything/plugin-infra/atom';
CallbackMap,
PluginContext,
} from '@toeverything/plugin-infra/entry';
import * as Manager from '@toeverything/plugin-infra/manager';
import { import {
editorItemsAtom, editorItemsAtom,
headerItemsAtom, headerItemsAtom,
registeredPluginAtom, registeredPluginAtom,
rootStore, rootStore,
windowItemsAtom, windowItemsAtom,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import type {
CallbackMap,
PluginContext,
} from '@toeverything/plugin-infra/entry';
import * as Jotai from 'jotai'; import * as Jotai from 'jotai';
import { Provider } from 'jotai/react'; import { Provider } from 'jotai/react';
import * as JotaiUtils from 'jotai/utils'; import * as JotaiUtils from 'jotai/utils';
@ -96,6 +96,8 @@ const createGlobalThis = () => {
document, document,
navigator, navigator,
userAgent: navigator.userAgent, userAgent: navigator.userAgent,
// todo: permission control
fetch: globalThis.fetch,
// fixme: use our own db api // fixme: use our own db api
indexedDB: globalThis.indexedDB, indexedDB: globalThis.indexedDB,
@ -135,20 +137,19 @@ await Promise.all(
return fetch(entryURL).then(async res => { return fetch(entryURL).then(async res => {
if (assets.length > 0) { if (assets.length > 0) {
await Promise.all( await Promise.all(
assets.map(asset => { assets.map(async asset => {
if (asset.endsWith('.css')) { if (asset.endsWith('.css')) {
return fetch(new URL(asset, baseURL)).then(res => { const res = await fetch(new URL(asset, baseURL));
if (res.ok) { if (res.ok) {
// todo: how to put css file into sandbox? // todo: how to put css file into sandbox?
return res.text().then(text => { return res.text().then(text => {
const style = document.createElement('style'); const style = document.createElement('style');
style.setAttribute('plugin-id', plugin); style.setAttribute('plugin-id', plugin);
style.textContent = text; style.textContent = text;
document.head.appendChild(style); document.head.appendChild(style);
}); });
} }
return null; return null;
});
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }

View File

@ -9,7 +9,7 @@ import {
currentPageIdAtom, currentPageIdAtom,
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
rootStore, rootStore,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import { useAtomValue } from 'jotai/react'; import { useAtomValue } from 'jotai/react';
import { Provider } from 'jotai/react'; import { Provider } from 'jotai/react';
import type { ErrorInfo, ReactElement, ReactNode } from 'react'; import type { ErrorInfo, ReactElement, ReactNode } from 'react';

View File

@ -3,7 +3,7 @@ import {
SettingWrapper, SettingWrapper,
} from '@affine/component/setting-components'; } from '@affine/component/setting-components';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { registeredPluginAtom } from '@toeverything/plugin-infra/manager'; import { registeredPluginAtom } from '@toeverything/plugin-infra/atom';
import { useAtomValue } from 'jotai'; import { useAtomValue } from 'jotai';
export const Plugins = () => { export const Plugins = () => {

View File

@ -14,7 +14,7 @@ import {
useBlockSuitePageMeta, useBlockSuitePageMeta,
usePageMetaHelper, usePageMetaHelper,
} from '@toeverything/hooks/use-block-suite-page-meta'; } from '@toeverything/hooks/use-block-suite-page-meta';
import { currentPageIdAtom } from '@toeverything/plugin-infra/manager'; import { currentPageIdAtom } from '@toeverything/plugin-infra/atom';
import { useAtom, useAtomValue } from 'jotai'; import { useAtom, useAtomValue } from 'jotai';
import { useState } from 'react'; import { useState } from 'react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';

View File

@ -3,7 +3,7 @@ import { WorkspaceSubPath } from '@affine/env/workspace';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta'; import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
import { currentPageIdAtom } from '@toeverything/plugin-infra/manager'; import { currentPageIdAtom } from '@toeverything/plugin-infra/atom';
import { useAtomValue } from 'jotai'; import { useAtomValue } from 'jotai';
import { useCallback, useState } from 'react'; import { useCallback, useState } from 'react';

View File

@ -7,7 +7,7 @@ import { SidebarSwitch } from '@affine/component/app-sidebar/sidebar-header';
import { isDesktop } from '@affine/env/constant'; import { isDesktop } from '@affine/env/constant';
import { CloseIcon, MinusIcon, RoundedRectangleIcon } from '@blocksuite/icons'; import { CloseIcon, MinusIcon, RoundedRectangleIcon } from '@blocksuite/icons';
import type { Page } from '@blocksuite/store'; import type { Page } from '@blocksuite/store';
import { headerItemsAtom } from '@toeverything/plugin-infra/manager'; import { headerItemsAtom } from '@toeverything/plugin-infra/atom';
import { useAtomValue } from 'jotai'; import { useAtomValue } from 'jotai';
import type { FC, HTMLAttributes, PropsWithChildren, ReactNode } from 'react'; import type { FC, HTMLAttributes, PropsWithChildren, ReactNode } from 'react';
import { import {

View File

@ -7,13 +7,13 @@ import { assertExists } from '@blocksuite/global/utils';
import type { Page, Workspace } from '@blocksuite/store'; import type { Page, Workspace } from '@blocksuite/store';
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta'; import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
import { useBlockSuiteWorkspacePage } from '@toeverything/hooks/use-block-suite-workspace-page'; import { useBlockSuiteWorkspacePage } from '@toeverything/hooks/use-block-suite-workspace-page';
import type { CallbackMap } from '@toeverything/plugin-infra/entry';
import { import {
contentLayoutAtom, contentLayoutAtom,
editorItemsAtom, editorItemsAtom,
rootStore, rootStore,
windowItemsAtom, windowItemsAtom,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import type { CallbackMap } from '@toeverything/plugin-infra/entry';
import type { LayoutNode } from '@toeverything/plugin-infra/type'; import type { LayoutNode } from '@toeverything/plugin-infra/type';
import clsx from 'clsx'; import clsx from 'clsx';
import { useAtomValue, useSetAtom } from 'jotai'; import { useAtomValue, useSetAtom } from 'jotai';

View File

@ -2,7 +2,7 @@ import { assertExists } from '@blocksuite/global/utils';
import { import {
currentPageIdAtom, currentPageIdAtom,
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import { useAtom, useSetAtom } from 'jotai'; import { useAtom, useSetAtom } from 'jotai';
import { useCallback, useEffect } from 'react'; import { useCallback, useEffect } from 'react';

View File

@ -1,6 +1,6 @@
import type { WorkspaceRegistry } from '@affine/env/workspace'; import type { WorkspaceRegistry } from '@affine/env/workspace';
import type { WorkspaceFlavour } from '@affine/env/workspace'; import type { WorkspaceFlavour } from '@affine/env/workspace';
import { currentPageIdAtom } from '@toeverything/plugin-infra/manager'; import { currentPageIdAtom } from '@toeverything/plugin-infra/atom';
import { useSetAtom } from 'jotai'; import { useSetAtom } from 'jotai';
import { useCallback } from 'react'; import { useCallback } from 'react';

View File

@ -28,7 +28,7 @@ import {
useSensors, useSensors,
} from '@dnd-kit/core'; } from '@dnd-kit/core';
import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/react'; import { usePassiveWorkspaceEffect } from '@toeverything/plugin-infra/__internal__/react';
import { currentWorkspaceIdAtom } from '@toeverything/plugin-infra/manager'; import { currentWorkspaceIdAtom } from '@toeverything/plugin-infra/atom';
import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import type { FC, PropsWithChildren, ReactElement } from 'react'; import type { FC, PropsWithChildren, ReactElement } from 'react';
import { lazy, Suspense, useCallback, useMemo } from 'react'; import { lazy, Suspense, useCallback, useMemo } from 'react';

View File

@ -1,7 +1,7 @@
import { DebugLogger } from '@affine/debug'; import { DebugLogger } from '@affine/debug';
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace'; import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
import { rootStore } from '@toeverything/plugin-infra/manager'; import { rootStore } from '@toeverything/plugin-infra/atom';
import { lazy } from 'react'; import { lazy } from 'react';
import type { LoaderFunction } from 'react-router-dom'; import type { LoaderFunction } from 'react-router-dom';
import { redirect } from 'react-router-dom'; import { redirect } from 'react-router-dom';

View File

@ -3,10 +3,7 @@ import { DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX } from '@affine/env/constant';
import { WorkspaceSubPath } from '@affine/env/workspace'; import { WorkspaceSubPath } from '@affine/env/workspace';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import { getActiveBlockSuiteWorkspaceAtom } from '@toeverything/plugin-infra/__internal__/workspace'; import { getActiveBlockSuiteWorkspaceAtom } from '@toeverything/plugin-infra/__internal__/workspace';
import { import { currentPageIdAtom, rootStore } from '@toeverything/plugin-infra/atom';
currentPageIdAtom,
rootStore,
} from '@toeverything/plugin-infra/manager';
import { useAtom } from 'jotai/react'; import { useAtom } from 'jotai/react';
import { useCallback, useEffect } from 'react'; import { useCallback, useEffect } from 'react';
import type { LoaderFunction } from 'react-router-dom'; import type { LoaderFunction } from 'react-router-dom';

View File

@ -7,10 +7,7 @@ import { WorkspaceSubPath } from '@affine/env/workspace';
import type { EditorContainer } from '@blocksuite/editor'; import type { EditorContainer } from '@blocksuite/editor';
import { assertExists } from '@blocksuite/global/utils'; import { assertExists } from '@blocksuite/global/utils';
import type { Page } from '@blocksuite/store'; import type { Page } from '@blocksuite/store';
import { import { currentPageIdAtom, rootStore } from '@toeverything/plugin-infra/atom';
currentPageIdAtom,
rootStore,
} from '@toeverything/plugin-infra/manager';
import { useAtomValue } from 'jotai'; import { useAtomValue } from 'jotai';
import { useAtom } from 'jotai/react'; import { useAtom } from 'jotai/react';
import { type ReactElement, useCallback, useEffect } from 'react'; import { type ReactElement, useCallback, useEffect } from 'react';

View File

@ -2,7 +2,7 @@ import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
import { import {
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
rootStore, rootStore,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import type { ReactElement } from 'react'; import type { ReactElement } from 'react';
import { type LoaderFunction, Outlet, redirect } from 'react-router-dom'; import { type LoaderFunction, Outlet, redirect } from 'react-router-dom';

View File

@ -5,7 +5,7 @@ import { arrayMove } from '@dnd-kit/sortable';
import { import {
currentPageIdAtom, currentPageIdAtom,
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import type { FC, ReactElement } from 'react'; import type { FC, ReactElement } from 'react';
import { lazy, Suspense, useCallback, useTransition } from 'react'; import { lazy, Suspense, useCallback, useTransition } from 'react';

View File

@ -4,6 +4,7 @@ import path from 'node:path';
import type { BuildFlags } from '../config/index.js'; import type { BuildFlags } from '../config/index.js';
import { projectRoot } from '../config/index.js'; import { projectRoot } from '../config/index.js';
import { buildI18N } from '../util/i18n.js'; import { buildI18N } from '../util/i18n.js';
import { buildInfra } from '../util/infra.js';
const cwd = path.resolve(projectRoot, 'apps', 'core'); const cwd = path.resolve(projectRoot, 'apps', 'core');
@ -45,6 +46,7 @@ const flags = {
} satisfies BuildFlags; } satisfies BuildFlags;
buildI18N(); buildI18N();
await buildInfra();
spawn( spawn(
'node', 'node',
[ [

View File

@ -7,6 +7,7 @@ import { config } from 'dotenv';
import { type BuildFlags, projectRoot } from '../config/index.js'; import { type BuildFlags, projectRoot } from '../config/index.js';
import { watchI18N } from '../util/i18n.js'; import { watchI18N } from '../util/i18n.js';
import { watchInfra } from '../util/infra.js';
const files = ['.env', '.env.local']; const files = ['.env', '.env.local'];
@ -93,6 +94,7 @@ flags.channel = buildFlags.channel as any;
flags.coverage = buildFlags.coverage; flags.coverage = buildFlags.coverage;
watchI18N(); watchI18N();
await watchInfra();
spawn( spawn(
'node', 'node',
[ [

View File

@ -0,0 +1,41 @@
import { spawn } from 'node:child_process';
import { resolve } from 'node:path';
import { build } from 'vite';
import { projectRoot } from '../config/index.js';
const infraFilePath = resolve(
projectRoot,
'packages',
'infra',
'vite.config.ts'
);
const pluginInfraFilePath = resolve(
projectRoot,
'packages',
'plugin-infra',
'vite.config.ts'
);
export const buildInfra = async () => {
await build({
configFile: infraFilePath,
});
await build({
configFile: pluginInfraFilePath,
});
};
export const watchInfra = async () => {
spawn('vite', ['build', '--watch'], {
cwd: resolve(projectRoot, 'packages', 'infra'),
shell: true,
stdio: 'inherit',
});
spawn('vite', ['build', '--watch'], {
cwd: resolve(projectRoot, 'packages', 'plugin-infra'),
shell: true,
stdio: 'inherit',
});
};

View File

@ -1,6 +1,6 @@
import { ProviderComposer } from '@affine/component/provider-composer'; import { ProviderComposer } from '@affine/component/provider-composer';
import { ThemeProvider } from '@affine/component/theme-provider'; import { ThemeProvider } from '@affine/component/theme-provider';
import { rootStore } from '@toeverything/plugin-infra/manager'; import { rootStore } from '@toeverything/plugin-infra/atom';
import { Provider } from 'jotai'; import { Provider } from 'jotai';
import type { PropsWithChildren } from 'react'; import type { PropsWithChildren } from 'react';
import { useMemo } from 'react'; import { useMemo } from 'react';

View File

@ -14,10 +14,10 @@
"import": "./dist/entry.js", "import": "./dist/entry.js",
"require": "./dist/entry.cjs" "require": "./dist/entry.cjs"
}, },
"./manager": { "./atom": {
"type": "./dist/manager.d.ts", "type": "./dist/atom.d.ts",
"import": "./dist/manager.js", "import": "./dist/atom.js",
"require": "./dist/manager.cjs" "require": "./dist/atom.cjs"
}, },
"./type": { "./type": {
"type": "./dist/type.d.ts", "type": "./dist/type.d.ts",

View File

@ -13,7 +13,7 @@ export default defineConfig({
entry: { entry: {
entry: resolve(root, 'src/entry.ts'), entry: resolve(root, 'src/entry.ts'),
type: resolve(root, 'src/type.ts'), type: resolve(root, 'src/type.ts'),
manager: resolve(root, 'src/manager.ts'), atom: resolve(root, 'src/atom.ts'),
'__internal__/workspace': resolve( '__internal__/workspace': resolve(
root, root,
'src/__internal__/workspace.ts' 'src/__internal__/workspace.ts'

View File

@ -1,6 +1,6 @@
import { IconButton } from '@affine/component'; import { IconButton } from '@affine/component';
import { SendIcon } from '@blocksuite/icons'; import { SendIcon } from '@blocksuite/icons';
import { contentLayoutAtom } from '@toeverything/plugin-infra/manager'; import { contentLayoutAtom } from '@toeverything/plugin-infra/atom';
import { useAtomValue, useSetAtom } from 'jotai'; import { useAtomValue, useSetAtom } from 'jotai';
import type { ReactElement } from 'react'; import type { ReactElement } from 'react';
import { Suspense, useCallback, useState } from 'react'; import { Suspense, useCallback, useState } from 'react';

View File

@ -1,5 +1,5 @@
import { IconButton, Tooltip } from '@affine/component'; import { IconButton, Tooltip } from '@affine/component';
import { contentLayoutAtom } from '@toeverything/plugin-infra/manager'; import { contentLayoutAtom } from '@toeverything/plugin-infra/atom';
import { useSetAtom } from 'jotai'; import { useSetAtom } from 'jotai';
import type { ReactElement } from 'react'; import type { ReactElement } from 'react';
import { useCallback } from 'react'; import { useCallback } from 'react';

View File

@ -1,8 +1,8 @@
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import { import {
currentWorkspaceIdAtom, currentWorkspaceIdAtom,
rootStore, rootStore,
} from '@toeverything/plugin-infra/manager'; } from '@toeverything/plugin-infra/atom';
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import { createElement } from 'react'; import { createElement } from 'react';
import { createRoot } from 'react-dom/client'; import { createRoot } from 'react-dom/client';