Skip to content

Commit

Permalink
feat: migrate router v5 plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Dec 18, 2024
1 parent 5cee46c commit 5c95f72
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 62 deletions.
120 changes: 59 additions & 61 deletions packages/runtime/plugin-router-v5/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
import type { AppTools, CliPluginFuture } from '@modern-js/app-tools';
import {
createRuntimeExportsUtils,
getEntryOptions,
Expand All @@ -7,74 +7,72 @@ import {
import './types';
import type { ServerRoute } from '@modern-js/types';

export const routerPlugin = (): CliPlugin<AppTools> => ({
export const routerPlugin = (): CliPluginFuture<AppTools> => ({
name: '@modern-js/plugin-router-v5',
required: ['@modern-js/runtime'],
setup: api => {
let routerExportsUtils: any;

return {
_internalRuntimePlugins({ entrypoint, plugins }) {
const userConfig = api.useResolvedConfigContext();
const { serverRoutes, metaName, packageName } = api.useAppContext();
if (isV5(userConfig)) {
const routerConfig = getEntryOptions(
entrypoint.entryName,
entrypoint.isMainEntry,
userConfig.runtime,
userConfig.runtimeByEntries,
packageName,
)?.router;
const serverBase = serverRoutes
.filter(
(route: ServerRoute) => route.entryName === entrypoint.entryName,
)
.map(route => route.urlPath)
.sort((a, b) => (a.length - b.length > 0 ? -1 : 1));
plugins.push({
name: 'router',
path: `@${metaName}/plugin-router-v5/runtime`,
config:
typeof routerConfig === 'boolean'
? { serverBase }
: { ...routerConfig, serverBase },
});
}
return { entrypoint, plugins };
},
config() {
const { internalDirectory, metaName } = api.useAppContext();
// .modern-js/.runtime-exports/router (legacy)
routerExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'router',
);
api._internalRuntimePlugins(({ entrypoint, plugins }) => {
const userConfig = api.getNormalizedConfig();
const { serverRoutes, metaName, packageName } = api.getAppContext();
if (isV5(userConfig)) {
const routerConfig = getEntryOptions(
entrypoint.entryName,
entrypoint.isMainEntry,
userConfig.runtime,
userConfig.runtimeByEntries,
packageName,
)?.router;
const serverBase = serverRoutes
.filter(
(route: ServerRoute) => route.entryName === entrypoint.entryName,
)
.map(route => route.urlPath)
.sort((a, b) => (a.length - b.length > 0 ? -1 : 1));
plugins.push({
name: 'router',
path: `@${metaName}/plugin-router-v5/runtime`,
config:
typeof routerConfig === 'boolean'
? { serverBase }
: { ...routerConfig, serverBase },
});
}
return { entrypoint, plugins };
});
api.config(() => {
const { internalDirectory, metaName } = api.getAppContext();
// .modern-js/.runtime-exports/router (legacy)
routerExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'router',
);

return {
source: {
alias: {
[`@${metaName}/runtime/router-v5`]: routerExportsUtils.getPath(),
},
return {
source: {
alias: {
[`@${metaName}/runtime/router-v5`]: routerExportsUtils.getPath(),
},
};
},
addRuntimeExports() {
const userConfig = api.useResolvedConfigContext();
const { internalDirectory, metaName } = api.useAppContext();
const pluginsExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'plugins',
},
};
});
api.addRuntimeExports(() => {
const userConfig = api.getNormalizedConfig();
const { internalDirectory, metaName } = api.getAppContext();
const pluginsExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'plugins',
);
if (isV5(userConfig)) {
pluginsExportsUtils.addExport(
`export { default as router } from '@${metaName}/plugin-router-v5/runtime'`,
);
routerExportsUtils?.addExport(
`export * from '@${metaName}/plugin-router-v5/runtime'`,
);
if (isV5(userConfig)) {
pluginsExportsUtils.addExport(
`export { default as router } from '@${metaName}/plugin-router-v5/runtime'`,
);
routerExportsUtils?.addExport(
`export * from '@${metaName}/plugin-router-v5/runtime'`,
);
}
},
};
}
});
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-router-v5/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('plugin-router-legacy', () => {
describe('cli-router-legacy', () => {
const setup = async () => {
const pluginManager = createPluginManager();
pluginManager.addPlugins([runtimePlugin(), cliPlugin() as Plugin]);
pluginManager.addPlugins([runtimePlugin(), cliPlugin()]);
const plugins = pluginManager.getPlugins();
const context = await createContext<AppTools>({
appContext: {
Expand Down

0 comments on commit 5c95f72

Please sign in to comment.