mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 12:22:45 +02:00
perf(core): Optimize docusaurus start/serve
, fix openBrowser()
perf issue on macOS (#11007)
Optimize openBrowser() util
This commit is contained in:
parent
3782244ce7
commit
502b9007be
1 changed files with 63 additions and 27 deletions
|
@ -12,8 +12,12 @@
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
|
||||||
import {execSync} from 'child_process';
|
import {exec} from 'child_process';
|
||||||
|
import {promisify} from 'util';
|
||||||
import open from 'open';
|
import open from 'open';
|
||||||
|
import {PerfLogger} from '@docusaurus/logger';
|
||||||
|
|
||||||
|
const execPromise = promisify(exec);
|
||||||
|
|
||||||
type BrowserName = string | undefined;
|
type BrowserName = string | undefined;
|
||||||
type BrowserArgs = string[];
|
type BrowserArgs = string[];
|
||||||
|
@ -49,38 +53,66 @@ async function tryOpenWithAppleScript({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldTryOpenChromiumWithAppleScript) {
|
if (shouldTryOpenChromiumWithAppleScript) {
|
||||||
// Will use the first open browser found from list
|
async function getBrowsersToTry(): Promise<string[]> {
|
||||||
const supportedChromiumBrowsers = [
|
// Will use the first open browser found from list
|
||||||
'Google Chrome Canary',
|
const supportedChromiumBrowsers = [
|
||||||
'Google Chrome Dev',
|
'Google Chrome Canary',
|
||||||
'Google Chrome Beta',
|
'Google Chrome Dev',
|
||||||
'Google Chrome',
|
'Google Chrome Beta',
|
||||||
'Microsoft Edge',
|
'Google Chrome',
|
||||||
'Brave Browser',
|
'Microsoft Edge',
|
||||||
'Vivaldi',
|
'Brave Browser',
|
||||||
'Chromium',
|
'Vivaldi',
|
||||||
];
|
'Chromium',
|
||||||
|
];
|
||||||
|
|
||||||
for (let chromiumBrowser of supportedChromiumBrowsers) {
|
// Among all the supported browsers, retrieves to stdout the active ones
|
||||||
|
const command = `ps cax -o command | grep -E "^(${supportedChromiumBrowsers.join(
|
||||||
|
'|',
|
||||||
|
)})$"`;
|
||||||
|
const result = await execPromise(command);
|
||||||
|
|
||||||
|
const activeBrowsers = result.stdout.toString().trim().split('\n');
|
||||||
|
|
||||||
|
// This preserves the initial browser order
|
||||||
|
// We open Google Chrome Canary in priority over Google Chrome
|
||||||
|
return supportedChromiumBrowsers.filter((b) =>
|
||||||
|
activeBrowsers.includes(b),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function tryBrowser(browserName: string): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
// Try our best to reuse existing tab
|
// This command runs the openChrome.applescript (copied from CRA)
|
||||||
// on OSX Chromium-based browser with AppleScript
|
const command = `osascript openChrome.applescript "${encodeURI(
|
||||||
execSync(`ps cax | grep "${chromiumBrowser}"`);
|
url,
|
||||||
execSync(
|
)}" "${browserName}"`;
|
||||||
`osascript openChrome.applescript "${encodeURI(
|
await execPromise(command, {
|
||||||
url,
|
cwd: __dirname,
|
||||||
)}" "${chromiumBrowser}"`,
|
});
|
||||||
{
|
|
||||||
cwd: __dirname,
|
|
||||||
stdio: 'ignore',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Ignore errors.
|
console.error(
|
||||||
|
`Failed to open browser ${browserName} with AppleScript`,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const browsers = await PerfLogger.async('getBrowsersToTry', () =>
|
||||||
|
getBrowsersToTry(),
|
||||||
|
);
|
||||||
|
for (let browser of browsers) {
|
||||||
|
const success = await PerfLogger.async(browser, () =>
|
||||||
|
tryBrowser(browser),
|
||||||
|
);
|
||||||
|
if (success) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +135,11 @@ function toOpenApp(params: Params): open.App | undefined {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startBrowserProcess(params: Params): Promise<boolean> {
|
async function startBrowserProcess(params: Params): Promise<boolean> {
|
||||||
if (await tryOpenWithAppleScript(params)) {
|
if (
|
||||||
|
await PerfLogger.async('tryOpenWithAppleScript', () =>
|
||||||
|
tryOpenWithAppleScript(params),
|
||||||
|
)
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue