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

chore(app-tools): use builder.startDevServer instead of ModernOldServer when call api-only #5430

Closed
wants to merge 1 commit into from
Closed
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/quick-jokes-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': patch
---

chore(app-tools): use builder.startDevServer instead of ModernOldServer when call api-only

chore(app-tools): 当仅启动 API 接口服务时,使用 builder.startDevServer 代替老的 ModernServer 方法
22 changes: 22 additions & 0 deletions packages/solutions/app-tools/src/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ export default ({
serverRoutes: routes,
};
api.setAppContext(appContext);

if (checkIsBuildCommands()) {
const normalizedConfig = api.useResolvedConfigContext();
const createBuilderForModern = await createBuilderGenerator(
bundler,
);
const builder = await createBuilderForModern({
normalizedConfig,
appContext,
});

appContext = {
...api.useAppContext(),
builder,
};
api.setAppContext(appContext);

builder.onAfterStartDevServer(async () => {
const hookRunners = api.useHookRunners();
printInstructions(hookRunners, appContext, normalizedConfig);
});
}
return;
}

Expand Down
40 changes: 7 additions & 33 deletions packages/solutions/app-tools/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { PluginAPI, ResolvedConfigContext } from '@modern-js/core';
import { DEFAULT_DEV_HOST } from '@modern-js/utils';
import { printInstructions } from '../utils/printInstructions';
import {
setServer,
createServer,
injectDataLoaderPlugin,
} from '../utils/createServer';
import { setServer, injectDataLoaderPlugin } from '../utils/createServer';
import { generateRoutes } from '../utils/routes';
import { DevOptions } from '../utils/types';
import { buildServerConfig } from '../utils/config';
Expand Down Expand Up @@ -50,7 +44,7 @@ export const dev = async (

await hookRunners.beforeDev();

if (!appContext.builder && !apiOnly) {
if (!appContext.builder) {
throw new Error(
'Expect the Builder to have been initialized, But the appContext.builder received `undefined`',
);
Expand Down Expand Up @@ -80,30 +74,10 @@ export const dev = async (
...devServerOptions,
};

if (apiOnly) {
const app = await createServer({
...(serverOptions as any),
compiler: null,
});

const host = normalizedConfig.dev?.host || DEFAULT_DEV_HOST;
const { server } = await appContext.builder.startDevServer({
serverOptions: serverOptions as any,
apiOnly,
});

app.listen(
{
port,
host,
},
async (err: Error) => {
if (err) {
throw err;
}
printInstructions(hookRunners, appContext, normalizedConfig);
},
);
} else {
const { server } = await appContext.builder!.startDevServer({
serverOptions: serverOptions as any,
});
setServer(server);
}
setServer(server);
};
13 changes: 1 addition & 12 deletions packages/solutions/app-tools/src/utils/createServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server, ModernDevServerOptions } from '@modern-js/server';
import { Server } from '@modern-js/server';
import type { InternalPlugins } from '@modern-js/types';

let server: Server | null = null;
Expand All @@ -16,17 +16,6 @@ export const closeServer = async () => {
}
};

export const createServer = async (options: ModernDevServerOptions) => {
if (server) {
await server.close();
}
server = new Server(options);

const app = await server.init();

return app;
};

export const injectDataLoaderPlugin = (internalPlugins: InternalPlugins) => {
const DataLoaderPlugin = require.resolve(
'@modern-js/plugin-data-loader/server',
Expand Down
24 changes: 0 additions & 24 deletions packages/solutions/app-tools/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { Server } from '@modern-js/server';
import { chalk } from '@modern-js/utils';
import {
closeServer,
createServer,
getServer,
} from '../src/utils/createServer';
import { getSelectedEntries } from '../src/utils/getSelectedEntries';
import { safeReplacer } from '../src/utils/config';

Expand Down Expand Up @@ -60,24 +54,6 @@ describe('test app-tools utils', () => {
});
});

it('should create and close server correctly', async () => {
const app = await createServer({
dev: false,
pwd: __dirname,
config: {
output: {
path: 'dist',
},
},
} as any);

expect(app instanceof Server).toBe(true);
expect(getServer()).toBe(app);

await closeServer();
expect(getServer()).toBeNull();
});

it('safeReplacer should handle circular object', () => {
const a: {
[key: string]: unknown;
Expand Down
Loading