Skip to content
Draft
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 @@ -43,6 +43,7 @@ export function resolveSidebarPathOption(
async function readCategoriesMetadata(contentPath: string) {
const categoryFiles = await Globby('**/_category_.{json,yml,yaml}', {
cwd: contentPath,
ignore: ['**/node_modules/**'],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment here: #12129 (comment)

This will make another legit use-case impossible.

We'd like to optimize, without preventing the content path being node_modules/@myCompany/docs. I'd like to see a test covering this edge case that keeps passing after the performance optimization.

});
const categoryToFile = _.groupBy(categoryFiles, path.dirname);
return combinePromises(
Expand Down
42 changes: 42 additions & 0 deletions packages/docusaurus-utils/src/__tests__/globUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ describe('isTranslatableSourceFile', () => {
});

describe('createMatcher', () => {
it('match default exclude node_modules correctly', () => {
const matcher = createMatcher(GlobExcludeDefault);
expect(matcher('node_modules/pkg/doc.md')).toBe(true);
expect(matcher('node_modules/pkg/index.js')).toBe(true);
expect(matcher('category/node_modules/pkg/doc.md')).toBe(true);
expect(matcher('category/node_modules/@scope/pkg/doc.md')).toBe(true);
// When contentPath is inside node_modules, relative paths of content
// files do NOT contain node_modules, so they should NOT be excluded.
expect(matcher('intro.md')).toBe(false);
expect(matcher('guide/setup.mdx')).toBe(false);
});

it('match default exclude MD/MDX partials correctly', () => {
const matcher = createMatcher(GlobExcludeDefault);
expect(matcher('doc.md')).toBe(false);
Expand Down Expand Up @@ -115,6 +127,36 @@ describe('createAbsoluteFilePathMatcher', () => {
expect(matcher('/root/_docs/_category/myDoc.mdx')).toBe(true);
});

it('match default exclude node_modules correctly', () => {
expect(matcher('/_root/docs/node_modules/pkg/doc.md')).toBe(true);
expect(matcher('/_root/docs/node_modules/@scope/pkg/doc.md')).toBe(true);
expect(
matcher('/_root/docs/category/node_modules/pkg/index.js'),
).toBe(true);
});

// Ensures that when contentPath is inside node_modules (e.g.,
// node_modules/@myCompany/docs), content files at that root are NOT
// excluded, because their relative paths don't contain node_modules.
it('does not exclude content when root folder is inside node_modules', () => {
const nmMatcher = createAbsoluteFilePathMatcher(GlobExcludeDefault, [
'/project/node_modules/@myCompany/docs',
]);
// Content at the root should NOT be excluded
expect(
nmMatcher('/project/node_modules/@myCompany/docs/intro.md'),
).toBe(false);
expect(
nmMatcher('/project/node_modules/@myCompany/docs/guide/setup.mdx'),
).toBe(false);
// But nested node_modules inside that root SHOULD be excluded
expect(
nmMatcher(
'/project/node_modules/@myCompany/docs/node_modules/dep/file.md',
),
).toBe(true);
});

it('match default exclude tests correctly', () => {
expect(matcher('/__test__/website/src/xyz.js')).toBe(false);
expect(matcher('/__test__/website/src/__test__/xyz.js')).toBe(true);
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-utils/src/globUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const Globby = Tinyglobby.glob;
* - Ignore tests
*/
export const GlobExcludeDefault = [
'**/node_modules/**',
'**/_*.{js,jsx,ts,tsx,md,mdx}',
'**/_*/**',
'**/*.test.{js,jsx,ts,tsx}',
Expand Down
Loading