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: mark a few client-side packages as side-effect-free #7085

Merged
merged 10 commits into from
Apr 9, 2022

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Apr 1, 2022

Motivation

Instant bundle size optimization⚡️

I did not mark core as side-effect-free, though, because there are a few side-effects there (mostly importing non-module CSS), and it's quite tedious trying to filter out everything.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Page builds fine

@Josh-Cena Josh-Cena added the pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient. label Apr 1, 2022
@Josh-Cena Josh-Cena requested review from slorber and lex111 as code owners April 1, 2022 05:43
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 1, 2022
@netlify
Copy link

netlify bot commented Apr 1, 2022

[V2]

Name Link
🔨 Latest commit 621000a
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/625044f013e07a000ab28028
😎 Deploy Preview https://deploy-preview-7085--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 60
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

Lighthouse ran on https://deploy-preview-7085--docusaurus-2.netlify.app/

@github-actions
Copy link

github-actions bot commented Apr 1, 2022

Size Change: -5.92 kB (-1%)

Total Size: 797 kB

Filename Size Change
website/build/assets/css/styles.********.css 105 kB +279 B (0%)
website/build/assets/js/main.********.js 604 kB -6.25 kB (-1%)
website/build/index.html 38.6 kB +57 B (0%)
ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 49.9 kB

compressed-size-action

@Josh-Cena
Copy link
Collaborator Author

The most prominent issue is CSS. As soon as a component imports CSS, to ensure stable CSS insertion order (#3678), we have to mark all downstream consumers as side-effect-ful. This makes us not able to actually fully tree-shake.

@@ -9,6 +9,8 @@ import React from 'react';
import clsx from 'clsx';
import {Details as DetailsGeneric} from '@docusaurus/theme-common';
import type {Props} from '@theme/Details';
// Ensure that the default details style is properly overridden
import '@docusaurus/theme-common/lib/components/Details/styles.module.css';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We unfortunately have to do this, to make theme-common tree-shakeable at all. Maybe we should have different exports fields for all side-effect-ful exports (like @docusaurus/theme-common/Details), or simply declare this as the official way to acquire default styles for Details?

Copy link
Collaborator

Choose a reason for hiding this comment

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

🤷‍♂️ if we do this maybe we should remove the css module import in theme common?

Note useKeyboardNavigation also imports css

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aye, but that one's not impacted by CSS insertion order, so as long as it is loaded (which, well, means useKeyboardNavigation.ts is used), it works properly.

My idea is that we should not export Details from src/index.tsx, but create a separate exports entry point instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I understand is that the CSS order problem is related to theme/Details vs theme-common/Details right?

This kind of problem is going to be solved soon once CSS cascade layers support improves (already quite good https://caniuse.com/css-cascade-layers)

@layer infima, theme-common, theme, site;

In the meantime maybe we could just add !important to the 4 rules that override theme-common?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mmm, you can see I obviously don't like !important because that makes site customizations harder, if not impossible...

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes agree, your solution is probably a better temporary workaround

@Josh-Cena Josh-Cena added the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Apr 3, 2022
@Josh-Cena Josh-Cena requested a review from slorber April 8, 2022 14:28
@Josh-Cena
Copy link
Collaborator Author

This looks much better IMO, no more hacks...

My idea is that Details should be a semi-public, if not fully public, API, so we should probably have a better pattern

@Josh-Cena Josh-Cena merged commit e5bf59f into main Apr 9, 2022
@Josh-Cena Josh-Cena deleted the jc/side-effects branch April 9, 2022 01:08
@Josh-Cena Josh-Cena removed the status: awaiting review This PR is ready for review, will be merged after maintainers' approval label Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants