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 5 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
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ module.exports = function getBabelConfig(api) {
mode: 'unsafe-wrap',
},
],
[
'transform-inline-environment-variables',
{
include: ['MUI_PACKAGE_VERSION'],
},
],
];

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
25 changes: 25 additions & 0 deletions packages/mui-material/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const version = process.env.MUI_PACKAGE_VERSION || null;

let versionNumber: string | null = null;
let preReleaseInfo: string | null = null;

if (version) {
[versionNumber, preReleaseInfo] = version.split('-');
DiegoAndai marked this conversation as resolved.
Show resolved Hide resolved
}

let destructuredVersion: number[] | null[] = [null, null, null];
let destructuredPreReleaseInfo: [string, number] | null[] = [null, null];

if (versionNumber) {
destructuredVersion = versionNumber.split('.').map(Number);
}

if (preReleaseInfo) {
destructuredPreReleaseInfo = [preReleaseInfo.split('.')[0], Number(preReleaseInfo.split('.')[1])];
}

const [major, minor, patch] = destructuredVersion;
const [preReleaseLabel, preReleaseNumber] = destructuredPreReleaseInfo;

export { version, major, minor, patch, preReleaseLabel, preReleaseNumber };
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
25 changes: 25 additions & 0 deletions packages/mui-system/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const version = process.env.MUI_PACKAGE_VERSION || null;

let versionNumber: string | null = null;
let preReleaseInfo: string | null = null;

if (version) {
[versionNumber, preReleaseInfo] = version.split('-');
}

let destructuredVersion: number[] | null[] = [null, null, null];
let destructuredPreReleaseInfo: [string, number] | null[] = [null, null];

if (versionNumber) {
destructuredVersion = versionNumber.split('.').map(Number);
}

if (preReleaseInfo) {
destructuredPreReleaseInfo = [preReleaseInfo.split('.')[0], Number(preReleaseInfo.split('.')[1])];
}

const [major, minor, patch] = destructuredVersion;
const [preReleaseLabel, preReleaseNumber] = destructuredPreReleaseInfo;

export { version, major, minor, patch, preReleaseLabel, preReleaseNumber };
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.

5 changes: 5 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import glob from 'fast-glob';
import path from 'path';
import { promisify } from 'util';
import yargs from 'yargs';
import fse from 'fs-extra';
import { getWorkspaceRoot } from './utils.mjs';

const exec = promisify(childProcess.exec);
Expand All @@ -25,11 +26,15 @@ async function run(argv) {
);
}

const packageJsonData = await fse.readFile(path.resolve('./package.json'), 'utf8');

const env = {
NODE_ENV: 'production',
BABEL_ENV: bundle,
MUI_BUILD_VERBOSE: verbose,
MUI_PACKAGE_VERSION: JSON.parse(packageJsonData).version,
};

const babelConfigPath = path.resolve(getWorkspaceRoot(), 'babel.config.js');
const srcDir = path.resolve('./src');
const extensions = ['.js', '.ts', '.tsx'];
Expand Down