Skip to content

Commit

Permalink
feat(core): add script env variables: NODE_ENV + BABEL_ENV + DOCUSAUR…
Browse files Browse the repository at this point in the history
…US_CURRENT_LOCALE (temporary i18n workaround) (#8677)
  • Loading branch information
slorber committed Mar 23, 2023
1 parent e8d971d commit 3a73ce5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 24 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"__snapshots__",
"website/src/data/users.tsx",
"website/src/data/tweets.tsx",
"website/docusaurus.config.localized.json",
"*.xyz",
"*.docx",
"versioned_docs",
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]');
Expand Down
11 changes: 9 additions & 2 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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({
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
50 changes: 30 additions & 20 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@ 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,
);

// 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';
Expand All @@ -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,
Expand All @@ -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) => ({
Expand Down Expand Up @@ -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,
},
},
],
Expand All @@ -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}`;
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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}`;
Expand Down
6 changes: 6 additions & 0 deletions website/docusaurus.config.localized.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 3a73ce5

Please sign in to comment.