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: support --lib param of mf-dev command #590

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion examples/module-federation/mf-react-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"types": "./dist/cjs/index.d.ts",
"scripts": {
"build": "rslib build",
"dev": "rslib mf dev",
"dev": "rslib mf-dev",
"serve": "pnpm build & http-server -p 3001 ./dist/ --cors",
"storybook": "storybook dev -p 6006"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/asset/assetConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { RsbuildConfig } from '@rsbuild/core';
import type { EnvironmentConfig } from '@rsbuild/core';
import type { Format } from '../types';

// TODO: asset config document
export const composeAssetConfig = (
bundle: boolean,
format: Format,
): RsbuildConfig => {
): EnvironmentConfig => {
if (format === 'esm' || format === 'cjs') {
if (bundle) {
return {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { onBeforeRestart } from './restart';

export async function build(
config: RslibConfig,
options: Pick<BuildOptions, 'lib' | 'watch'> = {},
options: Pick<BuildOptions, 'lib' | 'watch' | 'root'> = {},
): Promise<RsbuildInstance> {
const environments = await composeRsbuildEnvironments(config);
const { environments } = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
mode: 'production',
root: config.root,
plugins: config.plugins,
dev: config.dev,
server: config.server,
environments: pruneEnvironments(environments, options.lib),
},
Expand Down
26 changes: 11 additions & 15 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ const applyCommonOptions = (command: Command) => {
'--env-mode <mode>',
'specify the env mode to load the `.env.[mode]` file',
)
.option('--env-dir <dir>', 'specify the directory to load `.env` files');
.option('--env-dir <dir>', 'specify the directory to load `.env` files')
.option(
'--lib <id>',
'specify the library (repeatable, e.g. --lib esm --lib cjs)',
repeatableOption,
);
};

const repeatableOption = (value: string, previous: string[]) => {
Expand All @@ -51,16 +56,11 @@ export function runCli(): void {

const buildCommand = program.command('build');
const inspectCommand = program.command('inspect');
const mfDevCommand = program.command('mf dev');
const mfDevCommand = program.command('mf-dev');

[buildCommand, inspectCommand, mfDevCommand].forEach(applyCommonOptions);

buildCommand
.option(
'--lib <id>',
'build the specified library (may be repeated)',
repeatableOption,
)
.option('-w --watch', 'turn on watch mode, watch for changes and rebuild')
.description('build the library for production')
.action(async (options: BuildOptions) => {
Expand All @@ -87,11 +87,6 @@ export function runCli(): void {

inspectCommand
.description('inspect the Rsbuild / Rspack configs of Rslib projects')
.option(
'--lib <id>',
'inspect the specified library (may be repeated)',
repeatableOption,
)
.option(
'--output <output>',
'specify inspect content output path',
Expand Down Expand Up @@ -121,8 +116,9 @@ export function runCli(): void {
try {
const cliMfDev = async () => {
const { config, watchFiles } = await init(options);
// TODO: support lib option in mf dev server
await startMFDevServer(config);
await startMFDevServer(config, {
lib: options.lib,
});

watchFilesForRestart(watchFiles, async () => {
await cliMfDev();
Expand All @@ -131,7 +127,7 @@ export function runCli(): void {

await cliMfDev();
} catch (err) {
logger.error('Failed to start mf dev.');
logger.error('Failed to start mf-dev.');
logger.error(err);
process.exit(1);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export async function inspect(
config: RslibConfig,
options: Pick<InspectOptions, 'lib' | 'mode' | 'output' | 'verbose'> = {},
): Promise<RsbuildInstance> {
const environments = await composeRsbuildEnvironments(config);
const { environments } = await composeRsbuildEnvironments(config);
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
mode: 'production',
root: config.root,
plugins: config.plugins,
dev: config.dev,
server: config.server,
environments: pruneEnvironments(environments, options.lib),
},
Expand Down
82 changes: 43 additions & 39 deletions packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import { createRsbuild, mergeRsbuildConfig } from '@rsbuild/core';
import type { RsbuildConfig, RsbuildInstance } from '@rsbuild/core';
import { composeCreateRsbuildConfig } from '../config';
import { createRsbuild } from '@rsbuild/core';
import type { RsbuildInstance } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types';
import type { CommonOptions } from './commands';
import { onBeforeRestart } from './restart';

export async function startMFDevServer(
config: RslibConfig,
options: Pick<CommonOptions, 'lib'> = {},
): Promise<RsbuildInstance | undefined> {
const rsbuildInstance = await initMFRsbuild(config);
const rsbuildInstance = await initMFRsbuild(config, options);
return rsbuildInstance;
}

async function initMFRsbuild(
rslibConfig: RslibConfig,
config: RslibConfig,
options: Pick<CommonOptions, 'lib'> = {},
): Promise<RsbuildInstance | undefined> {
const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
const mfRsbuildConfig = rsbuildConfigObject.find(
(config) => config.format === 'mf',
);
const { environments, environmentWithInfos } =
await composeRsbuildEnvironments(config);

if (!mfRsbuildConfig) {
// no mf format, return.
return;
const selectedEnvironmentIds = environmentWithInfos
.filter((env) => {
const isMf = env.format === 'mf';
if (!options?.lib) {
return isMf;
}
return env.id && options.lib.includes(env.id);
})
.map((env) => env.id);

if (!selectedEnvironmentIds.length) {
throw new Error('No mf format found, please check your config.');
}

mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);
const selectedEnvironments = pruneEnvironments(
environments,
selectedEnvironmentIds,
);

const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
...mfRsbuildConfig.config,
plugins: [
...(rslibConfig.plugins || []),
...(mfRsbuildConfig.config.plugins || []),
],
server: mergeRsbuildConfig(
rslibConfig.server,
mfRsbuildConfig.config.server,
),
mode: 'development',
root: config.root,
plugins: config.plugins,
dev: {
...(config.dev ?? {}),
writeToDisk: true,
},
server: config.server,
tools: {
rspack: {
optimization: {
nodeEnv: 'development',
moduleIds: 'named',
},
},
},
environments: selectedEnvironments,
},
});

Expand All @@ -45,20 +66,3 @@ async function initMFRsbuild(
onBeforeRestart(devServer.server.close);
return rsbuildInstance;
}

function changeEnvToDev(rsbuildConfig: RsbuildConfig) {
return mergeRsbuildConfig(rsbuildConfig, {
mode: 'development',
dev: {
writeToDisk: true,
},
tools: {
rspack: {
optimization: {
nodeEnv: 'development',
moduleIds: 'named',
},
},
},
});
}
Loading
Loading