Skip to content

Commit

Permalink
feat: improve performance of shuvi dev
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Sep 15, 2022
1 parent 455a424 commit 535aefe
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
IServerPluginContext
} from '@shuvi/service';
import { sendHTML } from '@shuvi/service/lib/server/utils';
import { renderToHTML } from './renderToHTML';
import { Response, isRedirect, isText } from '@shuvi/platform-shared/shared';
import { IHandlePageRequest } from '../serverHooks';
import { renderToHTML } from './renderToHTML';

function createPageHandler(serverPluginContext: IServerPluginContext) {
return async function (req: IncomingMessage, res: ServerResponse) {
Expand Down Expand Up @@ -35,9 +36,16 @@ function createPageHandler(serverPluginContext: IServerPluginContext) {
export async function getPageMiddleware(
api: IServerPluginContext
): Promise<ShuviRequestHandler> {
let pageHandler = createPageHandler(api);
pageHandler = await api.serverPluginRunner.handlePageRequest(pageHandler);
const defaultPageHandler = createPageHandler(api);
let pageHandler: IHandlePageRequest;

return async function (req, res, next) {
if (!pageHandler) {
pageHandler = await api.serverPluginRunner.handlePageRequest(
defaultPageHandler
);
}

try {
await pageHandler(req, res);
} catch (error) {
Expand Down
18 changes: 14 additions & 4 deletions packages/service/src/resources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from '@shuvi/utils/lib/logger';

export interface IResources {
[x: string]: any;
}
Expand All @@ -12,11 +14,19 @@ const proxyHandler = {
if (result) {
return result;
}
const proxyObj = (requireTarget && require(requireTarget)) || {};
result = proxyObj[props];
if (typeof result === 'function') {
result = result();
try {
const proxyObj = (requireTarget && require(requireTarget)) || {};
result = proxyObj[props];
if (typeof result === 'function') {
result = result();
}
} catch (error) {
logger.error(
`"resouces.${props}" is not ready, try access it when necessary.`
);
throw error;
}

if (isCached) {
cache[props] = result;
}
Expand Down
62 changes: 38 additions & 24 deletions packages/service/src/server/middlewares/dev/devMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CLIENT_OUTPUT_DIR, SERVER_OUTPUT_DIR } from '../../../constants';
import { WebpackHotMiddleware } from './hotMiddleware';
import { Bunlder } from '../../../bundler';
import { Server } from '../../http-server';
import { ShuviRequestHandler } from '../../shuviServerTypes';
import { IServerPluginContext } from '../../plugin';

type ICallback = () => void;
Expand All @@ -35,14 +36,15 @@ export interface DevMiddleware {
apply(server?: Server): void;
send(action: string, payload?: any): void;
invalidate(): Promise<unknown>;
waitUntilValid(force?: boolean): void;
onHMR(req: IncomingMessage, socket: any, head: Buffer): void;
}

export async function getDevMiddleware(
export function getDevMiddleware(
bundler: Bunlder,
serverPluginContext: IServerPluginContext
): Promise<DevMiddleware> {
): DevMiddleware {
let valid = false;
let applied = false;
const context: IContext = {
state: false,
callbacks: []
Expand Down Expand Up @@ -70,26 +72,6 @@ export async function getDevMiddleware(
compiler: bundler.getSubCompiler(BUNDLER_TARGET_CLIENT)!
});

const apply = (server: Server) => {
const targetServer = server;
targetServer.use(
launchEditorMiddleware(
DEV_HOT_LAUNCH_EDITOR_ENDPOINT,
serverPluginContext.paths.rootDir
)
);
targetServer.use(
stackFrameMiddleware(
DEV_ORIGINAL_STACK_FRAME_ENDPOINT,
bundler,
serverPluginContext.resolveBuildFile,
CLIENT_OUTPUT_DIR,
SERVER_OUTPUT_DIR
)
);
bundler.applyDevMiddlewares(server);
};

const send = (action: string, payload?: any) => {
webpackHotMiddleware.publish({ action, data: payload });
};
Expand All @@ -116,11 +98,43 @@ export async function getDevMiddleware(
});
};

const apply = (server: Server) => {
if (applied) {
return;
}
applied = true;

const targetServer = server;
targetServer.use((async (_req, _resp, next) => {
if (!valid) {
await waitUntilValid();
valid = true;
}

next();
}) as ShuviRequestHandler);
targetServer.use(
launchEditorMiddleware(
DEV_HOT_LAUNCH_EDITOR_ENDPOINT,
serverPluginContext.paths.rootDir
)
);
targetServer.use(
stackFrameMiddleware(
DEV_ORIGINAL_STACK_FRAME_ENDPOINT,
bundler,
serverPluginContext.resolveBuildFile,
CLIENT_OUTPUT_DIR,
SERVER_OUTPUT_DIR
)
);
bundler.applyDevMiddlewares(server);
};

return {
apply,
send,
invalidate,
waitUntilValid,
onHMR
};
}
3 changes: 1 addition & 2 deletions packages/service/src/server/shuviDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export class ShuviDevServer extends ShuviServer {

async init() {
const { _serverContext: context, _server: server } = this;
const devMiddleware = await getDevMiddleware(this._bundler, context);
await devMiddleware.waitUntilValid();
const devMiddleware = getDevMiddleware(this._bundler, context);

if (context.config.proxy) {
applyHttpProxyMiddleware(server, context.config.proxy);
Expand Down
2 changes: 1 addition & 1 deletion packages/service/src/server/shuviServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export abstract class ShuviServer implements IShuviServer {

async listen(port: number, hostname?: string) {
await this._server.listen(port, hostname);
await this._pluginManager.runner.listen({ port, hostname });
this._pluginManager.runner.listen({ port, hostname });
}

async close() {
Expand Down

0 comments on commit 535aefe

Please sign in to comment.