Skip to content

Commit

Permalink
Merge branch 'main' into fix/router-v5-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Jul 24, 2024
2 parents 7aa8548 + 88e3f9a commit af6f8c5
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 407 deletions.
2 changes: 0 additions & 2 deletions packages/cli/uni-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
"@babel/preset-react": "^7.22.15",
"@babel/types": "^7.24.7",
"@modern-js/utils": "workspace:*",
"@modern-js/server": "workspace:*",
"@modern-js/prod-server": "workspace:*",
"@modern-js/babel-preset": "workspace:*",
"@pmmmwh/react-refresh-webpack-plugin": "0.5.10",
"@rsbuild/core": "1.0.1-beta.3",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/uni-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type {
MultiStats,
RspackConfig,
} from './types';
export type { StartDevServerOptions } from './shared/devServer';

export async function createUniBuilder(options: CreateUniBuilderOptions) {
return options.bundlerType === 'rspack'
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/uni-builder/src/rspack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
} from '../types';
import { parseCommonConfig } from '../shared/parseCommonConfig';
import { compatLegacyPlugin } from '../shared/compatLegacyPlugin';
import type { StartDevServerOptions } from '../shared/devServer';
import { SERVICE_WORKER_ENVIRONMENT_NAME } from '../shared/utils';

export async function parseConfig(
Expand Down Expand Up @@ -91,10 +90,5 @@ export async function createRspackBuilder(
});
rsbuild.addPlugins(warpedPlugins, options);
},
startDevServer: async (options: StartDevServerOptions = {}) => {
const { startDevServer } = await import('../shared/devServer');

return startDevServer(rsbuild, options, config);
},
};
}
202 changes: 30 additions & 172 deletions packages/cli/uni-builder/src/shared/devServer.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
import { merge } from 'ts-deepmerge';
import { applyOptionsChain, isProd } from '@modern-js/utils';

import {
type RsbuildInstance,
logger,
type DevConfig,
type ServerConfig,
type Rspack,
} from '@rsbuild/core';

import type { ModernDevServerOptions } from '@modern-js/server';
import type { Server } from 'node:http';
import {
applyPlugins,
type ApplyPlugins,
type ProdServerOptions as ModernServerOptions,
} from '@modern-js/prod-server';
import type {
UniBuilderConfig,
ToolsDevServerConfig,
DevServerHttpsOptions,
} from '../types';

type ServerOptions = Partial<Omit<ModernDevServerOptions, 'config'>> & {
config?: Partial<ModernDevServerOptions['config']>;
};

