Skip to content

Commit

Permalink
feat: migrate router plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Dec 12, 2024
1 parent 33864cb commit 0e1b9d9
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 122 deletions.
10 changes: 5 additions & 5 deletions packages/runtime/plugin-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const runtimePlugin = (params?: {
// the order of runtime plugins is affected by runtime hooks, mainly `init` and `hoc` hooks
usePlugins: params?.plugins || [
ssrPlugin(),
routerPlugin() as any,
statePlugin(),
routerPlugin(),
statePlugin() as any,
documentPlugin(),
],
setup: api => {
Expand Down Expand Up @@ -67,9 +67,9 @@ export const runtimePlugin = (params?: {
});

/* Note that the execution time of the config hook is before prepare.
/* This means that the entry information cannot be obtained in the config hook.
/* Therefore, aliases cannot be set directly in the config.
*/
/* This means that the entry information cannot be obtained in the config hook.
/* Therefore, aliases cannot be set directly in the config.
*/
api.onPrepare(() => {
const { builder, entrypoints, internalDirectory, metaName } =
api.getAppContext();
Expand Down
24 changes: 14 additions & 10 deletions packages/runtime/plugin-runtime/src/router/cli/code/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import path from 'path';
import type { AppNormalizedConfig, AppTools } from '@modern-js/app-tools';
import type { IAppContext, PluginAPI } from '@modern-js/core';
import type {
AppNormalizedConfig,
AppTools,
AppToolsContext,
} from '@modern-js/app-tools';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import type {
Entrypoint,
NestedRouteForCli,
Expand Down Expand Up @@ -31,10 +35,10 @@ import * as templates from './templates';
import { getServerCombinedModueFile, getServerLoadersFile } from './utils';

export const generateCode = async (
appContext: IAppContext,
appContext: AppToolsContext<'shared'>,
config: AppNormalizedConfig<'shared'>,
entrypoints: Entrypoint[],
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
const {
internalDirectory,
Expand All @@ -44,7 +48,7 @@ export const generateCode = async (
packageName,
} = appContext;

const hookRunners = api.useHookRunners();
const hooks = api.getHooks();

const isV5 = isRouterV5(config);
const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
Expand All @@ -63,7 +67,7 @@ export const generateCode = async (
pageRoutesEntry,
nestedRoutesEntry,
} = entrypoint;
const { metaName } = api.useAppContext();
const { metaName } = api.getAppContext();
if (isAutoMount) {
// generate routes file for file system routes entrypoint.
if (pageRoutesEntry || nestedRoutesEntry) {
Expand Down Expand Up @@ -101,7 +105,7 @@ export const generateCode = async (
}
}

const config = api.useResolvedConfigContext();
const config = api.getNormalizedConfig();
const ssrByRouteIds = config.server.ssrByRouteIds || [];
const clonedRoutes = cloneDeep(initialRoutes);

Expand All @@ -113,7 +117,7 @@ export const generateCode = async (
)
: initialRoutes;

const { routes } = await hookRunners.modifyFileSystemRoutes({
const { routes } = await hooks.modifyFileSystemRoutes.call({
entrypoint,
routes: markedRoutes,
});
Expand Down Expand Up @@ -143,7 +147,7 @@ export const generateCode = async (
}
}

const { code } = await hookRunners.beforeGenerateRoutes({
const { code } = await hooks.onBeforeGenerateRoutes.call({
entrypoint,
code: await templates.fileSystemRoutes({
metaName,
Expand Down Expand Up @@ -197,7 +201,7 @@ export const generateCode = async (
const serverLoaderCombined = templates.ssrLoaderCombinedModule(
entrypoints,
entrypoint,
config,
config as AppNormalizedConfig<'shared'>,
appContext,
);
if (serverLoaderCombined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'path';
import type { AppNormalizedConfig, IAppContext } from '@modern-js/app-tools';
import type {
AppNormalizedConfig,
AppToolsContext,
} from '@modern-js/app-tools';
import type {
Entrypoint,
NestedRouteForCli,
Expand Down Expand Up @@ -452,7 +455,7 @@ export function ssrLoaderCombinedModule(
entrypoints: Entrypoint[],
entrypoint: Entrypoint,
config: AppNormalizedConfig<'shared'>,
appContext: IAppContext,
appContext: AppToolsContext<'shared'>,
) {
const { entryName, isMainEntry } = entrypoint;
const { packageName, internalDirectory } = appContext;
Expand Down
36 changes: 23 additions & 13 deletions packages/runtime/plugin-runtime/src/router/cli/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import type { AppTools } from '@modern-js/app-tools';
import type { PluginAPI } from '@modern-js/core';
import type { AppNormalizedConfig, AppTools } from '@modern-js/app-tools';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import type { Entrypoint } from '@modern-js/types';
import { cloneDeep } from '@modern-js/utils/lodash';
import * as templates from './code/templates';
Expand All @@ -10,23 +10,28 @@ import { modifyEntrypoints } from './entry';
let originEntrypoints: any[] = [];

export async function handleModifyEntrypoints(
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
entrypoints: Entrypoint[],
) {
const config = api.useResolvedConfigContext();
const config = api.getNormalizedConfig();
return modifyEntrypoints(entrypoints, config);
}

export async function handleGeneratorEntryCode(
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
entrypoints: Entrypoint[],
) {
const appContext = api.useAppContext();
const { internalDirectory } = api.useAppContext();
const resolvedConfig = api.useResolvedConfigContext();
const appContext = api.getAppContext();
const { internalDirectory } = appContext;
const resolvedConfig = api.getNormalizedConfig();
const { generatorRegisterCode, generateCode } = await import('./code');
originEntrypoints = cloneDeep(entrypoints);
await generateCode(appContext, resolvedConfig, entrypoints, api);
await generateCode(
appContext,
resolvedConfig as AppNormalizedConfig<'shared'>,
entrypoints,
api,
);
await Promise.all(
entrypoints.map(async entrypoint => {
if (entrypoint.nestedRoutesEntry || entrypoint.pageRoutesEntry) {
Expand All @@ -48,10 +53,10 @@ export async function handleGeneratorEntryCode(
}

export async function handleFileChange(
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
e: any,
) {
const appContext = api.useAppContext();
const appContext = api.getAppContext();
const { appDirectory, entrypoints } = appContext;
const { filename, eventType } = e;
const nestedRouteEntries = entrypoints
Expand All @@ -70,9 +75,14 @@ export async function handleFileChange(
isPageFile(absoluteFilePath) && isPageComponentFile(absoluteFilePath);

if (isRouteComponent && (eventType === 'add' || eventType === 'unlink')) {
const resolvedConfig = api.useResolvedConfigContext();
const resolvedConfig = api.getNormalizedConfig();
const { generateCode } = await import('./code');
const entrypoints = cloneDeep(originEntrypoints);
await generateCode(appContext, resolvedConfig, entrypoints, api);
await generateCode(
appContext,
resolvedConfig as AppNormalizedConfig<'shared'>,
entrypoints,
api,
);
}
}
Loading

0 comments on commit 0e1b9d9

Please sign in to comment.