Skip to content

Commit 90a8ca3

Browse files
authored
chore: prevent importing theme components with relative paths (facebook#7674)
1 parent 2c7012f commit 90a8ca3

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

.eslintrc.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ const OFF = 0;
99
const WARNING = 1;
1010
const ERROR = 2;
1111

12+
const ClientRestrictedImportPatterns = [
13+
// Prevent importing lodash in client bundle for bundle size
14+
'lodash',
15+
'lodash.**',
16+
'lodash/**',
17+
// Prevent importing server code in client bundle
18+
'**/../babel/**',
19+
'**/../server/**',
20+
'**/../commands/**',
21+
'**/../webpack/**',
22+
];
23+
1224
module.exports = {
1325
root: true,
1426
env: {
@@ -371,25 +383,32 @@ module.exports = {
371383
},
372384
overrides: [
373385
{
374-
files: [
375-
'packages/docusaurus-*/src/theme/**/*.{js,ts,tsx}',
376-
'packages/docusaurus/src/client/**/*.{js,ts,tsx}',
377-
],
386+
files: ['packages/docusaurus/src/client/**/*.{js,ts,tsx}'],
387+
rules: {
388+
'no-restricted-imports': [
389+
'error',
390+
{
391+
patterns: ClientRestrictedImportPatterns,
392+
},
393+
],
394+
},
395+
},
396+
{
397+
files: ['packages/docusaurus-*/src/theme/**/*.{js,ts,tsx}'],
398+
excludedFiles: '*.test.{js,ts,tsx}',
378399
rules: {
379400
'no-restricted-imports': [
380401
'error',
381402
{
382-
patterns: [
383-
// Prevent importing lodash in client bundle for bundle size
384-
'lodash',
385-
'lodash.**',
386-
'lodash/**',
387-
// Prevent importing server code in client bundle
388-
'**/../babel/**',
389-
'**/../server/**',
390-
'**/../commands/**',
391-
'**/../webpack/**',
392-
],
403+
patterns: ClientRestrictedImportPatterns.concat(
404+
// Prevents relative imports between React theme components
405+
[
406+
'../**',
407+
'./**',
408+
// Allows relative styles module import with consistent filename
409+
'!./styles.module.css',
410+
],
411+
),
393412
},
394413
],
395414
},

packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import BlogLayout from '@theme/BlogLayout';
1717
import TagsListByLetter from '@theme/TagsListByLetter';
1818
import type {Props} from '@theme/BlogTagsListPage';
19-
import SearchMetadata from '../SearchMetadata';
19+
import SearchMetadata from '@theme/SearchMetadata';
2020

2121
export default function BlogTagsListPage({tags, sidebar}: Props): JSX.Element {
2222
const title = translateTagsPageTitle();

packages/docusaurus-theme-classic/src/theme/Layout/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import Navbar from '@theme/Navbar';
1616
import Footer from '@theme/Footer';
1717
import LayoutProviders from '@theme/LayoutProviders';
1818
import ErrorPageContent from '@theme/ErrorPageContent';
19-
import './styles.css';
2019
import type {Props} from '@theme/Layout';
20+
import styles from './styles.module.css';
2121

2222
export default function Layout(props: Props): JSX.Element {
2323
const {
@@ -41,7 +41,12 @@ export default function Layout(props: Props): JSX.Element {
4141

4242
<Navbar />
4343

44-
<div className={clsx(ThemeClassNames.wrapper.main, wrapperClassName)}>
44+
<div
45+
className={clsx(
46+
ThemeClassNames.wrapper.main,
47+
styles.mainWrapper,
48+
wrapperClassName,
49+
)}>
4550
<ErrorBoundary fallback={(params) => <ErrorPageContent {...params} />}>
4651
{children}
4752
</ErrorBoundary>

packages/docusaurus-theme-classic/src/theme/Layout/styles.css renamed to packages/docusaurus-theme-classic/src/theme/Layout/styles.module.css

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
html,
9-
body {
8+
:global(html, body) {
109
height: 100%;
1110
}
1211

13-
#__docusaurus {
14-
min-height: 100%;
15-
display: flex;
16-
flex-direction: column;
17-
}
18-
19-
.main-wrapper {
12+
.mainWrapper {
2013
flex: 1 0 auto;
2114
}
2215

23-
/* Docusaurus-specific utility classes */
24-
25-
.docusaurus-mt-lg {
16+
/* Docusaurus-specific utility class */
17+
:global(.docusaurus-mt-lg) {
2618
margin-top: 3rem;
2719
}
20+
21+
:global(#__docusaurus) {
22+
min-height: 100%;
23+
display: flex;
24+
flex-direction: column;
25+
}

0 commit comments

Comments
 (0)