Skip to content

Commit

Permalink
fix: do not open browser when open parameter is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
esskar committed Dec 29, 2024
1 parent a0e518e commit 81de6f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/core/src/server/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ export function resolveUrl(str: string, base: string): string {

const normalizeOpenConfig = (
config: NormalizedConfig,
): { targets: string[]; before?: () => Promise<void> | void } => {
): {
targets: string[];
before?: () => Promise<void> | void;
skip?: boolean;
} => {
const { open } = config.server;

if (typeof open === 'boolean') {
return { targets: [] };
return { targets: [], skip: !open };
}
if (typeof open === 'string') {
return { targets: [open] };
Expand Down Expand Up @@ -139,7 +143,10 @@ export async function open({
config: NormalizedConfig;
clearCache?: boolean;
}): Promise<void> {
const { targets, before } = normalizeOpenConfig(config);
const { targets, before, skip } = normalizeOpenConfig(config);
if (skip) {
return;
}

// Skip open in codesandbox. After being bundled, the `open` package will
// try to call system xdg-open, which will cause an error on codesandbox.
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tests/open.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { replacePortPlaceholder, resolveUrl } from '../src/server/open';
import type { NormalizedConfig, NormalizedServerConfig } from '../dist-types';
import { open, replacePortPlaceholder, resolveUrl } from '../src/server/open';

describe('plugin-open', () => {
it('#replacePortPlaceholder - should replace port number correctly', () => {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/en/config/server/open.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export default {
};
```

- Do not open any browser at all:

```js
export default {
server: {
open: false,
},
};
```

## Port placeholder

The port number that Rsbuild server listens on may change. For example, if the port is in use, Rsbuild will automatically increment the port number until it finds an available port.
Expand Down

0 comments on commit 81de6f8

Please sign in to comment.