mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-28 17:57:48 +02:00
feat(core): add script env variables: NODE_ENV + BABEL_ENV + DOCUSAURUS_CURRENT_LOCALE (temporary i18n workaround) (#8677)
This commit is contained in:
parent
e8d971d2d6
commit
3a73ce5ee1
6 changed files with 57 additions and 24 deletions
|
@ -28,6 +28,7 @@
|
|||
"__snapshots__",
|
||||
"website/src/data/users.tsx",
|
||||
"website/src/data/tweets.tsx",
|
||||
"website/docusaurus.config.localized.json",
|
||||
"*.xyz",
|
||||
"*.docx",
|
||||
"versioned_docs",
|
||||
|
|
|
@ -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('<command> [options]');
|
||||
|
|
|
@ -46,6 +46,10 @@ export async function build(
|
|||
// See https://github.com/facebook/docusaurus/pull/2496
|
||||
forceTerminate: boolean = true,
|
||||
): Promise<string> {
|
||||
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<string> {
|
||||
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({
|
||||
|
|
|
@ -39,10 +39,13 @@ export async function start(
|
|||
siteDirParam: string = '.',
|
||||
cliOptions: Partial<StartCLIOptions> = {},
|
||||
): Promise<void> {
|
||||
// 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() {
|
||||
|
|
|
@ -16,6 +16,9 @@ const {
|
|||
dogfoodingThemeInstances,
|
||||
} = require('./_dogfooding/dogfooding.config');
|
||||
|
||||
/** @type {Record<string,Record<string,string>>} */
|
||||
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 =
|
||||
'<svg style="fill: #1DA1F2; vertical-align: middle; margin-left: 3px;" width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg>';
|
||||
|
||||
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}`;
|
||||
|
|
6
website/docusaurus.config.localized.json
Normal file
6
website/docusaurus.config.localized.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue