Skip to content

Commit

Permalink
cleanup signature of extractSiteSourceCodeTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Sep 20, 2024
1 parent 1589f9d commit 785f676
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/docusaurus-babel/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function getBabelOptions({
babelOptions,
}: {
isServer?: boolean;
// TODO Docusaurus v4 fix this
// weird to have getBabelOptions take a babelOptions param
babelOptions?: TransformOptions | string;
} = {}): TransformOptions {
const caller = {name: isServer ? 'server' : 'client'};
Expand Down
12 changes: 3 additions & 9 deletions packages/docusaurus/src/commands/writeTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import fs from 'fs-extra';
import path from 'path';
import {getCustomBabelConfigFilePath, getBabelOptions} from '@docusaurus/babel';
import {loadContext, type LoadContextParams} from '../server/site';
import {initPlugins} from '../server/plugins/init';
import {
Expand Down Expand Up @@ -103,16 +102,11 @@ Available locales are: ${context.i18n.locales.join(',')}.`,
);
}

const babelOptions = getBabelOptions({
isServer: true,
babelOptions: await getCustomBabelConfigFilePath(siteDir),
});
const extractedCodeTranslations = await extractSiteSourceCodeTranslations(
const extractedCodeTranslations = await extractSiteSourceCodeTranslations({
siteDir,
plugins,
babelOptions,
await getExtraSourceCodeFilePaths(),
);
extraSourceCodeFilePaths: await getExtraSourceCodeFilePaths(),
});

const defaultCodeMessages = await loadPluginsDefaultCodeTranslationMessages(
plugins,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,10 @@ export default function MyComponent(props: Props) {
plugin2,
{name: 'dummy', options: {}, version: {type: 'synthetic'}} as const,
] as LoadedPlugin[];
const translations = await extractSiteSourceCodeTranslations(
const translations = await extractSiteSourceCodeTranslations({
siteDir,
plugins,
TestBabelOptions,
);
});
expect(translations).toEqual({
siteComponentFileId1: {
description: 'site component 1 desc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
type TransformOptions,
} from '@babel/core';
import {SRC_DIR_NAME} from '@docusaurus/utils';
import {getBabelOptions, getCustomBabelConfigFilePath} from '@docusaurus/babel';
import {safeGlobby} from '../utils';
import type {
InitializedPlugin,
Expand Down Expand Up @@ -82,12 +83,20 @@ async function getSourceCodeFilePaths(
return globSourceCodeFilePaths(allPaths);
}

export async function extractSiteSourceCodeTranslations(
siteDir: string,
plugins: InitializedPlugin[],
babelOptions: TransformOptions,
extraSourceCodeFilePaths: string[] = [],
): Promise<TranslationFileContent> {
export async function extractSiteSourceCodeTranslations({
siteDir,
plugins,
extraSourceCodeFilePaths = [],
}: {
siteDir: string;
plugins: InitializedPlugin[];
extraSourceCodeFilePaths?: string[];
}): Promise<TranslationFileContent> {
const babelOptions = getBabelOptions({
isServer: true,
babelOptions: await getCustomBabelConfigFilePath(siteDir),
});

// Should we warn here if the same translation "key" is found in multiple
// source code files?
function toTranslationFileContent(
Expand Down

0 comments on commit 785f676

Please sign in to comment.