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 app tools to use new cli plugin #6603

Merged
merged 9 commits into from
Dec 11, 2024
Merged
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/fluffy-mayflies-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': patch
---

feat: migrate app tools to use new cli plugin

feat: app tools 使用新的 cli 插件
2 changes: 1 addition & 1 deletion packages/solutions/app-tools/bin/modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (!process.env.MODERN_JS_VERSION) {
process.env.MODERN_JS_VERSION = version;
}

require('../dist/cjs/new/run.js').run({
require('../dist/cjs/run/index.js').run({
internalPlugins: {
cli: INTERNAL_APP_TOOLS_PLUGINS,
autoLoad: INTERNAL_APP_TOOLS_RUNTIME_PLUGINS,
Expand Down
8 changes: 4 additions & 4 deletions packages/solutions/app-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"default": "./dist/cjs/index.js"
},
"./cli/run": {
"types": "./dist/types/new/run.d.ts",
"jsnext:source": "./src/new/run.ts",
"default": "./dist/cjs/new/run.js"
"types": "./dist/types/run/index.d.ts",
"jsnext:source": "./src/run/index.ts",
"default": "./dist/cjs/run/index.js"
},
"./types": {
"types": "./lib/types.d.ts",
Expand All @@ -64,7 +64,7 @@
"./dist/types/index.d.ts"
],
"cli/run": [
"./dist/types/new/run.d.ts"
"./dist/types/run/index.d.ts"
],
"types": [
"./lib/types.d.ts"
Expand Down
25 changes: 16 additions & 9 deletions packages/solutions/app-tools/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PluginAPI, ResolvedConfigContext } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import { logger } from '@modern-js/utils';
import type { AppTools } from '../types';
import { buildServerConfig } from '../utils/config';
Expand All @@ -8,17 +8,17 @@ import { generateRoutes } from '../utils/routes';
import type { BuildOptions } from '../utils/types';

export const build = async (
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
options?: BuildOptions,
) => {
if (options?.analyze) {
// Builder will read this env var to enable bundle analyzer
process.env.BUNDLE_ANALYZE = 'true';
}

let resolvedConfig = api.useResolvedConfigContext();
const appContext = api.useAppContext();
const hookRunners = api.useHookRunners();
const resolvedConfig = api.getNormalizedConfig();
const appContext = api.getAppContext();
const hooks = api.getHooks();

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

if (apiOnly) {
const { appDirectory, distDirectory, serverConfigFile } = appContext;
await hookRunners.beforeBuild({
await hooks.onBeforeBuild.call({
environments: {},
// "null" bundlerConfigs
bundlerConfigs: undefined,
isFirstCompile: false,
isWatch: false,
});

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

await generateRoutes(appContext);

await hookRunners.afterBuild({
await hooks.onAfterBuild.call({
environments: {},
// "null" stats
stats: undefined,
isFirstCompile: false,
isWatch: false,
});

return;
}

resolvedConfig = { ...resolvedConfig, cliOptions: options };
ResolvedConfigContext.set(resolvedConfig);
api.modifyResolvedConfig(config => {
return { ...config, cliOptions: options };
});

const { distDirectory, appDirectory, serverConfigFile } = appContext;

Expand Down
14 changes: 7 additions & 7 deletions packages/solutions/app-tools/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { PluginAPI } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import type { AppTools } from '../types';
import { getServerPlugins } from '../utils/loadPlugins';

export const deploy = async (
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
options: any,
) => {
const hookRunners = api.useHookRunners();
const hooks = api.getHooks();

const { metaName } = api.useAppContext();
const { metaName } = api.getAppContext();

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

await hookRunners.beforeDeploy(options);
await hookRunners.deploy(options);
await hookRunners.afterDeploy(options);
await hooks.onBeforeDeploy.call(options);
await hooks.deploy.call();
await hooks.onAfterDeploy.call(options);
};
27 changes: 16 additions & 11 deletions packages/solutions/app-tools/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path';
import { type PluginAPI, ResolvedConfigContext } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import { applyPlugins } from '@modern-js/prod-server';
import { type ApplyPlugins, createDevServer } from '@modern-js/server';
import {
Expand All @@ -8,11 +8,11 @@ import {
getMeta,
logger,
} from '@modern-js/utils';
import type { AppTools } from '../types';
import type { AppNormalizedConfig, AppTools } from '../types';
import { buildServerConfig } from '../utils/config';
import { setServer } from '../utils/createServer';
import { loadServerPlugins } from '../utils/loadPlugins';
import { printInstructionsCompat } from '../utils/printInstructions';
import { printInstructions } from '../utils/printInstructions';
import { registerCompiler } from '../utils/register';
import { generateRoutes } from '../utils/routes';
import type { DevOptions } from '../utils/types';
Expand All @@ -22,17 +22,17 @@ export interface ExtraServerOptions {
}

export const dev = async (
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
options: DevOptions,
devServerOptions?: ExtraServerOptions,
) => {
if (options.analyze) {
// Builder will read this env var to enable bundle analyzer
process.env.BUNDLE_ANALYZE = 'true';
}
let normalizedConfig = api.useResolvedConfigContext();
const appContext = api.useAppContext();
const hookRunners = api.useHookRunners();
const normalizedConfig = api.getNormalizedConfig();
const appContext = api.getAppContext();
const hooks = api.getHooks();

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

normalizedConfig = { ...normalizedConfig, cliOptions: options };
ResolvedConfigContext.set(normalizedConfig);
api.modifyResolvedConfig(config => {
return { ...config, cliOptions: options };
});

const {
appDirectory,
Expand All @@ -76,7 +77,7 @@ export const dev = async (
`${meta}.server`,
);

await hookRunners.beforeDev();
await hooks.onBeforeDev.call();

if (!appContext.builder && !apiOnly) {
throw new Error(
Expand Down Expand Up @@ -129,7 +130,11 @@ export const dev = async (
host,
},
() => {
printInstructionsCompat(hookRunners, appContext, normalizedConfig);
printInstructions(
hooks,
appContext,
normalizedConfig as AppNormalizedConfig<'shared'>,
);
},
);
} else {
Expand Down
24 changes: 12 additions & 12 deletions packages/solutions/app-tools/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PluginAPI } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import { castArray } from '@modern-js/uni-builder';
import { type Command, newAction, upgradeAction } from '@modern-js/utils';
import { i18n, localeKeys } from '../locale';
Expand All @@ -12,10 +12,10 @@ import type {

export const devCommand = async (
program: Command,
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
const runner = api.useHookRunners();
const devToolMetas = await runner.registerDev();
const hooks = api.getHooks();
const devToolMetas = await hooks.registerDev.call();

const devProgram = program
.command('dev')
Expand All @@ -39,7 +39,7 @@ export const devCommand = async (

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

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

export const buildCommand = async (
program: Command,
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
const runner = api.useHookRunners();
const platformBuilders = await runner.registerBuildPlatform();
const hooks = api.getHooks();
const platformBuilders = await hooks.registerBuildPlatform.call();

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

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

export const serverCommand = (
program: Command,
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
program
.command('serve')
Expand All @@ -101,7 +101,7 @@ export const serverCommand = (

export const deployCommand = (
program: Command,
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
program
.command('deploy')
Expand Down Expand Up @@ -152,7 +152,7 @@ export const newCommand = (program: Command, locale: string) => {

export const inspectCommand = (
program: Command,
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
) => {
program
.command('inspect')
Expand Down
6 changes: 3 additions & 3 deletions packages/solutions/app-tools/src/commands/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { PluginAPI } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import type { RsbuildMode } from '@rsbuild/core';
import type { AppTools } from '../types';
import type { InspectOptions } from '../utils/types';

export const inspect = async (
api: PluginAPI<AppTools<'shared'>>,
api: CLIPluginAPI<AppTools<'shared'>>,
options: InspectOptions,
) => {
const appContext = api.useAppContext();
const appContext = api.getAppContext();
if (!appContext.builder) {
throw new Error(
'Expect the Builder to have been initialized, But the appContext.builder received `undefined`',
Expand Down
20 changes: 12 additions & 8 deletions packages/solutions/app-tools/src/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import type { PluginAPI } from '@modern-js/core';
import type { CLIPluginAPI } from '@modern-js/plugin-v2';
import { createProdServer } from '@modern-js/prod-server';
import {
SERVER_DIR,
Expand All @@ -8,14 +8,14 @@ import {
isApiOnly,
logger,
} from '@modern-js/utils';
import type { AppTools } from '../types';
import type { AppNormalizedConfig, AppTools } from '../types';
import { loadServerPlugins } from '../utils/loadPlugins';
import { printInstructionsCompat } from '../utils/printInstructions';
import { printInstructions } from '../utils/printInstructions';

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

const {
distDirectory,
Expand Down Expand Up @@ -87,6 +87,10 @@ export const start = async (api: PluginAPI<AppTools<'shared'>>) => {
});

app.listen(port, async () => {
await printInstructionsCompat(hookRunners, appContext, userConfig);
await printInstructions(
hooks,
appContext,
userConfig as AppNormalizedConfig<'shared'>,
);
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
ServerRoute,
} from '@modern-js/types';
import type { Command } from '@modern-js/utils';
import { getModifyHtmlPartials } from '../../plugins/analyze/getHtmlTemplate';
import type { AppTools, AppToolsNormalizedConfig } from '../../types';
import type { RuntimePlugin } from '../../types/hooks';
import { getModifyHtmlPartials } from '../plugins/analyze/getHtmlTemplate';
import type { AppTools, AppToolsNormalizedConfig } from '../types';
import type { RuntimePlugin } from '../types/hooks';
import {
transformHookParams,
transformHookResult,
Expand All @@ -21,7 +21,7 @@ import {
* old plugin useHookRunners function result
*/
export function getHookRunners(
context: InternalContext<AppTools>,
context: InternalContext<AppTools<'shared'>>,
): Record<string, any> {
const { hooks } = context;
return {
Expand Down Expand Up @@ -162,24 +162,14 @@ export function getHookRunners(
/**
* @deprecated
*/
registerDev: async (params: {
name: string;
entry: string;
type: string;
config: any;
}) => {
return hooks.registerDev.call(params);
registerDev: async () => {
return hooks.registerDev.call();
},
/**
* @deprecated
*/
registerBuildPlatform: async (params: {
name: string;
entry: string;
type: string;
config: any;
}) => {
return hooks.registerBuildPlatform.call(params);
registerBuildPlatform: async () => {
return hooks.registerBuildPlatform.call();
},
/**
* @deprecated
Expand Down
Loading
Loading