Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add script env variables: NODE_ENV + BABEL_ENV + DOCUSAURUS_CURRENT_LOCALE (temporary i18n workaround) #8677

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add docusaurus script env variables
slorber committed Feb 17, 2023

Unverified

The committer email address is not verified.
commit c4531fd1be11fef397d1b2d3904ef1b6489d5070
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
"__snapshots__",
"website/src/data/users.tsx",
"website/src/data/tweets.tsx",
"website/docusaurus.config.localized.json",
"*.xyz",
"*.docx",
"versioned_docs",
6 changes: 6 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
@@ -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 "prod
// 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]');
10 changes: 8 additions & 2 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -46,6 +46,9 @@ 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';

const siteDir = await fs.realpath(siteDirParam);

['SIGINT', 'SIGTERM'].forEach((sig) => {
@@ -117,8 +120,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({
38 changes: 29 additions & 9 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
@@ -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,
@@ -63,10 +66,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,
@@ -83,17 +103,17 @@ 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) => ({
@@ -151,7 +171,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,
},
},
],
@@ -163,7 +183,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}`;
@@ -292,7 +312,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!
@@ -332,7 +352,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 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"
}
}