const getServerOptions = (
builderConfig: UniBuilderConfig,
): ModernServerOptions['config'] => {
return {
output: {
path: builderConfig.output?.distPath?.root,
assetPrefix: builderConfig.output?.assetPrefix,
distPath: builderConfig.output?.distPath,
},
source: {
alias: {},
},
html: {},
tools: {
babel: {},
},
server: {},
runtime: {},
bff: {},
dev: {},
security: {},
};
import { type DevConfig, type ServerConfig } from '@rsbuild/core';

import type { UniBuilderConfig, ToolsDevServerConfig } from '../types';

const transformDevSetupMiddlewares = (
seuptMiddlewares: DevConfig['setupMiddlewares'],
): DevConfig['setupMiddlewares'] => {
if (seuptMiddlewares) {
const newSetupMiddlewares: DevConfig['setupMiddlewares'] =
seuptMiddlewares.map(handler => (_, server) => {
handler(
{
unshift() {
// ignore
},
push() {
// ignore
},
},
server,
);
});
return newSetupMiddlewares;
}
return undefined;
};

export const transformToRsbuildServerOptions = (
Expand Down Expand Up @@ -101,19 +79,11 @@ export const transformToRsbuildServerOptions = (
rsbuildDev.progressBar = true;
}

if (newDevServerConfig.before?.length || newDevServerConfig.after?.length) {
rsbuildDev.setupMiddlewares = [
...(newDevServerConfig.setupMiddlewares || []),
middlewares => {
// the order: devServer.before => setupMiddlewares.unshift => internal middlewares => setupMiddlewares.push => devServer.after.
middlewares.unshift(...(newDevServerConfig.before || []));

middlewares.push(...(newDevServerConfig.after || []));
},
];
} else if (newDevServerConfig.setupMiddlewares) {
rsbuildDev.setupMiddlewares = newDevServerConfig.setupMiddlewares;
}
// devConfig.setupMiddlewares, devConfig.after, devConfig.before apply by @modern-js/server
// setupMiddlewares apply by @modern-js/server
rsbuildDev.setupMiddlewares = transformDevSetupMiddlewares(
newDevServerConfig.setupMiddlewares,
);

const server: ServerConfig = isProd()
? {
Expand Down Expand Up @@ -145,115 +115,3 @@ export const transformToRsbuildServerOptions = (

return { dev: rsbuildDev, server };
};

const getDevServerOptions = async ({
builderConfig,
serverOptions,
}: {
builderConfig: UniBuilderConfig;
serverOptions: ServerOptions;
}): Promise<{
config: ModernDevServerOptions['config'];
}> => {
const defaultConfig = getServerOptions(builderConfig);
const config = serverOptions.config
? (merge(defaultConfig, serverOptions.config) as typeof defaultConfig)
: defaultConfig;

return { config };
};

export type StartDevServerOptions = {
compiler?: Rspack.Compiler | Rspack.MultiCompiler;
getPortSilently?: boolean;
apiOnly?: boolean;
serverOptions?: ServerOptions;
applyPlugins?: ApplyPlugins;
};

export type UniBuilderStartServerResult = {
server: Server;
port: number;
};

export async function startDevServer(
rsbuild: RsbuildInstance,
options: StartDevServerOptions = {},
builderConfig: UniBuilderConfig,
) {
logger.debug('create dev server');

if (!options.applyPlugins) {
options.applyPlugins = applyPlugins;
}

const { createDevServer } = await import('@modern-js/server');

const rsbuildServer = await rsbuild.createDevServer({
...options,
runCompile: !options.apiOnly,
});

const { serverOptions = {} } = options;

const { config } = await getDevServerOptions({
builderConfig,
serverOptions,
});

const rsbuildConfig = rsbuild.getNormalizedConfig();

const https = serverOptions.dev?.https ?? rsbuildConfig.server.https;

const { port } = rsbuildServer;
const {
server: { host },
dev: { writeToDisk },
} = rsbuildConfig;

const server = await createDevServer(
{
pwd: rsbuild.context.rootPath,
...serverOptions,
appContext: serverOptions.appContext || {},
rsbuild,
getMiddlewares: () => ({
middlewares: rsbuildServer.middlewares,
close: rsbuildServer.close,
onHTTPUpgrade: rsbuildServer.onHTTPUpgrade,
}),
dev: {
watch: serverOptions.dev?.watch ?? true,
https: https as DevServerHttpsOptions,
writeToDisk,
},
config,
},
options.applyPlugins,
);

logger.debug('listen dev server');

return new Promise<UniBuilderStartServerResult>(resolve => {
server.listen(
{
host,
port,
},
async (err?: Error) => {
if (err) {
throw err;
}

logger.debug('listen dev server done');

await rsbuildServer.afterListen();

resolve({
port,
server,
});
},
);
});
}
12 changes: 0 additions & 12 deletions packages/cli/uni-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import type { PluginBabelOptions } from '@rsbuild/plugin-babel';
import type { PluginSassOptions } from '@rsbuild/plugin-sass';
import type { PluginLessOptions } from '@rsbuild/plugin-less';
import type { AliasOption } from '@modern-js/utils';
import type {
StartDevServerOptions,
UniBuilderStartServerResult,
} from './shared/devServer';
import type { PluginSourceBuildOptions } from '@rsbuild/plugin-source-build';
import type TerserPlugin from 'terser-webpack-plugin';
import type { Options as HTMLPluginOptions } from 'html-webpack-plugin';
Expand Down Expand Up @@ -377,14 +373,6 @@ export type OverridesUniBuilderInstance = {
before?: string;
},
) => void;
/**
* should be used in conjunction with the upper-layer framework:
*
* missing route.json (required in modern server)
*/
startDevServer: (
options: StartDevServerOptions,
) => Promise<UniBuilderStartServerResult>;
};

export type UniBuilderContext = RsbuildPluginAPI['context'] & {
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/uni-builder/src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { compatLegacyPlugin } from '../shared/compatLegacyPlugin';
import { pluginModuleScopes } from './plugins/moduleScopes';
import { pluginBabel } from './plugins/babel';
import { pluginReact } from './plugins/react';
import type { StartDevServerOptions } from '../shared/devServer';
import { SERVICE_WORKER_ENVIRONMENT_NAME } from '../shared/utils';

export async function parseConfig(
Expand Down Expand Up @@ -134,10 +133,5 @@ export async function createWebpackBuilder(
});
rsbuild.addPlugins(warpedPlugins, options);
},
startDevServer: async (options: StartDevServerOptions = {}) => {
const { startDevServer } = await import('../shared/devServer');

return startDevServer(rsbuild, options, config);
},
};
}
1 change: 1 addition & 0 deletions packages/runtime/plugin-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"@modern-js/utils": "workspace:*",
"@modern-js/plugin": "workspace:*",
"@modern-js/prod-server": "workspace:*",
"@modern-js/server-core": "workspace:*",
"@modern-js/types": "workspace:*",
"@modern-js/babel-preset": "workspace:*",
"@testing-library/jest-dom": "^5.16.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/plugin-testing/src/cli/bff/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CliPlugin, IAppContext } from '@modern-js/core';
import { isApiOnly } from '@modern-js/utils';
import { ServerPlugin } from '@modern-js/prod-server';
// must import from server-core, due to ts compiler error.
import type { ServerPlugin } from '@modern-js/server-core';
import { UserConfig } from '../../base/config';
import {
TestConfigOperator,
Expand Down
1 change: 1 addition & 0 deletions packages/server/prod-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
loadServerPlugins,
loadServerRuntimeConfig,
} from '@modern-js/server-core/node';

