-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[material-ui][mui-system] Add support for version runtime checks #43190
Conversation
Netlify deploy previewhttps://deploy-preview-43190--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
Usually something like this would be done in the compilation step. All transpilers that I know of have the ability to replace "env vars" at build time. For babel: https://babeljs.io/docs/babel-plugin-transform-inline-environment-variables i.e.
|
Thanks for pointing this out @Janpot! I refactored the code accordingly |
Looking into the test failure |
This looks great. Gonna add a few more reflections that don't have to be added in this PR. They can perfectly be addressed in a backwards compatible manner if accepted. As I understand, one use-case of this value would be feature detection by dependent libraries that have @mui/material as a peer dependency.
|
@Janpot this makes sense, I'll implement it 👌🏼 |
Edit: Nvm, I figured it out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 👍
Is there context on the why behind this change? Looking at this from the outside, I'm confused. I see we add a bit of bundle size/complexity for something that it seems we can't start to use after 2 major versions, so before some time (for example, it can't be used by MUI X v7 with Material UI v5 as it's a breaking change to import a module that doesn't exist, it can't be used with Material UI v6 as we need two majors to compare to have it useful) We faced similar problems in the past, for React, for Material UI v4 vs. v5, etc. Usually, feature detection was enough, e.g. https://github.com/mui/mui-x/pull/2108/files#diff-b4487b0ccf1b0178520560e5e9b18efbd0d4030106ae794bda6638be8cbdd526R44. Back then, we discussed making a PR close to this one, we didn't because of lack of clear visibility on a use case. But I imagine there was a different thought process? Related to mui/mui-x#14055? More importantly, let's make it so that all PRs are self-sustaining. Meaning to not depend on private conversions: Notion, Email, meeting, Slack, etc to be understood. This is important:
|
The context was that it was required for MUI X components to support Material UI v5 and v6. Sorry for not including it from the beginning, I've updated the PR's description. Besides that, this seems useful for any developers supporting more than one Material UI version. This is modeled after React's
This was backported to v5, so it can be used right away. |
Just want to add that this is not always the case. For example: the signature of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DiegoAndai Thanks for updating the PR description. I can see how people extending Material UI could benefit this in the future, I guess we will see. It's a change with a long time horizon payoff 😄
This was backported in #43233, so it can be used right away.
#43233 was released under @mui/[email protected]
. I don't think MUI X v7 can use it, it has "@mui/material": "^5.15.14",
as a peer dependency. Increasing the minimum range is a breaking change, so possible from MUI X v8.0.0. Until then it will need to fallback to the old hacky ways, e.g.: https://github.com/mui/mui-x/pull/2108/files#diff-b4487b0ccf1b0178520560e5e9b18efbd0d4030106ae794bda6638be8cbdd526R44
export const major = Number(process.env.MUI_MAJOR_VERSION); | ||
export const minor = Number(process.env.MUI_MINOR_VERSION); | ||
export const patch = Number(process.env.MUI_PATCH_VERSION); | ||
export const preReleaseLabel = process.env.MUI_PRERELEASE_LABEL || null; | ||
export const preReleaseNumber = Number(process.env.MUI_PRERELEASE_NUMBER) || null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, what if we were to only expose the version? Less bundle size:
export const major = Number(process.env.MUI_MAJOR_VERSION); | |
export const minor = Number(process.env.MUI_MINOR_VERSION); | |
export const patch = Number(process.env.MUI_PATCH_VERSION); | |
export const preReleaseLabel = process.env.MUI_PRERELEASE_LABEL || null; | |
export const preReleaseNumber = Number(process.env.MUI_PRERELEASE_NUMBER) || null; |
e.g. React https://github.com/facebook/react/blob/main/packages/shared/ReactVersion.js
or jQuery, I'm not aware they export more.
But fair enough #43190 (comment), then just
export const major = Number(process.env.MUI_MAJOR_VERSION); | |
export const minor = Number(process.env.MUI_MINOR_VERSION); | |
export const patch = Number(process.env.MUI_PATCH_VERSION); | |
export const preReleaseLabel = process.env.MUI_PRERELEASE_LABEL || null; | |
export const preReleaseNumber = Number(process.env.MUI_PRERELEASE_NUMBER) || null; | |
export const major = Number(process.env.MUI_MAJOR_VERSION); | |
export const minor = Number(process.env.MUI_MINOR_VERSION); | |
export const patch = Number(process.env.MUI_PATCH_VERSION); |
in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these five (or two) consts make much difference in bundle size. Unpacking them is useful (#43190 (comment)) and having the pre release consts keeps it consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get the value of version, major, minor, patch but I don't understand preReleaseLabel
, preReleaseNumber
.
I do believe that we should care it, bundle size is a fight of every single PR, if done systematically, it starts to add-up, and in small instances I think it sends the right signal to the community.
https://unpkg.com/browse/@mui/[email protected]/version/index.js shows:
export const version = "6.0.0";
export const major = Number("6");
export const minor = Number("0");
export const patch = Number("0");
-export const preReleaseLabel = undefined || null;
-export const preReleaseNumber = Number(undefined) || null;
export default version;
So 0.1 kB gzipped saving potential 😄? I would be curious to play https://goober.rocks/the-great-shave-off for Material UI
I love Elon's point in https://www.youtube.com/watch?v=tRsxLLghL1k&t=574s: unless you have to add back 10% of what you remove, you are not removing enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundle-size-wise I don't really mind either way. I expect these to have 0% impact on end-user bundles. These will be fully tree-shaken by minifiers if unused.
But I agree it's not clean (isNaN(Number(undefined))
...). In hindsight I'd say just a prerelease: string | undefined
export makes more sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bundle size is a fight of every single PR, if done systematically
Yes, this makes sense 👍🏼
I expect these to have 0% impact on end-user bundles. These will be fully tree-shaken by minifiers if unused.
That was my understanding as well.
So what option makes the most sense to you @oliviertassinari @Janpot?:
- Replace
preReleaseLabel
andpreReleaseNumber
withpreRelease: string | undefined
- Remove
preReleaseLabel
andpreReleaseNumber
Both could be considered breaking changes, but "not really" as those were only available in pre-release versions. If we want to be extra conservative, we can deprecate and remove them from the next major.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These will be fully tree-shaken by minifiers if unused.
@Janpot True 🙃, though one could argue that if nobody imports them then we don't need them.
So what option makes the most sense
Happy either way 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From an API point of view changing it prerelease: string | undefined
makes the most sense to me. But I don't have a strong opinion, I only see use for the major so far.
From a semver point of view it depends on how pedantic we want to be about this being a breaking change, but I think we can still kill it (edit: preReleaseLabel
and preReleaseNumber
in favor for prerelease
I mean).
though one could argue that if nobody imports them then we don't need them.
In the end we decided we'd just don't support css vars themes in toolpad when you're on v5 (which we initially did support), so we don't need this anymore at the moment. It keeps the door open for feature detection in the future (yagni?). I really have no strong opinion on this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the purpose in having this long term: third party libraries who want to implement backward support between our different majors might appreciate that we have it. We could have it in, and see if this is used long term.
I guess, I'm a bit wired like this 😁: https://youtu.be/tRsxLLghL1k?si=sRhbVRHVLRR83pMX&t=574. "if you are not adding back at least 10% of what you are removing, you know you are not removing enough", why I started this thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well essentially the 10% that was removed was the CssVarsProvider
as a separate theme provider. On toolpad side we couldn't know whether the ThemeProvider
from our peer dependency supported css vars themes or not. (In 5 it doesn't and we wanted to fall back to CssVarsProvider
in that case, as we were already doing). In the end we decided to break backwards compatibilty so the problem went away altogether. but at that time version
was already added to @mui/material. I'm totally fine removing it again. But we saw a clear need at some point that is not unthinkable to return in v7.
Essentially we took (the proverbial) 10% out and had to 0.1% back in. It's even better 😄
Add a version const export that matches the current version for runtime checks on package consumers. This version is added in compilation time (at build). I only added this const to released stable packages Material and System, but let me know if I should add it for any others.
Use case: For MUI X and Toolpad to be able to support multiple versions of Material UI.
Example usage: https://codesandbox.io/p/sandbox/43190-version-exports-example-q9fkt6?file=%2Fsrc%2FApp.tsx%3A9%2C1