Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate runtime cli plugin to new cli plugin #6629

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/witty-hotels-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/runtime': patch
---

feat: migrate runtime cli plugin to new cli plugin

feat: runtime CLI 插件迁移到新的 CLI 插件
2 changes: 1 addition & 1 deletion packages/cli/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const initAppContext = ({
appDirectory: string;
plugins: CliPlugin[];
configFile: string | false;
runtimeConfigFile: string | false;
runtimeConfigFile: string;
options?: {
metaName?: string;
srcDir?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/plugin-router-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"dependencies": {
"@modern-js/plugin": "workspace:*",
"@modern-js/plugin-v2": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@modern-js/types": "workspace:*",
"@modern-js/utils": "workspace:*",
Expand Down
53 changes: 43 additions & 10 deletions packages/runtime/plugin-router-v5/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AppContext, manager } from '@modern-js/core';
import RuntimePlugin from '@modern-js/runtime/cli';
import type { AppTools } from '@modern-js/app-tools';
import { createPluginManager } from '@modern-js/plugin-v2';
import { createContext, initPluginAPI } from '@modern-js/plugin-v2/cli';
import runtimePlugin from '@modern-js/runtime/cli';
import plugin, { useHistory, useParams } from '../src';
import cliPlugin from '../src/cli';

Expand All @@ -12,20 +14,51 @@ describe('plugin-router-legacy', () => {
});

describe('cli-router-legacy', () => {
const main = manager.clone().usePlugin(RuntimePlugin, cliPlugin as any);
let runner: any;

beforeAll(async () => {
runner = await main.init();
});
const setup = async () => {
const pluginManager = createPluginManager();
pluginManager.addPlugins([runtimePlugin(), cliPlugin() as Plugin]);
const plugins = pluginManager.getPlugins();
const context = await createContext<AppTools>({
appContext: {
plugins,
} as any,
config: {},
normalizedConfig: { plugins: [] } as any,
});
const pluginAPI = {
...initPluginAPI<AppTools>({
context,
pluginManager,
}),
checkEntryPoint: ({ path, entry }: any) => {
return { path, entry };
},
modifyEntrypoints: ({ entrypoints }: any) => {
return { entrypoints };
},
generateEntryCode: async ({ entrypoints }: any) => {},
_internalRuntimePlugins: ({ entrypoint, plugins }: any) => {
return { entrypoint, plugins };
},
addRuntimeExports: () => {},
modifyFileSystemRoutes: () => {},
onBeforeGenerateRoutes: () => {},
};
context.pluginAPI = pluginAPI;
for (const plugin of plugins) {
await plugin.setup(pluginAPI);
}
return pluginAPI;
};

test('should plugin-router-legacy defined', async () => {
expect(cliPlugin).toBeDefined();
});

it('plugin-router-legacy cli config is defined', async () => {
AppContext.set({ metaName: 'modern-js' } as any);
const config = await runner.config();
const api = await setup();
api.updateAppContext({ metaName: 'modern-js' } as any);
const config = await api.getHooks().config.call();
expect(
config.find(
(item: any) => item.source.alias['@modern-js/runtime/plugins'],
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"@modern-js-reduck/react": "^1.1.10",
"@modern-js-reduck/store": "^1.1.10",
"@modern-js/plugin": "workspace:*",
"@modern-js/plugin-v2": "workspace:*",
"@modern-js/plugin-data-loader": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@modern-js/types": "workspace:*",
Expand All @@ -220,7 +221,6 @@
},
"devDependencies": {
"@modern-js/app-tools": "workspace:*",
"@modern-js/core": "workspace:*",
"@remix-run/web-fetch": "^4.1.3",
"@rsbuild/core": "1.1.9",
"@scripts/build": "workspace:*",
Expand Down
30 changes: 13 additions & 17 deletions packages/runtime/plugin-runtime/src/cli/code.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import path from 'path';
import type {
AppNormalizedConfig,
AppTools,
IAppContext,
NormalizedConfig,
RuntimePlugin,
AppToolsContext,
AppToolsFeatureHooks,
AppToolsNormalizedConfig,
} from '@modern-js/app-tools';
import type { MaybeAsync } from '@modern-js/plugin';
import type { Entrypoint } from '@modern-js/types';
import { fs, MAIN_ENTRY_NAME } from '@modern-js/utils';
import { fs } from '@modern-js/utils';
import {
ENTRY_BOOTSTRAP_FILE_NAME,
ENTRY_POINT_FILE_NAME,
Expand All @@ -24,7 +22,7 @@ import * as serverTemplate from './template.server';

function getSSRMode(
entry: string,
config: AppNormalizedConfig,
config: AppToolsNormalizedConfig,
): 'string' | 'stream' | false {
const { ssr, ssrByEntries } = config.server;

Expand All @@ -49,12 +47,9 @@ function getSSRMode(

export const generateCode = async (
entrypoints: Entrypoint[],
appContext: IAppContext,
config: NormalizedConfig<AppTools>,
onCollectRuntimePlugins: (params: {
entrypoint: Entrypoint;
plugins: RuntimePlugin[];
}) => MaybeAsync<{ entrypoint: Entrypoint; plugins: RuntimePlugin[] }>,
appContext: AppToolsContext<'shared'>,
config: AppToolsNormalizedConfig,
hooks: AppToolsFeatureHooks<'shared'>,
) => {
const { mountId } = config.html;
const { enableAsyncEntry } = config.source;
Expand All @@ -75,10 +70,11 @@ export const generateCode = async (
customBootstrap,
customServerEntry,
} = entrypoint;
const { plugins: runtimePlugins } = await onCollectRuntimePlugins({
entrypoint,
plugins: [],
});
const { plugins: runtimePlugins } =
await hooks._internalRuntimePlugins.call({
entrypoint,
plugins: [],
});
if (isAutoMount) {
// index.jsx
const indexCode = template.index({
Expand Down
Loading
Loading