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

refactor: create @docusaurus/bundler and @docusaurus/babel packages #10511

Merged
merged 25 commits into from
Sep 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4a145bc
upgrade webpackbar to v6
slorber Sep 19, 2024
b4e71c1
improve dynamic bundler architecture
slorber Sep 19, 2024
bd00c5d
cleanup ContextReplacementPlugin import mess
slorber Sep 19, 2024
45c1126
extract @docusaurus/bundler package
slorber Sep 19, 2024
db5d4ce
revert copy-webpack-plugin version
slorber Sep 19, 2024
b7584cc
package.json typo
slorber Sep 19, 2024
bb48dd8
add tslib
slorber Sep 19, 2024
8dac80f
move minimizers to bundler package
slorber Sep 19, 2024
a3a33d9
move compiler to bundler package
slorber Sep 19, 2024
5d8339b
move clean-css deps
slorber Sep 20, 2024
a386846
move js loader to bundler pkg
slorber Sep 20, 2024
818d835
move style loader and dependencies to bundler pkg
slorber Sep 20, 2024
7fc4c7c
preset comment
slorber Sep 20, 2024
4940034
fix tests
slorber Sep 20, 2024
92ef99d
fix tests
slorber Sep 20, 2024
03a5649
create docusaurus-babel package
slorber Sep 20, 2024
1589f9d
move utils to docusaurus-babel
slorber Sep 20, 2024
785f676
cleanup signature of extractSiteSourceCodeTranslations
slorber Sep 20, 2024
e2fe8dd
migrate translation extraction code to babel package
slorber Sep 20, 2024
f11d38b
fix import typo
slorber Sep 20, 2024
4e4dcaa
move babel-loader dependency for pnp
slorber Sep 20, 2024
c10a955
re-add babel-core deps
slorber Sep 20, 2024
715b700
extract globTranslatableSourceFiles util
slorber Sep 20, 2024
766176b
Use @docusaurus/babel/preset for newly init sites + docs
slorber Sep 20, 2024
3ca28be
add babel pkg to init templates
slorber Sep 20, 2024
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
Prev Previous commit
Next Next commit
extract globTranslatableSourceFiles util
  • Loading branch information
slorber committed Sep 20, 2024
commit 715b70032d69ba56dbb3d87ed7b9ed2e36f027f3
1 change: 1 addition & 0 deletions packages/docusaurus-theme-translations/package.json
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
"@docusaurus/babel": "3.5.2",
"@docusaurus/core": "3.5.2",
"@docusaurus/logger": "3.5.2",
"@docusaurus/utils": "3.5.2",
"lodash": "^4.17.21"
},
"engines": {
5 changes: 2 additions & 3 deletions packages/docusaurus-theme-translations/src/utils.ts
Original file line number Diff line number Diff line change
@@ -13,8 +13,7 @@

import path from 'path';
import fs from 'fs-extra';
// Unsafe import, should we create a package for the translationsExtractor ?;
import {globSourceCodeFilePaths} from '@docusaurus/core/lib/server/translations/translationsExtractor';
import {globTranslatableSourceFiles} from '@docusaurus/utils';
import {extractAllSourceCodeFileTranslations} from '@docusaurus/babel';
import type {TranslationFileContent} from '@docusaurus/types';

@@ -60,7 +59,7 @@ export async function extractThemeCodeMessages(
// eslint-disable-next-line no-param-reassign
targetDirs ??= (await getThemes()).flatMap((theme) => theme.src);

const filePaths = (await globSourceCodeFilePaths(targetDirs)).filter(
const filePaths = (await globTranslatableSourceFiles(targetDirs)).filter(
(filePath) => ['.js', '.jsx'].includes(path.extname(filePath)),
);

22 changes: 22 additions & 0 deletions packages/docusaurus-utils/src/globUtils.ts
Original file line number Diff line number Diff line change
@@ -103,3 +103,25 @@ 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<string[]> {
// We only support extracting source code translations from these kind of files
const extensionsAllowed = new Set([
'.js',
'.jsx',
'.ts',
'.tsx',
// TODO support md/mdx too? (may be overkill)
// need to compile the MDX to JSX first and remove front matter
// '.md',
// '.mdx',
]);

const filePaths = await safeGlobby(patterns);
return filePaths.filter((filePath) =>
extensionsAllowed.has(path.extname(filePath)),
);
}
1 change: 1 addition & 0 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@ export {
Globby,
GlobExcludeDefault,
safeGlobby,
globTranslatableSourceFiles,
createMatcher,
createAbsoluteFilePathMatcher,
} from './globUtils';
8 changes: 3 additions & 5 deletions packages/docusaurus/src/commands/writeTranslations.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

import fs from 'fs-extra';
import path from 'path';
import {globTranslatableSourceFiles} from '@docusaurus/utils';
import {loadContext, type LoadContextParams} from '../server/site';
import {initPlugins} from '../server/plugins/init';
import {
@@ -16,10 +17,7 @@ import {
loadPluginsDefaultCodeTranslationMessages,
applyDefaultCodeTranslations,
} from '../server/translations/translations';
import {
extractSiteSourceCodeTranslations,
globSourceCodeFilePaths,
} from '../server/translations/translationsExtractor';
import {extractSiteSourceCodeTranslations} from '../server/translations/translationsExtractor';
import type {InitializedPlugin} from '@docusaurus/types';

export type WriteTranslationsCLIOptions = Pick<
@@ -48,7 +46,7 @@ async function getExtraSourceCodeFilePaths(): Promise<string[]> {
if (!themeCommonLibDir) {
return []; // User may not use a Docusaurus official theme? Quite unlikely...
}
return globSourceCodeFilePaths([themeCommonLibDir]);
return globTranslatableSourceFiles([themeCommonLibDir]);
}

async function writePluginTranslationFiles({
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

import nodePath from 'path';
import logger from '@docusaurus/logger';
import {safeGlobby, SRC_DIR_NAME} from '@docusaurus/utils';
import {globTranslatableSourceFiles, SRC_DIR_NAME} from '@docusaurus/utils';
import {
getBabelOptions,
getCustomBabelConfigFilePath,
@@ -18,21 +18,6 @@ import type {
TranslationFileContent,
} from '@docusaurus/types';

// We only support extracting source code translations from these kind of files
const TranslatableSourceCodeExtension = new Set([
'.js',
'.jsx',
'.ts',
'.tsx',
// TODO support md/mdx too? (may be overkill)
// need to compile the MDX to JSX first and remove front matter
// '.md',
// '.mdx',
]);
function isTranslatableSourceCodePath(filePath: string): boolean {
return TranslatableSourceCodeExtension.has(nodePath.extname(filePath));
}

function getSiteSourceCodeFilePaths(siteDir: string): string[] {
return [nodePath.join(siteDir, SRC_DIR_NAME)];
}
@@ -53,13 +38,6 @@ function getPluginSourceCodeFilePaths(plugin: InitializedPlugin): string[] {
return codePaths.map((p) => nodePath.resolve(plugin.path, p));
}

export async function globSourceCodeFilePaths(
dirPaths: string[],
): Promise<string[]> {
const filePaths = await safeGlobby(dirPaths);
return filePaths.filter(isTranslatableSourceCodePath);
}

async function getSourceCodeFilePaths(
siteDir: string,
plugins: InitializedPlugin[],
@@ -74,7 +52,7 @@ async function getSourceCodeFilePaths(

const allPaths = [...sitePaths, ...pluginsPaths];

return globSourceCodeFilePaths(allPaths);
return globTranslatableSourceFiles(allPaths);
}

export async function extractSiteSourceCodeTranslations({