"use strict"; /** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.start = void 0; const tslib_1 = require("tslib"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const lodash_1 = tslib_1.__importDefault(require("lodash")); const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger")); const utils_1 = require("@docusaurus/utils"); const chokidar_1 = tslib_1.__importDefault(require("chokidar")); const html_webpack_plugin_1 = tslib_1.__importDefault(require("html-webpack-plugin")); const openBrowser_1 = tslib_1.__importDefault(require("react-dev-utils/openBrowser")); const WebpackDevServerUtils_1 = require("react-dev-utils/WebpackDevServerUtils"); const evalSourceMapMiddleware_1 = tslib_1.__importDefault(require("react-dev-utils/evalSourceMapMiddleware")); const webpack_1 = tslib_1.__importDefault(require("webpack")); const webpack_dev_server_1 = tslib_1.__importDefault(require("webpack-dev-server")); const webpack_merge_1 = tslib_1.__importDefault(require("webpack-merge")); const server_1 = require("../server"); const client_1 = tslib_1.__importDefault(require("../webpack/client")); const utils_2 = require("../webpack/utils"); const getHostPort_1 = require("../server/getHostPort"); async function start(siteDirParam = '.', cliOptions = {}) { // Temporary workaround to unlock the ability to translate the site config // We'll remove it if a better official API can be designed // See https://github.com/facebook/docusaurus/issues/4542 process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale; const siteDir = await fs_extra_1.default.realpath(siteDirParam); logger_1.default.info('Starting the development server...'); function loadSite() { return (0, server_1.load)({ siteDir, config: cliOptions.config, locale: cliOptions.locale, localizePath: undefined, // Should this be configurable? }); } // Process all related files as a prop. const props = await loadSite(); const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'; const { host, port } = await (0, getHostPort_1.getHostPort)(cliOptions); if (port === null) { process.exit(); } const { baseUrl, headTags, preBodyTags, postBodyTags } = props; const urls = (0, WebpackDevServerUtils_1.prepareUrls)(protocol, host, port); const openUrl = (0, utils_1.normalizeUrl)([urls.localUrlForBrowser, baseUrl]); logger_1.default.success `Docusaurus website is running at: url=${openUrl}`; // Reload files processing. const reload = lodash_1.default.debounce(() => { loadSite() .then(({ baseUrl: newBaseUrl }) => { const newOpenUrl = (0, utils_1.normalizeUrl)([urls.localUrlForBrowser, newBaseUrl]); if (newOpenUrl !== openUrl) { logger_1.default.success `Docusaurus website is running at: url=${newOpenUrl}`; } }) .catch((err) => { logger_1.default.error(err.stack); }); }, 500); const { siteConfig, plugins, localizationDir } = props; const normalizeToSiteDir = (filepath) => { if (filepath && path_1.default.isAbsolute(filepath)) { return (0, utils_1.posixPath)(path_1.default.relative(siteDir, filepath)); } return (0, utils_1.posixPath)(filepath); }; const pluginPaths = plugins .flatMap((plugin) => plugin.getPathsToWatch?.() ?? []) .filter(Boolean) .map(normalizeToSiteDir); const pathsToWatch = [...pluginPaths, props.siteConfigPath, localizationDir]; const pollingOptions = { usePolling: !!cliOptions.poll, interval: Number.isInteger(cliOptions.poll) ? cliOptions.poll : undefined, }; const httpsConfig = await (0, utils_2.getHttpsConfig)(); const fsWatcher = chokidar_1.default.watch(pathsToWatch, { cwd: siteDir, ignoreInitial: true, ...{ pollingOptions }, }); ['add', 'change', 'unlink', 'addDir', 'unlinkDir'].forEach((event) => fsWatcher.on(event, reload)); let config = (0, webpack_merge_1.default)(await (0, client_1.default)(props, cliOptions.minify, false), { watchOptions: { ignored: /node_modules\/(?!@docusaurus)/, poll: cliOptions.poll, }, infrastructureLogging: { // Reduce log verbosity, see https://github.com/facebook/docusaurus/pull/5420#issuecomment-906613105 level: 'warn', }, plugins: [ // Generates an `index.html` file with the