From 3a73ce5ee1f1861ba65afafbf7859edb3f9688af Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Thu, 23 Mar 2023 14:58:53 +0100 Subject: [PATCH] feat(core): add script env variables: NODE_ENV + BABEL_ENV + DOCUSAURUS_CURRENT_LOCALE (temporary i18n workaround) (#8677) --- .cspell.json | 1 + packages/docusaurus/bin/docusaurus.mjs | 6 +++ packages/docusaurus/src/commands/build.ts | 11 ++++- packages/docusaurus/src/commands/start.ts | 7 +++- website/docusaurus.config.js | 50 ++++++++++++++--------- website/docusaurus.config.localized.json | 6 +++ 6 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 website/docusaurus.config.localized.json diff --git a/.cspell.json b/.cspell.json index ba31d09cfd5d..7eb6731582c1 100644 --- a/.cspell.json +++ b/.cspell.json @@ -28,6 +28,7 @@ "__snapshots__", "website/src/data/users.tsx", "website/src/data/tweets.tsx", + "website/docusaurus.config.localized.json", "*.xyz", "*.docx", "versioned_docs", diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 76cead11dbaf..00670612b9f8 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -24,6 +24,12 @@ import { } from '../lib/index.js'; import beforeCli from './beforeCli.mjs'; +// Env variables are initialized to dev, but can be overridden by each command +// For example, "docusaurus build" overrides them to "production" +// See also https://github.com/facebook/docusaurus/issues/8599 +process.env.BABEL_ENV ??= 'development'; +process.env.NODE_ENV ??= 'development'; + await beforeCli(); cli.version(DOCUSAURUS_VERSION).usage(' [options]'); diff --git a/packages/docusaurus/src/commands/build.ts b/packages/docusaurus/src/commands/build.ts index 6250f0095bef..9d33f8bc8392 100644 --- a/packages/docusaurus/src/commands/build.ts +++ b/packages/docusaurus/src/commands/build.ts @@ -46,6 +46,10 @@ export async function build( // See https://github.com/facebook/docusaurus/pull/2496 forceTerminate: boolean = true, ): Promise { + process.env.BABEL_ENV = 'production'; + process.env.NODE_ENV = 'production'; + process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale; + const siteDir = await fs.realpath(siteDirParam); ['SIGINT', 'SIGTERM'].forEach((sig) => { @@ -117,8 +121,11 @@ async function buildLocale({ forceTerminate: boolean; isLastLocale: boolean; }): Promise { - process.env.BABEL_ENV = 'production'; - process.env.NODE_ENV = 'production'; + // 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 = locale; + logger.info`name=${`[${locale}]`} Creating an optimized production build...`; const props: Props = await load({ diff --git a/packages/docusaurus/src/commands/start.ts b/packages/docusaurus/src/commands/start.ts index 849ee610bf4f..fa80c8136543 100644 --- a/packages/docusaurus/src/commands/start.ts +++ b/packages/docusaurus/src/commands/start.ts @@ -39,10 +39,13 @@ export async function start( siteDirParam: string = '.', cliOptions: Partial = {}, ): Promise { + // 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.realpath(siteDirParam); - process.env.NODE_ENV = 'development'; - process.env.BABEL_ENV = 'development'; logger.info('Starting the development server...'); function loadSite() { diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 0bf911f66daf..fecc434f8ecc 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -16,6 +16,9 @@ const { dogfoodingThemeInstances, } = require('./_dogfooding/dogfooding.config'); +/** @type {Record>} */ +const ConfigLocalized = require('./docusaurus.config.localized.json'); + const ArchivedVersionsDropdownItems = Object.entries(VersionsArchived).splice( 0, 5, @@ -23,16 +26,7 @@ const ArchivedVersionsDropdownItems = Object.entries(VersionsArchived).splice( // This probably only makes sense for the alpha/beta/rc phase, temporary function getNextVersionName() { - const expectedPrefix = '2.0.0-rc.'; - - const lastReleasedVersion = versions[0]; - if (!lastReleasedVersion || !lastReleasedVersion.includes(expectedPrefix)) { - throw new Error( - 'this code is only meant to be used during the 2.0 alpha/beta/rc phase.', - ); - } - const version = parseInt(lastReleasedVersion.replace(expectedPrefix, ''), 10); - return `${expectedPrefix}${version + 1}`; + return 'Canary'; } const isDev = process.env.NODE_ENV === 'development'; @@ -59,10 +53,27 @@ const isVersioningDisabled = !!process.env.DISABLE_VERSIONING || isI18nStaging; const TwitterSvg = ''; +const defaultLocale = 'en'; + +function getLocalizedConfigValue(/** @type {string} */ key) { + const currentLocale = process.env.DOCUSAURUS_CURRENT_LOCALE ?? defaultLocale; + const values = ConfigLocalized[key]; + if (!values) { + throw new Error(`Localized config key=${key} not found`); + } + const value = values[currentLocale] ?? values[defaultLocale]; + if (!value) { + throw new Error( + `Localized value for config key=${key} not found for both currentLocale=${currentLocale} or defaultLocale=${defaultLocale}`, + ); + } + return value; +} + /** @type {import('@docusaurus/types').Config} */ const config = { title: 'Docusaurus', - tagline: 'Build optimized websites quickly, focus on your content', + tagline: getLocalizedConfigValue('tagline'), organizationName: 'facebook', projectName: 'docusaurus', baseUrl, @@ -79,17 +90,16 @@ const config = { }, ], i18n: { - defaultLocale: 'en', - + defaultLocale, locales: isDeployPreview || isBranchDeploy ? // Deploy preview and branch deploys: keep them fast! - ['en'] + [defaultLocale] : isI18nStaging ? // Staging locales: https://docusaurus-i18n-staging.netlify.app/ - ['en', 'ja'] + [defaultLocale, 'ja'] : // Production locales - ['en', 'fr', 'pt-BR', 'ko', 'zh-CN'], + [defaultLocale, 'fr', 'pt-BR', 'ko', 'zh-CN'], }, webpack: { jsLoader: (isServer) => ({ @@ -147,7 +157,7 @@ const config = { description: 'Keep yourself up-to-date about new features in every release', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, - language: 'en', + language: defaultLocale, }, }, ], @@ -159,7 +169,7 @@ const config = { path: 'community', routeBasePath: 'community', editUrl: ({locale, versionDocsDirPath, docPath}) => { - if (locale !== 'en') { + if (locale !== defaultLocale) { return `https://crowdin.com/project/docusaurus-v2/${locale}`; } return `https://github.com/facebook/docusaurus/edit/main/website/${versionDocsDirPath}/${docPath}`; @@ -288,7 +298,7 @@ const config = { // sidebarCollapsible: false, // sidebarCollapsed: true, editUrl: ({locale, docPath}) => { - if (locale !== 'en') { + if (locale !== defaultLocale) { return `https://crowdin.com/project/docusaurus-v2/${locale}`; } // We want users to submit doc updates to the upstream/next version! @@ -324,7 +334,7 @@ const config = { // routeBasePath: '/', path: 'blog', editUrl: ({locale, blogDirPath, blogPath}) => { - if (locale !== 'en') { + if (locale !== defaultLocale) { return `https://crowdin.com/project/docusaurus-v2/${locale}`; } return `https://github.com/facebook/docusaurus/edit/main/website/${blogDirPath}/${blogPath}`; diff --git a/website/docusaurus.config.localized.json b/website/docusaurus.config.localized.json new file mode 100644 index 000000000000..1c90178b80c2 --- /dev/null +++ b/website/docusaurus.config.localized.json @@ -0,0 +1,6 @@ +{ + "tagline": { + "en": "Build optimized websites quickly, focus on your content", + "fr": "Construisez rapidement des sites web optimisés, concentrez-vous sur votre contenu" + } +}