export type { ServerPlugin } from '@modern-js/server-core';

export type { ProdServerOptions, BaseEnv } from './types';
Expand Down
2 changes: 1 addition & 1 deletion packages/server/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"ws": "^8.13.0"
},
"devDependencies": {
"@rsbuild/core": "1.0.1-beta.3",
"@modern-js/uni-builder": "workspace:*",
"@scripts/build": "workspace:*",
"@scripts/jest-config": "workspace:*",
"@types/connect-history-api-fallback": "^1.3.5",
Expand Down
27 changes: 18 additions & 9 deletions packages/server/server/src/createDevServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Server as NodeServer } from 'node:http';
import path from 'node:path';
import { ServerBaseOptions, createServerBase } from '@modern-js/server-core';
import { createServerBase } from '@modern-js/server-core';
import {
createNodeServer,
loadServerRuntimeConfig,
Expand All @@ -9,12 +8,10 @@ import { ApplyPlugins, ModernDevServerOptions } from './types';
import { getDevOptions } from './helpers';
import { devPlugin } from './dev';

export type { ModernDevServerOptions } from './types';

export const createDevServer = async <O extends ServerBaseOptions>(
options: ModernDevServerOptions<O>,
applyPlugins: ApplyPlugins<O>,
): Promise<NodeServer> => {
export async function createDevServer(
options: ModernDevServerOptions,
applyPlugins: ApplyPlugins,
) {
const { config, pwd, serverConfigFile, serverConfigPath } = options;
const dev = getDevOptions(options);

Expand Down Expand Up @@ -56,5 +53,17 @@ export const createDevServer = async <O extends ServerBaseOptions>(

await server.init();

nodeServer.listen(
{
host: dev.host || '127.0.0.1',
port: dev.port || '8080',
},
(err?: Error) => {
if (err) {
throw err;
}
},
);

return nodeServer;
};
}
Loading

0 comments on commit af6f8c5

Please sign in to comment.