From 37d8844506d066a2bb07a5b19ccc4cfd39b6385c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Thu, 27 Mar 2025 12:22:44 +0100 Subject: [PATCH] fix(cli): fix CLI write-translation bug (#11027) * fix write-translation bug * fix write-translation bug --- .../src/__tests__/globUtils.test.ts | 14 ++++++++++ packages/docusaurus-utils/src/globUtils.ts | 26 +++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts index e65837b4f5..edfb62c9bd 100644 --- a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts @@ -9,8 +9,22 @@ import { GlobExcludeDefault, createMatcher, createAbsoluteFilePathMatcher, + isTranslatableSourceFile, } from '../globUtils'; +describe('isTranslatableSourceFile', () => { + it('works', () => { + expect(isTranslatableSourceFile('./xyz.ts')).toBe(true); + expect(isTranslatableSourceFile('./xyz.tsx')).toBe(true); + expect(isTranslatableSourceFile('./xyz.js')).toBe(true); + expect(isTranslatableSourceFile('./xyz.jsx')).toBe(true); + + expect(isTranslatableSourceFile('./xyz.md')).toBe(false); + expect(isTranslatableSourceFile('./xyz.mdx')).toBe(false); + expect(isTranslatableSourceFile('./xyz.d.ts')).toBe(false); + }); +}); + describe('createMatcher', () => { it('match default exclude MD/MDX partials correctly', () => { const matcher = createMatcher(GlobExcludeDefault); diff --git a/packages/docusaurus-utils/src/globUtils.ts b/packages/docusaurus-utils/src/globUtils.ts index c7e2cab134..ba6b558975 100644 --- a/packages/docusaurus-utils/src/globUtils.ts +++ b/packages/docusaurus-utils/src/globUtils.ts @@ -104,11 +104,8 @@ export async function safeGlobby( return Globby(globPaths, options); } -// A bit weird to put this here, but it's used by core + theme-translations -export async function globTranslatableSourceFiles( - patterns: string[], -): Promise { - // We only support extracting source code translations from these kind of files +export const isTranslatableSourceFile: (filePath: string) => boolean = (() => { + // We only support extracting source code translations from these extensions const extensionsAllowed = new Set([ '.js', '.jsx', @@ -120,8 +117,21 @@ export async function globTranslatableSourceFiles( // '.mdx', ]); + const isBlacklistedFilePath = (filePath: string) => { + // We usually extract from ts files, unless they are .d.ts files + return filePath.endsWith('.d.ts'); + }; + + return (filePath): boolean => { + const ext = path.extname(filePath); + return extensionsAllowed.has(ext) && !isBlacklistedFilePath(filePath); + }; +})(); + +// A bit weird to put this here, but it's used by core + theme-translations +export async function globTranslatableSourceFiles( + patterns: string[], +): Promise { const filePaths = await safeGlobby(patterns); - return filePaths.filter((filePath) => - extensionsAllowed.has(path.extname(filePath)), - ); + return filePaths.filter(isTranslatableSourceFile); }