Skip to content

Commit

Permalink
perf: make open browser faster (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Nov 8, 2023
1 parent c743a94 commit 58306e2
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-elephants-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rsbuild/core': patch
---

perf: make open browser faster
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// words - list of words to be always considered correct
"words": [
"antd",
"applescript",
"atrules",
"deepmerge",
"docgen",
Expand All @@ -24,6 +25,7 @@
"onclosetag",
"onopentag",
"ontext",
"osascript",
"outro",
"oxlint",
"pluggable",
Expand Down
26 changes: 10 additions & 16 deletions packages/core/src/plugins/startUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { join } from 'path';
import { logger, castArray, type DefaultRsbuildPlugin } from '@rsbuild/shared';
import { execSync } from 'child_process';
import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);

const supportedChromiumBrowsers = [
'Google Chrome Canary',
Expand All @@ -13,12 +16,12 @@ const supportedChromiumBrowsers = [
'Chromium',
];

const getTargetBrowser = () => {
const getTargetBrowser = async () => {
// Use user setting first
let targetBrowser = process.env.BROWSER;
// If user setting not found or not support, use opening browser first
if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
const ps = execSync('ps cax').toString();
const { stdout: ps } = await execAsync('ps cax');
targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
}
return targetBrowser;
Expand All @@ -41,18 +44,18 @@ export async function openBrowser(url: string): Promise<boolean> {

if (shouldTryOpenChromeWithAppleScript) {
try {
const targetBrowser = getTargetBrowser();
const targetBrowser = await getTargetBrowser();
if (targetBrowser) {
// Try to reuse existing tab with AppleScript
execSync(
await execAsync(
`osascript openChrome.applescript "${encodeURI(
url,
)}" "${targetBrowser}"`,
{
stdio: 'ignore',
cwd: join(__dirname, '../../static'),
},
);

return true;
}
return false;
Expand Down Expand Up @@ -85,17 +88,8 @@ export function pluginStartUrl(): DefaultRsbuildPlugin {
return {
name: 'plugin-start-url',
setup(api) {
let port: number;

api.onAfterStartDevServer(async (params) => {
({ port } = params);
});

api.onDevCompileDone(async ({ isFirstCompile }) => {
if (!isFirstCompile || !port) {
return;
}

const { port } = params;
const config = api.getNormalizedConfig();
const { startUrl, beforeStartUrl } = config.dev;
const { open, https } = api.context.devServer || {};
Expand Down
22 changes: 0 additions & 22 deletions packages/core/tests/plugins/open.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
}
`;
exports[`applyDefaultPlugins > should apply default plugins correctyly when target = node 1`] = `
exports[`applyDefaultPlugins > should apply default plugins correctly when target = node 1`] = `
{
"builtins": {
"css": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('applyDefaultPlugins', () => {
process.env.NODE_ENV = NODE_ENV;
});

it('should apply default plugins correctyly when target = node', async () => {
it('should apply default plugins correctly when target = node', async () => {
const { NODE_ENV } = process.env;
process.env.NODE_ENV = 'test';
const rsbuild = await createStubRsbuild({
Expand Down
3 changes: 0 additions & 3 deletions packages/shared/tests/a.test.ts

This file was deleted.

0 comments on commit 58306e2

Please sign in to comment.