Skip to content

Commit 811f30b

Browse files
authored
feat: migrate app tools to use new cli plugin (#6603)
1 parent bc1670a commit 811f30b

31 files changed

+464
-563
lines changed

.changeset/fluffy-mayflies-battle.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
---
4+
5+
feat: migrate app tools to use new cli plugin
6+
7+
feat: app tools 使用新的 cli 插件

packages/solutions/app-tools/bin/modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (!process.env.MODERN_JS_VERSION) {
1010
process.env.MODERN_JS_VERSION = version;
1111
}
1212

13-
require('../dist/cjs/new/run.js').run({
13+
require('../dist/cjs/run/index.js').run({
1414
internalPlugins: {
1515
cli: INTERNAL_APP_TOOLS_PLUGINS,
1616
autoLoad: INTERNAL_APP_TOOLS_RUNTIME_PLUGINS,

packages/solutions/app-tools/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
"default": "./dist/cjs/index.js"
3636
},
3737
"./cli/run": {
38-
"types": "./dist/types/new/run.d.ts",
39-
"jsnext:source": "./src/new/run.ts",
40-
"default": "./dist/cjs/new/run.js"
38+
"types": "./dist/types/run/index.d.ts",
39+
"jsnext:source": "./src/run/index.ts",
40+
"default": "./dist/cjs/run/index.js"
4141
},
4242
"./types": {
4343
"types": "./lib/types.d.ts",
@@ -64,7 +64,7 @@
6464
"./dist/types/index.d.ts"
6565
],
6666
"cli/run": [
67-
"./dist/types/new/run.d.ts"
67+
"./dist/types/run/index.d.ts"
6868
],
6969
"types": [
7070
"./lib/types.d.ts"

packages/solutions/app-tools/src/commands/build.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type PluginAPI, ResolvedConfigContext } from '@modern-js/core';
1+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
22
import { logger } from '@modern-js/utils';
33
import type { AppTools } from '../types';
44
import { buildServerConfig } from '../utils/config';
@@ -8,17 +8,17 @@ import { generateRoutes } from '../utils/routes';
88
import type { BuildOptions } from '../utils/types';
99

1010
export const build = async (
11-
api: PluginAPI<AppTools<'shared'>>,
11+
api: CLIPluginAPI<AppTools<'shared'>>,
1212
options?: BuildOptions,
1313
) => {
1414
if (options?.analyze) {
1515
// Builder will read this env var to enable bundle analyzer
1616
process.env.BUNDLE_ANALYZE = 'true';
1717
}
1818

19-
let resolvedConfig = api.useResolvedConfigContext();
20-
const appContext = api.useAppContext();
21-
const hookRunners = api.useHookRunners();
19+
const resolvedConfig = api.getNormalizedConfig();
20+
const appContext = api.getAppContext();
21+
const hooks = api.getHooks();
2222

2323
// we need load server plugin to appContext for ssg & deploy commands.
2424
await loadServerPlugins(api, appContext.appDirectory, appContext.metaName);
@@ -42,9 +42,12 @@ export const build = async (
4242

4343
if (apiOnly) {
4444
const { appDirectory, distDirectory, serverConfigFile } = appContext;
45-
await hookRunners.beforeBuild({
45+
await hooks.onBeforeBuild.call({
46+
environments: {},
4647
// "null" bundlerConfigs
4748
bundlerConfigs: undefined,
49+
isFirstCompile: false,
50+
isWatch: false,
4851
});
4952

5053
await buildServerConfig({
@@ -55,16 +58,20 @@ export const build = async (
5558

5659
await generateRoutes(appContext);
5760

58-
await hookRunners.afterBuild({
61+
await hooks.onAfterBuild.call({
62+
environments: {},
5963
// "null" stats
6064
stats: undefined,
65+
isFirstCompile: false,
66+
isWatch: false,
6167
});
6268

6369
return;
6470
}
6571

66-
resolvedConfig = { ...resolvedConfig, cliOptions: options };
67-
ResolvedConfigContext.set(resolvedConfig);
72+
api.modifyResolvedConfig(config => {
73+
return { ...config, cliOptions: options };
74+
});
6875

6976
const { distDirectory, appDirectory, serverConfigFile } = appContext;
7077

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import type { PluginAPI } from '@modern-js/core';
1+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
22
import type { AppTools } from '../types';
33
import { getServerPlugins } from '../utils/loadPlugins';
44

55
export const deploy = async (
6-
api: PluginAPI<AppTools<'shared'>>,
6+
api: CLIPluginAPI<AppTools<'shared'>>,
77
options: any,
88
) => {
9-
const hookRunners = api.useHookRunners();
9+
const hooks = api.getHooks();
1010

11-
const { metaName } = api.useAppContext();
11+
const { metaName } = api.getAppContext();
1212

1313
// deploy command need get all plugins
1414
await getServerPlugins(api, metaName);
1515

16-
await hookRunners.beforeDeploy(options);
17-
await hookRunners.deploy(options);
18-
await hookRunners.afterDeploy(options);
16+
await hooks.onBeforeDeploy.call(options);
17+
await hooks.deploy.call();
18+
await hooks.onAfterDeploy.call(options);
1919
};

packages/solutions/app-tools/src/commands/dev.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import { type PluginAPI, ResolvedConfigContext } from '@modern-js/core';
2+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
33
import { applyPlugins } from '@modern-js/prod-server';
44
import { type ApplyPlugins, createDevServer } from '@modern-js/server';
55
import {
@@ -8,11 +8,11 @@ import {
88
getMeta,
99
logger,
1010
} from '@modern-js/utils';
11-
import type { AppTools } from '../types';
11+
import type { AppNormalizedConfig, AppTools } from '../types';
1212
import { buildServerConfig } from '../utils/config';
1313
import { setServer } from '../utils/createServer';
1414
import { loadServerPlugins } from '../utils/loadPlugins';
15-
import { printInstructionsCompat } from '../utils/printInstructions';
15+
import { printInstructions } from '../utils/printInstructions';
1616
import { registerCompiler } from '../utils/register';
1717
import { generateRoutes } from '../utils/routes';
1818
import type { DevOptions } from '../utils/types';
@@ -22,17 +22,17 @@ export interface ExtraServerOptions {
2222
}
2323

2424
export const dev = async (
25-
api: PluginAPI<AppTools<'shared'>>,
25+
api: CLIPluginAPI<AppTools<'shared'>>,
2626
options: DevOptions,
2727
devServerOptions?: ExtraServerOptions,
2828
) => {
2929
if (options.analyze) {
3030
// Builder will read this env var to enable bundle analyzer
3131
process.env.BUNDLE_ANALYZE = 'true';
3232
}
33-
let normalizedConfig = api.useResolvedConfigContext();
34-
const appContext = api.useAppContext();
35-
const hookRunners = api.useHookRunners();
33+
const normalizedConfig = api.getNormalizedConfig();
34+
const appContext = api.getAppContext();
35+
const hooks = api.getHooks();
3636

3737
if (appContext.moduleType && appContext.moduleType === 'module') {
3838
const { registerEsm } = await import('../esm/register-esm.mjs');
@@ -49,8 +49,9 @@ export const dev = async (
4949
normalizedConfig?.source?.alias,
5050
);
5151

52-
normalizedConfig = { ...normalizedConfig, cliOptions: options };
53-
ResolvedConfigContext.set(normalizedConfig);
52+
api.modifyResolvedConfig(config => {
53+
return { ...config, cliOptions: options };
54+
});
5455

5556
const {
5657
appDirectory,
@@ -76,7 +77,7 @@ export const dev = async (
7677
`${meta}.server`,
7778
);
7879

79-
await hookRunners.beforeDev();
80+
await hooks.onBeforeDev.call();
8081

8182
if (!appContext.builder && !apiOnly) {
8283
throw new Error(
@@ -129,7 +130,11 @@ export const dev = async (
129130
host,
130131
},
131132
() => {
132-
printInstructionsCompat(hookRunners, appContext, normalizedConfig);
133+
printInstructions(
134+
hooks,
135+
appContext,
136+
normalizedConfig as AppNormalizedConfig<'shared'>,
137+
);
133138
},
134139
);
135140
} else {

packages/solutions/app-tools/src/commands/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PluginAPI } from '@modern-js/core';
1+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
22
import { castArray } from '@modern-js/uni-builder';
33
import { type Command, newAction, upgradeAction } from '@modern-js/utils';
44
import { i18n, localeKeys } from '../locale';
@@ -12,10 +12,10 @@ import type {
1212

1313
export const devCommand = async (
1414
program: Command,
15-
api: PluginAPI<AppTools<'shared'>>,
15+
api: CLIPluginAPI<AppTools<'shared'>>,
1616
) => {
17-
const runner = api.useHookRunners();
18-
const devToolMetas = await runner.registerDev();
17+
const hooks = api.getHooks();
18+
const devToolMetas = await hooks.registerDev.call();
1919

2020
const devProgram = program
2121
.command('dev')
@@ -39,7 +39,7 @@ export const devCommand = async (
3939

4040
for (const subCmd of meta.subCommands) {
4141
devProgram.command(subCmd).action(async (options: DevOptions = {}) => {
42-
const { appDirectory } = api.useAppContext();
42+
const { appDirectory } = api.getAppContext();
4343
const { isTypescript } = await import('@modern-js/utils');
4444

4545
await meta.action(options, {
@@ -52,10 +52,10 @@ export const devCommand = async (
5252

5353
export const buildCommand = async (
5454
program: Command,
55-
api: PluginAPI<AppTools<'shared'>>,
55+
api: CLIPluginAPI<AppTools<'shared'>>,
5656
) => {
57-
const runner = api.useHookRunners();
58-
const platformBuilders = await runner.registerBuildPlatform();
57+
const hooks = api.getHooks();
58+
const platformBuilders = await hooks.registerBuildPlatform.call();
5959

6060
const buildProgram = program
6161
.command('build')
@@ -72,7 +72,7 @@ export const buildCommand = async (
7272
const platforms = castArray(platformBuilder.platform);
7373
for (const platform of platforms) {
7474
buildProgram.command(platform).action(async () => {
75-
const { appDirectory } = api.useAppContext();
75+
const { appDirectory } = api.getAppContext();
7676
const { isTypescript } = await import('@modern-js/utils');
7777

7878
await platformBuilder.build(platform, {
@@ -85,7 +85,7 @@ export const buildCommand = async (
8585

8686
export const serverCommand = (
8787
program: Command,
88-
api: PluginAPI<AppTools<'shared'>>,
88+
api: CLIPluginAPI<AppTools<'shared'>>,
8989
) => {
9090
program
9191
.command('serve')
@@ -101,7 +101,7 @@ export const serverCommand = (
101101

102102
export const deployCommand = (
103103
program: Command,
104-
api: PluginAPI<AppTools<'shared'>>,
104+
api: CLIPluginAPI<AppTools<'shared'>>,
105105
) => {
106106
program
107107
.command('deploy')
@@ -152,7 +152,7 @@ export const newCommand = (program: Command, locale: string) => {
152152

153153
export const inspectCommand = (
154154
program: Command,
155-
api: PluginAPI<AppTools<'shared'>>,
155+
api: CLIPluginAPI<AppTools<'shared'>>,
156156
) => {
157157
program
158158
.command('inspect')

packages/solutions/app-tools/src/commands/inspect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { PluginAPI } from '@modern-js/core';
1+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
22
import type { RsbuildMode } from '@rsbuild/core';
33
import type { AppTools } from '../types';
44
import type { InspectOptions } from '../utils/types';
55

66
export const inspect = async (
7-
api: PluginAPI<AppTools<'shared'>>,
7+
api: CLIPluginAPI<AppTools<'shared'>>,
88
options: InspectOptions,
99
) => {
10-
const appContext = api.useAppContext();
10+
const appContext = api.getAppContext();
1111
if (!appContext.builder) {
1212
throw new Error(
1313
'Expect the Builder to have been initialized, But the appContext.builder received `undefined`',

packages/solutions/app-tools/src/commands/serve.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import type { PluginAPI } from '@modern-js/core';
2+
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
33
import { createProdServer } from '@modern-js/prod-server';
44
import {
55
SERVER_DIR,
@@ -8,14 +8,14 @@ import {
88
isApiOnly,
99
logger,
1010
} from '@modern-js/utils';
11-
import type { AppTools } from '../types';
11+
import type { AppNormalizedConfig, AppTools } from '../types';
1212
import { loadServerPlugins } from '../utils/loadPlugins';
13-
import { printInstructionsCompat } from '../utils/printInstructions';
13+
import { printInstructions } from '../utils/printInstructions';
1414

15-
export const start = async (api: PluginAPI<AppTools<'shared'>>) => {
16-
const appContext = api.useAppContext();
17-
const userConfig = api.useResolvedConfigContext();
18-
const hookRunners = api.useHookRunners();
15+
export const start = async (api: CLIPluginAPI<AppTools<'shared'>>) => {
16+
const appContext = api.getAppContext();
17+
const userConfig = api.getNormalizedConfig();
18+
const hooks = api.getHooks();
1919

2020
const {
2121
distDirectory,
@@ -87,6 +87,10 @@ export const start = async (api: PluginAPI<AppTools<'shared'>>) => {
8787
});
8888

8989
app.listen(port, async () => {
90-
await printInstructionsCompat(hookRunners, appContext, userConfig);
90+
await printInstructions(
91+
hooks,
92+
appContext,
93+
userConfig as AppNormalizedConfig<'shared'>,
94+
);
9195
});
9296
};

packages/solutions/app-tools/src/new/compat/hooks.ts renamed to packages/solutions/app-tools/src/compat/hooks.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import type {
88
ServerRoute,
99
} from '@modern-js/types';
1010
import type { Command } from '@modern-js/utils';
11-
import { getModifyHtmlPartials } from '../../plugins/analyze/getHtmlTemplate';
12-
import type { AppTools, AppToolsNormalizedConfig } from '../../types';
13-
import type { RuntimePlugin } from '../../types/hooks';
11+
import { getModifyHtmlPartials } from '../plugins/analyze/getHtmlTemplate';
12+
import type { AppTools, AppToolsNormalizedConfig } from '../types';
13+
import type { RuntimePlugin } from '../types/hooks';
1414
import {
1515
transformHookParams,
1616
transformHookResult,
@@ -21,7 +21,7 @@ import {
2121
* old plugin useHookRunners function result
2222
*/
2323
export function getHookRunners(
24-
context: InternalContext<AppTools>,
24+
context: InternalContext<AppTools<'shared'>>,
2525
): Record<string, any> {
2626
const { hooks } = context;
2727
return {
@@ -162,24 +162,14 @@ export function getHookRunners(
162162
/**
163163
* @deprecated
164164
*/
165-
registerDev: async (params: {
166-
name: string;
167-
entry: string;
168-
type: string;
169-
config: any;
170-
}) => {
171-
return hooks.registerDev.call(params);
165+
registerDev: async () => {
166+
return hooks.registerDev.call();
172167
},
173168
/**
174169
* @deprecated
175170
*/
176-
registerBuildPlatform: async (params: {
177-
name: string;
178-
entry: string;
179-
type: string;
180-
config: any;
181-
}) => {
182-
return hooks.registerBuildPlatform.call(params);
171+
registerBuildPlatform: async () => {
172+
return hooks.registerBuildPlatform.call();
183173
},
184174
/**
185175
* @deprecated

0 commit comments

Comments
 (0)