mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 20:57:17 +02:00
feat: make docusaurus serve automatically open in browser (#7500)
* feat: add automatic url open to npm run serve * fix * adding ci check * add docs Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
This commit is contained in:
parent
dbd161d67c
commit
35e63515e6
3 changed files with 13 additions and 3 deletions
|
@ -148,6 +148,10 @@ cli
|
||||||
.option('-p, --port <port>', 'use specified port (default: 3000)')
|
.option('-p, --port <port>', 'use specified port (default: 3000)')
|
||||||
.option('--build', 'build website before serving (default: false)')
|
.option('--build', 'build website before serving (default: false)')
|
||||||
.option('-h, --host <host>', 'use specified host (default: localhost)')
|
.option('-h, --host <host>', 'use specified host (default: localhost)')
|
||||||
|
.option(
|
||||||
|
'--no-open',
|
||||||
|
'do not open page in the browser (default: false, or true in CI)',
|
||||||
|
)
|
||||||
.action(async (siteDir, options) =>
|
.action(async (siteDir, options) =>
|
||||||
serve(await resolveDir(siteDir), options),
|
serve(await resolveDir(siteDir), options),
|
||||||
);
|
);
|
||||||
|
|
|
@ -10,6 +10,7 @@ import path from 'path';
|
||||||
import logger from '@docusaurus/logger';
|
import logger from '@docusaurus/logger';
|
||||||
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
|
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
|
||||||
import serveHandler from 'serve-handler';
|
import serveHandler from 'serve-handler';
|
||||||
|
import openBrowser from 'react-dev-utils/openBrowser';
|
||||||
import {loadSiteConfig} from '../server/config';
|
import {loadSiteConfig} from '../server/config';
|
||||||
import {build} from './build';
|
import {build} from './build';
|
||||||
import {getHostPort, type HostPortOptions} from '../server/getHostPort';
|
import {getHostPort, type HostPortOptions} from '../server/getHostPort';
|
||||||
|
@ -19,6 +20,7 @@ export type ServeCLIOptions = HostPortOptions &
|
||||||
Pick<LoadContextOptions, 'config'> & {
|
Pick<LoadContextOptions, 'config'> & {
|
||||||
dir?: string;
|
dir?: string;
|
||||||
build?: boolean;
|
build?: boolean;
|
||||||
|
open?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function serve(
|
export async function serve(
|
||||||
|
@ -76,8 +78,11 @@ export async function serve(
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.success`Serving path=${buildDir} directory at: url=${
|
const url = servingUrl + baseUrl;
|
||||||
servingUrl + baseUrl
|
logger.success`Serving path=${buildDir} directory at: url=${url}`;
|
||||||
}`;
|
|
||||||
server.listen(port);
|
server.listen(port);
|
||||||
|
|
||||||
|
if (cliOptions.open && !process.env.CI) {
|
||||||
|
openBrowser(url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,6 +150,7 @@ Serve your built website locally.
|
||||||
| `--build` | `false` | Build website before serving |
|
| `--build` | `false` | Build website before serving |
|
||||||
| `--config` | `undefined` | Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js` |
|
| `--config` | `undefined` | Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js` |
|
||||||
| `--host` | `localhost` | Specify a host to use. For example, if you want your server to be accessible externally, you can use `--host 0.0.0.0`. |
|
| `--host` | `localhost` | Specify a host to use. For example, if you want your server to be accessible externally, you can use `--host 0.0.0.0`. |
|
||||||
|
| `--no-open` | `false` locally, `true` in CI | Do not open a browser window to the server location. |
|
||||||
|
|
||||||
### `docusaurus clear [siteDir]` {#docusaurus-clear-sitedir}
|
### `docusaurus clear [siteDir]` {#docusaurus-clear-sitedir}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue