Skip to content

fix: pass server config to Rsbuild #584

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

Merged
merged 3 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions packages/core/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function build(
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
plugins: config.plugins,
server: config.server,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function inspect(
const rsbuildInstance = await createRsbuild({
rsbuildConfig: {
plugins: config.plugins,
server: config.server,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ async function initMFRsbuild(
...(rslibConfig.plugins || []),
...(mfRsbuildConfig.config.plugins || []),
],
server: mergeRsbuildConfig(
rslibConfig.server,
mfRsbuildConfig.config.server,
),
},
});

const devServer = await rsbuildInstance.startDevServer();

onBeforeRestart(devServer.server.close);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ export async function composeCreateRsbuildConfig(
const {
lib: libConfigsArray,
plugins: sharedPlugins,
server,
...sharedRsbuildConfig
} = rslibConfig;

Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/integration/server/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "server-basic-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/integration/server/basic/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleEsmConfig()],
server: {
publicDir: {
copyOnBuild: false,
},
},
});
1 change: 1 addition & 0 deletions tests/integration/server/basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
43 changes: 43 additions & 0 deletions tests/integration/server/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { exec } from 'node:child_process';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import fse from 'fs-extra';
import { awaitFileExists, buildAndGetResults } from 'test-helper';
import { describe, expect, test } from 'vitest';

describe('server config', async () => {
test('basic config', async () => {
const fixturePath = join(__dirname, 'basic');
await buildAndGetResults({ fixturePath });

// Check if logo.svg in public is copied to dist/esm
const logoPath = join(__dirname, 'dist', 'esm', 'logo.svg');
const logoExists = existsSync(logoPath);
expect(logoExists).toBe(false);
});

test('mf dev command', async () => {
const fixturePath = join(__dirname, 'mf-dev');
const distPath = join(fixturePath, 'dist');
const rsbuildConfigFile = join(distPath, '.rsbuild/rsbuild.config.mjs');
fse.removeSync(distPath);

const childProcess = exec('npx rslib mf dev', {
cwd: fixturePath,
env: {
...process.env,
DEBUG: 'rsbuild',
},
});

await awaitFileExists(rsbuildConfigFile);
childProcess.kill();

// Check if the server config is merged correctly
const rsbuildConfigContent = await fse.readFile(rsbuildConfigFile, 'utf-8');
expect(rsbuildConfigContent).toContain(`base: '/'`);
expect(rsbuildConfigContent).toContain('open: true');
expect(rsbuildConfigContent).toContain('port: 3002');
expect(rsbuildConfigContent).toContain('printUrls: false');
});
});
6 changes: 6 additions & 0 deletions tests/integration/server/mf-dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "server-mf-dev-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
23 changes: 23 additions & 0 deletions tests/integration/server/mf-dev/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'mf',
server: {
port: 3002,
printUrls: false,
},
},
],
server: {
port: 3001,
open: true,
},
plugins: [
pluginModuleFederation({
name: 'test',
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/server/mf-dev/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
Loading