Skip to content

Commit f8a6e3c

Browse files
authored
[material-ui][mui-system] Add support for version runtime checks (#43190)
1 parent c536582 commit f8a6e3c

File tree

12 files changed

+90
-4
lines changed

12 files changed

+90
-4
lines changed

babel.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ module.exports = function getBabelConfig(api) {
8181
mode: 'unsafe-wrap',
8282
},
8383
],
84+
[
85+
'transform-inline-environment-variables',
86+
{
87+
include: [
88+
'MUI_VERSION',
89+
'MUI_MAJOR_VERSION',
90+
'MUI_MINOR_VERSION',
91+
'MUI_PATCH_VERSION',
92+
'MUI_PRERELEASE_LABEL',
93+
'MUI_PRERELEASE_NUMBER',
94+
],
95+
},
96+
],
8497
];
8598

8699
if (process.env.NODE_ENV === 'production') {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"babel-plugin-module-resolver": "^5.0.2",
135135
"babel-plugin-optimize-clsx": "^2.6.2",
136136
"babel-plugin-react-remove-properties": "^0.3.0",
137+
"babel-plugin-transform-inline-environment-variables": "^0.4.4",
137138
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
138139
"chalk": "^5.3.0",
139140
"compression-webpack-plugin": "^11.1.0",

packages/mui-material/src/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ export * from './useAutocomplete';
475475
export { default as GlobalStyles } from './GlobalStyles';
476476
export * from './GlobalStyles';
477477

478+
export * from './version';
479+
478480
/**
479481
* @deprecated will be removed in v5.beta, please use StyledEngineProvider from @mui/material/styles instead
480482
*/

packages/mui-material/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,5 @@ export * from './generateUtilityClass';
416416
export { default as generateUtilityClasses } from './generateUtilityClasses';
417417

418418
export { default as Unstable_TrapFocus } from './Unstable_TrapFocus';
419+
420+
export * from './version';

packages/mui-material/src/index.test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66
import { expect } from 'chai';
77
import * as MaterialUI from './index';
88

9+
const versionExports = [
10+
'version',
11+
'major',
12+
'minor',
13+
'patch',
14+
'preReleaseLabel',
15+
'preReleaseNumber',
16+
];
17+
918
describe('material-ui', () => {
1019
it('should have exports', () => {
1120
expect(typeof MaterialUI).to.equal('object');
1221
});
1322

1423
it('should not have undefined exports', () => {
15-
Object.keys(MaterialUI).forEach((exportKey) =>
16-
expect(Boolean(MaterialUI[exportKey])).to.equal(true),
17-
);
24+
Object.keys(MaterialUI)
25+
.filter((exportKey) => !versionExports.includes(exportKey))
26+
.forEach((exportKey) => expect(Boolean(MaterialUI[exportKey])).to.equal(true));
1827
});
1928

2029
it('should reexport certain members from @mui/base', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const version = process.env.MUI_VERSION;
2+
export const major = Number(process.env.MUI_MAJOR_VERSION);
3+
export const minor = Number(process.env.MUI_MINOR_VERSION);
4+
export const patch = Number(process.env.MUI_PATCH_VERSION);
5+
export const preReleaseLabel = process.env.MUI_PRERELEASE_LABEL || null;
6+
export const preReleaseNumber = Number(process.env.MUI_PRERELEASE_NUMBER) || null;
7+
8+
export default version;

packages/mui-system/src/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,5 @@ export * from './Grid';
122122

123123
export { default as Stack } from './Stack';
124124
export * from './Stack';
125+
126+
export * from './version';

packages/mui-system/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export { default as unstable_createCssVarsTheme } from './cssVars/createCssVarsT
6767
export { default as responsivePropType } from './responsivePropType';
6868
export { default as RtlProvider } from './RtlProvider';
6969
export * from './RtlProvider';
70+
export * from './version';
7071

7172
/** ----------------- */
7273
/** Layout components */
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const version = process.env.MUI_VERSION;
2+
export const major = Number(process.env.MUI_MAJOR_VERSION);
3+
export const minor = Number(process.env.MUI_MINOR_VERSION);
4+
export const patch = Number(process.env.MUI_PATCH_VERSION);
5+
export const preReleaseLabel = process.env.MUI_PRERELEASE_LABEL || null;
6+
export const preReleaseNumber = Number(process.env.MUI_PRERELEASE_NUMBER) || null;
7+
8+
export default version;

pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import glob from 'fast-glob';
33
import path from 'path';
44
import { promisify } from 'util';
55
import yargs from 'yargs';
6-
import { getWorkspaceRoot } from './utils.mjs';
6+
import { getVersionEnvVariables, getWorkspaceRoot } from './utils.mjs';
77

88
const exec = promisify(childProcess.exec);
99

@@ -29,7 +29,9 @@ async function run(argv) {
2929
NODE_ENV: 'production',
3030
BABEL_ENV: bundle,
3131
MUI_BUILD_VERBOSE: verbose,
32+
...(await getVersionEnvVariables()),
3233
};
34+
3335
const babelConfigPath = path.resolve(getWorkspaceRoot(), 'babel.config.js');
3436
const srcDir = path.resolve('./src');
3537
const extensions = ['.js', '.ts', '.tsx'];

scripts/utils.mjs

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import url from 'url';
3+
import fse from 'fs-extra';
34

45
/**
56
* Returns the full path of the root directory of this repository.
@@ -10,3 +11,32 @@ export function getWorkspaceRoot() {
1011
const workspaceRoot = path.resolve(currentDirectory, '..');
1112
return workspaceRoot;
1213
}
14+
15+
/**
16+
* Returns the version and destructured values of the version as env variables to be replaced.
17+
*/
18+
export async function getVersionEnvVariables() {
19+
const packageJsonData = await fse.readFile(path.resolve('./package.json'), 'utf8');
20+
const { version = null } = JSON.parse(packageJsonData);
21+
22+
if (!version) {
23+
throw new Error('Could not find the version in the package.json');
24+
}
25+
26+
const [versionNumber, preReleaseInfo] = version.split('-');
27+
const [major, minor, patch] = versionNumber.split('.');
28+
const [preReleaseLabel, preReleaseNumber] = preReleaseInfo ? preReleaseInfo.split('.') : [];
29+
30+
if (!major || !minor || !patch) {
31+
throw new Error(`Couldn't parse version from package.json`);
32+
}
33+
34+
return {
35+
MUI_VERSION: version,
36+
MUI_MAJOR_VERSION: major,
37+
MUI_MINOR_VERSION: minor,
38+
MUI_PATCH_VERSION: patch,
39+
MUI_PRERELEASE_LABEL: preReleaseLabel,
40+
MUI_PRERELEASE_NUMBER: preReleaseNumber,
41+
};
42+
}

0 commit comments

Comments
 (0)