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

chore: prevent importing theme components with relative paths #7674

Merged
merged 2 commits into from Jun 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 34 additions & 15 deletions .eslintrc.js
Expand Up @@ -9,6 +9,18 @@ const OFF = 0;
const WARNING = 1;
const ERROR = 2;

const ClientRestrictedImportPatterns = [
// Prevent importing lodash in client bundle for bundle size
'lodash',
'lodash.**',
'lodash/**',
// Prevent importing server code in client bundle
'**/../babel/**',
'**/../server/**',
'**/../commands/**',
'**/../webpack/**',
];

module.exports = {
root: true,
env: {
Expand Down Expand Up @@ -371,25 +383,32 @@ module.exports = {
},
overrides: [
{
files: [
'packages/docusaurus-*/src/theme/**/*.{js,ts,tsx}',
'packages/docusaurus/src/client/**/*.{js,ts,tsx}',
],
files: ['packages/docusaurus/src/client/**/*.{js,ts,tsx}'],
rules: {
'no-restricted-imports': [
'error',
{
patterns: ClientRestrictedImportPatterns,
},
],
},
},
{
files: ['packages/docusaurus-*/src/theme/**/*.{js,ts,tsx}'],
excludedFiles: '*.test.{js,ts,tsx}',
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
// Prevent importing lodash in client bundle for bundle size
'lodash',
'lodash.**',
'lodash/**',
// Prevent importing server code in client bundle
'**/../babel/**',
'**/../server/**',
'**/../commands/**',
'**/../webpack/**',
],
patterns: ClientRestrictedImportPatterns.concat(
// Prevents relative imports between React theme components
[
'../**',
'./**',
// Allows relative styles module import with consistent filename
'!./styles.module.css',
],
),
},
],
},
Expand Down
Expand Up @@ -16,7 +16,7 @@ import {
import BlogLayout from '@theme/BlogLayout';
import TagsListByLetter from '@theme/TagsListByLetter';
import type {Props} from '@theme/BlogTagsListPage';
import SearchMetadata from '../SearchMetadata';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

now catched with error

/Users/sebastienlorber/Desktop/projects/docusaurus/packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx
19:1 error '../SearchMetadata' import is restricted from being used by a pattern no-restricted-imports

import SearchMetadata from '@theme/SearchMetadata';

export default function BlogTagsListPage({tags, sidebar}: Props): JSX.Element {
const title = translateTagsPageTitle();
Expand Down
Expand Up @@ -16,8 +16,8 @@ import Navbar from '@theme/Navbar';
import Footer from '@theme/Footer';
import LayoutProviders from '@theme/LayoutProviders';
import ErrorPageContent from '@theme/ErrorPageContent';
import './styles.css';
import type {Props} from '@theme/Layout';
import styles from './styles.module.css';

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

<Navbar />

<div className={clsx(ThemeClassNames.wrapper.main, wrapperClassName)}>
<div
className={clsx(
ThemeClassNames.wrapper.main,
styles.mainWrapper,
wrapperClassName,
)}>
<ErrorBoundary fallback={(params) => <ErrorPageContent {...params} />}>
{children}
</ErrorBoundary>
Expand Down
Expand Up @@ -5,23 +5,21 @@
* LICENSE file in the root directory of this source tree.
*/

html,
body {
:global(html, body) {
height: 100%;
}

#__docusaurus {
min-height: 100%;
display: flex;
flex-direction: column;
}

.main-wrapper {
.mainWrapper {
flex: 1 0 auto;
}

/* Docusaurus-specific utility classes */

.docusaurus-mt-lg {
/* Docusaurus-specific utility class */
:global(.docusaurus-mt-lg) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not really liking this solitary utility class 🤪 maybe we should just duplicate the rule everywhere 🤷‍♂️

margin-top: 3rem;
}

:global(#__docusaurus) {
min-height: 100%;
display: flex;
flex-direction: column;
}