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

[material-ui][mui-system] Add support for version runtime checks #43190

Merged
merged 6 commits into from
Aug 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ module.exports = function getBabelConfig(api) {
mode: 'unsafe-wrap',
},
],
[
'transform-inline-environment-variables',
{
include: [
'MUI_VERSION',
'MUI_MAJOR_VERSION',
'MUI_MINOR_VERSION',
'MUI_PATCH_VERSION',
'MUI_PRERELEASE_LABEL',
'MUI_PRERELEASE_NUMBER',
],
},
],
];

if (process.env.NODE_ENV === 'production') {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"babel-plugin-module-resolver": "^5.0.2",
"babel-plugin-optimize-clsx": "^2.6.2",
"babel-plugin-react-remove-properties": "^0.3.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"chalk": "^5.3.0",
"compression-webpack-plugin": "^11.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-material/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ export * from './useAutocomplete';
export { default as GlobalStyles } from './GlobalStyles';
export * from './GlobalStyles';

export * from './version';

/**
* @deprecated will be removed in v5.beta, please use StyledEngineProvider from @mui/material/styles instead
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-material/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,5 @@ export * from './generateUtilityClass';
export { default as generateUtilityClasses } from './generateUtilityClasses';

export { default as Unstable_TrapFocus } from './Unstable_TrapFocus';

export * from './version';
15 changes: 12 additions & 3 deletions packages/mui-material/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
import { expect } from 'chai';
import * as MaterialUI from './index';

const versionExports = [
'version',
'major',
'minor',
'patch',
'preReleaseLabel',
'preReleaseNumber',
];

describe('material-ui', () => {
it('should have exports', () => {
expect(typeof MaterialUI).to.equal('object');
});

it('should not have undefined exports', () => {
Object.keys(MaterialUI).forEach((exportKey) =>
expect(Boolean(MaterialUI[exportKey])).to.equal(true),
);
Object.keys(MaterialUI)
.filter((exportKey) => !versionExports.includes(exportKey))
.forEach((exportKey) => expect(Boolean(MaterialUI[exportKey])).to.equal(true));
});

it('should reexport certain members from @mui/base', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/mui-material/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const version = process.env.MUI_VERSION;
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;
Comment on lines +2 to +6
Copy link
Member

@oliviertassinari oliviertassinari Aug 26, 2024

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:

Suggested change
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

SCR-20240826-ujde

or jQuery, I'm not aware they export more.

SCR-20240826-uibg

But fair enough #43190 (comment), then just

Suggested change
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?

Copy link
Member Author

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.

Copy link
Member

@oliviertassinari oliviertassinari Aug 28, 2024

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/material@6.0.0/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.

Copy link
Member

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.

Copy link
Member Author

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?:

  1. Replace preReleaseLabel and preReleaseNumber with preRelease: string | undefined
  2. Remove preReleaseLabel and preReleaseNumber

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.

Copy link
Member

@oliviertassinari oliviertassinari Aug 30, 2024

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 👍

Copy link
Member

@Janpot Janpot Sep 2, 2024

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.

Copy link
Member

@oliviertassinari oliviertassinari Sep 2, 2024

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.

Copy link
Member

@Janpot Janpot Sep 2, 2024

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 😄


export default version;
2 changes: 2 additions & 0 deletions packages/mui-system/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,5 @@ export * from './Grid';

export { default as Stack } from './Stack';
export * from './Stack';

export * from './version';
1 change: 1 addition & 0 deletions packages/mui-system/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export { default as unstable_createCssVarsTheme } from './cssVars/createCssVarsT
export { default as responsivePropType } from './responsivePropType';
export { default as RtlProvider } from './RtlProvider';
export * from './RtlProvider';
export * from './version';

/** ----------------- */
/** Layout components */
Expand Down
8 changes: 8 additions & 0 deletions packages/mui-system/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const version = process.env.MUI_VERSION;
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 default version;
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import glob from 'fast-glob';
import path from 'path';
import { promisify } from 'util';
import yargs from 'yargs';
import { getWorkspaceRoot } from './utils.mjs';
import { getVersionEnvVariables, getWorkspaceRoot } from './utils.mjs';

const exec = promisify(childProcess.exec);

Expand All @@ -29,7 +29,9 @@ async function run(argv) {
NODE_ENV: 'production',
BABEL_ENV: bundle,
MUI_BUILD_VERBOSE: verbose,
...(await getVersionEnvVariables()),
};

const babelConfigPath = path.resolve(getWorkspaceRoot(), 'babel.config.js');
const srcDir = path.resolve('./src');
const extensions = ['.js', '.ts', '.tsx'];
Expand Down
30 changes: 30 additions & 0 deletions scripts/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import url from 'url';
import fse from 'fs-extra';

/**
* Returns the full path of the root directory of this repository.
Expand All @@ -10,3 +11,32 @@ export function getWorkspaceRoot() {
const workspaceRoot = path.resolve(currentDirectory, '..');
return workspaceRoot;
}

/**
* Returns the version and destructured values of the version as env variables to be replaced.
*/
export async function getVersionEnvVariables() {
const packageJsonData = await fse.readFile(path.resolve('./package.json'), 'utf8');
const { version = null } = JSON.parse(packageJsonData);

if (!version) {
throw new Error('Could not find the version in the package.json');
}

const [versionNumber, preReleaseInfo] = version.split('-');
const [major, minor, patch] = versionNumber.split('.');
const [preReleaseLabel, preReleaseNumber] = preReleaseInfo ? preReleaseInfo.split('.') : [];

if (!major || !minor || !patch) {
throw new Error(`Couldn't parse version from package.json`);
}

return {
MUI_VERSION: version,
MUI_MAJOR_VERSION: major,
MUI_MINOR_VERSION: minor,
MUI_PATCH_VERSION: patch,
MUI_PRERELEASE_LABEL: preReleaseLabel,
MUI_PRERELEASE_NUMBER: preReleaseNumber,
};
}