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 a089bb4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
20 changes: 15 additions & 5 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 All @@ -138,15 +142,18 @@ export async function open({
routes: Routes;
config: NormalizedConfig;
clearCache?: boolean;
}): Promise<void> {
const { targets, before } = normalizeOpenConfig(config);
}): Promise<boolean> {
const { targets, before, skip } = normalizeOpenConfig(config);
if (skip) {
return false;
}

// Skip open in codesandbox. After being bundled, the `open` package will
// try to call system xdg-open, which will cause an error on codesandbox.
// https://github.com/codesandbox/codesandbox-client/issues/6642
const isCodesandbox = process.env.CSB === 'true';
if (isCodesandbox) {
return;
return false;
}

if (clearCache) {
Expand Down Expand Up @@ -175,6 +182,7 @@ export async function open({
await before();
}

let opened = false;
for (const url of urls) {
/**
* If an URL has been opened in current process, we will not open it again.
Expand All @@ -183,6 +191,8 @@ export async function open({
if (!openedURLs.includes(url)) {
openBrowser(url);
openedURLs.push(url);
opened = true;
}
}
return opened;
}
29 changes: 28 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 Expand Up @@ -30,4 +31,30 @@ describe('plugin-open', () => {
expect(resolveUrl('//localhost', baseUrl)).toEqual('http://localhost/');
expect(resolveUrl('path', baseUrl)).toEqual('http://localhost/path');
});

it('#open - should return false when open parameter is false', async () => {
const result = await open({
port: 3000,
routes: [],
config: {
server: {
open: false,
} as NormalizedServerConfig,
} as NormalizedConfig,
});
expect(result).toBe(false);
});

it('#open - should return true when open parameter is true', async () => {
const result = await open({
port: 3000,
routes: [],
config: {
server: {
open: true,
} as NormalizedServerConfig,
} as NormalizedConfig,
});
expect(result).toBe(false);
});
});
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 a089bb4

Please sign in to comment.