Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ function filterUnwantedRedirects(
): RedirectItem[] {
// We don't want to create the same redirect twice, since that would lead to
// writing the same html redirection file twice.
Object.entries(_.groupBy(redirects, (redirect) => redirect.from)).forEach(
([from, groupedFromRedirects]) => {
Map.groupBy(redirects, (redirect) => redirect.from).forEach(
(groupedFromRedirects, from) => {
if (groupedFromRedirects.length > 1) {
logger.report(
pluginContext.siteConfig.onDuplicateRoutes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const BreadcrumbSeparator = '/';

// Just an alias to the make code more explicit
function getLocalDocId(docId: string): string {
return _.last(docId.split('/'))!;
return docId.split('/').at(-1)!;
}

export const CategoryMetadataFilenamePattern = '_category_.{json,yml,yaml}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ export function createSidebarsUtils(sidebars: Sidebars): SidebarsUtils {
allDocIds: string[];
sidebarFilePath: string;
}) {
const allSidebarDocIds = Object.values(sidebarNameToDocIds).flat();
const invalidDocIds = _.difference(allSidebarDocIds, allDocIds);
const allSidebarDocIds = new Set(Object.values(sidebarNameToDocIds).flat());
const existingDocIds = new Set(allDocIds);
const invalidDocIds = [...allSidebarDocIds.difference(existingDocIds)];

if (invalidDocIds.length > 0) {
throw new Error(
Expand All @@ -319,7 +320,7 @@ These sidebar document ids do not exist:
- ${invalidDocIds.sort().join('\n- ')}

Available document ids are:
- ${_.uniq(allDocIds).sort().join('\n- ')}
- ${[...existingDocIds].sort().join('\n- ')}
`,
);
}
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-plugin-content-docs/src/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ type TranslationMessageEntry = [string, TranslationMessage];
function ensureNoSidebarDuplicateEntries(
translationEntries: TranslationMessageEntry[],
): void {
const grouped = _.groupBy(translationEntries, (entry) => entry[0]);
const duplicates = Object.entries(grouped).filter(
(entry) => entry[1].length > 1,
);
const grouped = Map.groupBy(translationEntries, (entry) => entry[0]);
const duplicates = [...grouped].filter((entry) => entry[1].length > 1);

if (duplicates.length > 0) {
throw new Error(`Multiple docs sidebar items produce the same translation key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import _ from 'lodash';
import type {VersionsOptions} from '@docusaurus/plugin-content-docs';

export function validateVersionName(name: unknown): asserts name is string {
Expand Down Expand Up @@ -74,10 +73,10 @@ export function validateVersionsOptions(
`Docs option lastVersion: ${options.lastVersion} is invalid. ${availableVersionNamesMsg}`,
);
}
const unknownVersionConfigNames = _.difference(
Object.keys(options.versions),
availableVersionNames,
);
const availableVersions = new Set(availableVersionNames);
const unknownVersionConfigNames = [
...new Set(Object.keys(options.versions)).difference(availableVersions),
];
if (unknownVersionConfigNames.length > 0) {
throw new Error(
`Invalid docs option "versions": unknown versions (${unknownVersionConfigNames.join(
Expand All @@ -92,10 +91,9 @@ export function validateVersionsOptions(
`Invalid docs option "onlyIncludeVersions": an empty array is not allowed, at least one version is needed.`,
);
}
const unknownOnlyIncludeVersionNames = _.difference(
options.onlyIncludeVersions,
availableVersionNames,
);
const unknownOnlyIncludeVersionNames = [
...new Set(options.onlyIncludeVersions).difference(availableVersions),
];
if (unknownOnlyIncludeVersionNames.length > 0) {
throw new Error(
`Invalid docs option "onlyIncludeVersions": unknown versions (${unknownOnlyIncludeVersionNames.join(
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/src/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function groupTaggedItems<Item>(
// If user add twice the same tag to a md doc (weird but possible),
// we don't want the item to appear twice in the list...
Object.values(result).forEach((group) => {
group.items = _.uniq(group.items);
group.items = [...new Set(group.items)];
});

return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/swizzle/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function wrap({
const isDirectory = await isDir(path.join(themePath, themeComponentName));

// Top/Parent/ComponentName => ComponentName
const componentName = _.last(themeComponentName.split('/'))!;
const componentName = themeComponentName.split('/').at(-1)!;
const wrapperComponentName = `${componentName}Wrapper`;

const wrapperFileName = `${themeComponentName}${isDirectory ? '/index' : ''}${
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/commands/swizzle/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ export function getMissingIntermediateComponentFolderNames(
return _.range(1, paths.length + 1).map((i) => paths.slice(0, i).join('/'));
}

const expandedComponentNames = _.uniq(
const expandedComponentNames = new Set(
componentNames.flatMap((componentName) =>
getAllIntermediatePaths(componentName),
),
);

return _.difference(expandedComponentNames, componentNames);
return [...expandedComponentNames.difference(new Set(componentNames))];
}

const skipReadDirNames = ['__test__', '__tests__', '__mocks__', '__fixtures__'];
Expand Down
16 changes: 9 additions & 7 deletions packages/docusaurus/src/commands/swizzle/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ export function getPluginByThemeName(
}

export function getThemeNames(plugins: SwizzlePlugin[]): string[] {
const themeNames = _.uniq(
// The fact that getThemePath is attached to the plugin instance makes
// this code impossible to optimize. If this is a static method, we don't
// need to initialize all plugins just to filter which are themes
// Benchmark: loadContext-58ms; initPlugins-323ms
plugins.map((plugin) => pluginToThemeName(plugin)).filter(Boolean),
) as string[];
const themeNames = [
...new Set(
// The fact that getThemePath is attached to the plugin instance makes
// this code impossible to optimize. If this is a static method, we don't
// need to initialize all plugins just to filter which are themes
// Benchmark: loadContext-58ms; initPlugins-323ms
plugins.map((plugin) => pluginToThemeName(plugin)).filter(Boolean),
),
] as string[];

// Opinionated ordering: user is most likely to swizzle:
// - the classic theme
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus/src/server/codegen/codegenRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,9 @@ function ensureNoPropsConflict(route: RouteConfig) {
if (!route.props && !route.modules) {
return;
}
const conflictingPropNames = _.intersection(
Object.keys(route.props ?? {}),
Object.keys(route.modules ?? {}),
);
const propNames = new Set(Object.keys(route.props ?? {}));
const moduleNames = new Set(Object.keys(route.modules ?? {}));
const conflictingPropNames = [...propNames.intersection(moduleNames)];
if (conflictingPropNames.length > 0) {
throw new Error(
`Route ${
Expand Down
35 changes: 16 additions & 19 deletions packages/docusaurus/src/server/plugins/pluginIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import _ from 'lodash';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import type {InitializedPlugin} from '@docusaurus/types';

Expand All @@ -16,26 +15,24 @@ import type {InitializedPlugin} from '@docusaurus/types';
export function ensureUniquePluginInstanceIds(
plugins: InitializedPlugin[],
): void {
const pluginsByName = _.groupBy(plugins, (p) => p.name);
Object.entries(pluginsByName).forEach(([pluginName, pluginInstances]) => {
const pluginInstancesById = _.groupBy(
const pluginsByName = Map.groupBy(plugins, (p) => p.name);
pluginsByName.forEach((pluginInstances, pluginName) => {
const pluginInstancesById = Map.groupBy(
pluginInstances,
(p) => p.options.id ?? DEFAULT_PLUGIN_ID,
);
Object.entries(pluginInstancesById).forEach(
([pluginId, pluginInstancesWithId]) => {
if (pluginInstancesWithId.length !== 1) {
throw new Error(
`Plugin "${pluginName}" is used ${
pluginInstancesWithId.length
} times with ID "${pluginId}".\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique ID to each plugin instance.${
pluginId === DEFAULT_PLUGIN_ID
? `\n\nThe plugin ID is "${DEFAULT_PLUGIN_ID}" by default. It's possible that the preset you are using already includes a plugin instance, in which case you either want to disable the plugin in the preset (to use a single instance), or assign another ID to your extra plugin instance (to use multiple instances).`
: ''
}`,
);
}
},
);
pluginInstancesById.forEach((pluginInstancesWithId, pluginId) => {
if (pluginInstancesWithId.length !== 1) {
throw new Error(
`Plugin "${pluginName}" is used ${
pluginInstancesWithId.length
} times with ID "${pluginId}".\nTo use the same plugin multiple times on a Docusaurus site, you need to assign a unique ID to each plugin instance.${
pluginId === DEFAULT_PLUGIN_ID
? `\n\nThe plugin ID is "${DEFAULT_PLUGIN_ID}" by default. It's possible that the preset you are using already includes a plugin instance, in which case you either want to disable the plugin in the preset (to use a single instance), or assign another ID to your extra plugin instance (to use multiple instances).`
: ''
}`,
);
}
});
});
}
16 changes: 8 additions & 8 deletions packages/docusaurus/src/server/translations/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ async function writeTranslationFileContent({
const existingContent = await readTranslationFileContent(filePath);

// Warn about potential legacy keys
const unknownKeys = _.difference(
Object.keys(existingContent ?? {}),
Object.keys(newContent),
);
const existingKeys = new Set(Object.keys(existingContent ?? {}));
const newKeys = new Set(Object.keys(newContent));
const unknownKeys = [...existingKeys.difference(newKeys)];
if (unknownKeys.length > 0) {
logger.warn`Some translation keys looks unknown to us in file path=${filePath}.
Maybe you should remove them? ${unknownKeys}`;
Expand Down Expand Up @@ -276,10 +275,11 @@ export function applyDefaultCodeTranslations({
extractedCodeTranslations: TranslationFileContent;
defaultCodeMessages: CodeTranslations;
}): TranslationFileContent {
const unusedDefaultCodeMessages = _.difference(
Object.keys(defaultCodeMessages),
Object.keys(extractedCodeTranslations),
);
const defaultMessageIds = new Set(Object.keys(defaultCodeMessages));
const extractedMessageIds = new Set(Object.keys(extractedCodeTranslations));
const unusedDefaultCodeMessages = [
...defaultMessageIds.difference(extractedMessageIds),
];
if (unusedDefaultCodeMessages.length > 0) {
logger.warn`Unused default message codes found.
Please report this Docusaurus issue. name=${unusedDefaultCodeMessages}`;
Expand Down
Loading