From 059f3af7267142aeed54c5ea8545f2e6fd0cfe92 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Mon, 5 Aug 2024 17:39:52 -0400 Subject: [PATCH 1/6] Add support for version const --- package.json | 2 +- packages/mui-material/src/index.d.ts | 2 + packages/mui-material/src/index.js | 2 + packages/mui-system/src/index.d.ts | 2 + packages/mui-system/src/index.js | 2 + scripts/releaseConst.mjs | 75 ++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 scripts/releaseConst.mjs diff --git a/package.json b/package.json index 81119abdf0de00..a2dc96a30f9e81 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build:public": "lerna run --no-private build", "build:public:ci": "lerna run --no-private build --concurrency 8 --skip-nx-cache", "build:codesandbox": "NODE_OPTIONS=\"--max_old_space_size=4096\" lerna run --concurrency 8 --scope \"@mui/*\" --scope \"@mui-internal/*\" --no-private build", - "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker", + "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker && node scripts/releaseConst.mjs", "release:build": "lerna run --concurrency 8 --no-private build --skip-nx-cache", "release:changelog": "node scripts/releaseChangelog.mjs", "release:publish": "pnpm publish --recursive --tag next", diff --git a/packages/mui-material/src/index.d.ts b/packages/mui-material/src/index.d.ts index 6df30184e376b9..c20f8a57dc583a 100644 --- a/packages/mui-material/src/index.d.ts +++ b/packages/mui-material/src/index.d.ts @@ -2,6 +2,8 @@ import * as React from 'react'; import { DistributiveOmit } from '@mui/types'; import { StyledComponentProps } from './styles'; +export const version: string; + export { StyledComponentProps }; /** diff --git a/packages/mui-material/src/index.js b/packages/mui-material/src/index.js index 6877769b6767b2..3b3a2dd7617000 100644 --- a/packages/mui-material/src/index.js +++ b/packages/mui-material/src/index.js @@ -1,6 +1,8 @@ /* eslint-disable import/export */ import * as colors from './colors'; +export const version = '6.0.0-beta.4'; + export { colors }; export * from './styles'; diff --git a/packages/mui-system/src/index.d.ts b/packages/mui-system/src/index.d.ts index 39833a63d62e29..01d294e68a4d6d 100644 --- a/packages/mui-system/src/index.d.ts +++ b/packages/mui-system/src/index.d.ts @@ -1,6 +1,8 @@ // disable automatic export export {}; +export const version: string; + export * from './borders'; export { default as breakpoints, handleBreakpoints, mergeBreakpointsInOrder } from './breakpoints'; diff --git a/packages/mui-system/src/index.js b/packages/mui-system/src/index.js index 1356ec711b58b4..0f066068688d69 100644 --- a/packages/mui-system/src/index.js +++ b/packages/mui-system/src/index.js @@ -1,5 +1,7 @@ import MuiError from '@mui/internal-babel-macros/MuiError.macro'; +export const version = '6.0.0-beta.4'; + export { css, keyframes, StyledEngineProvider } from '@mui/styled-engine'; export { default as GlobalStyles } from './GlobalStyles'; export { default as borders } from './borders'; diff --git a/scripts/releaseConst.mjs b/scripts/releaseConst.mjs new file mode 100644 index 00000000000000..24845d76064e01 --- /dev/null +++ b/scripts/releaseConst.mjs @@ -0,0 +1,75 @@ +import fs from 'fs'; +import yargs from 'yargs'; +import { getWorkspaceRoot } from './utils.mjs'; + +// Packages to add the version to +const packages = ['mui-material', 'mui-system']; + +function run() { + for (const packageName of packages) { + // Define the path to the package folder + const packagePath = `${getWorkspaceRoot()}/packages/${packageName}`; + + // Define the path to the package.json file + const packageJsonPath = `${packagePath}/package.json`; + + // Define the path to the index.js file + const indexPath = `${packagePath}/src/index.js`; + + // Read the file content + fs.readFile(packageJsonPath, 'utf8', (err1, packageJsonData) => { + if (err1) { + console.error('Error reading the file:', err1); + return; + } + + try { + // Parse the JSON content + const packageJson = JSON.parse(packageJsonData); + const version = packageJson.version; + + if (version) { + // Write the updated version to the index.js file + fs.readFile(indexPath, 'utf8', (err2, indexData) => { + if (err2) { + console.error('Error reading the file:', err2); + return; + } + + if (!indexData.includes('export const version =')) { + console.error(`The version const is not found in the ${indexPath} file.`); + return; + } + + // Replace the version in the file content + const result = indexData.replace( + /export const version.*;/g, + `export const version = '${version}';`, + ); + + // // Write the updated content to the file + fs.writeFile(indexPath, result, 'utf8', (writeErr) => { + if (writeErr) { + console.error('Error writing the file:', writeErr); + } + }); + }); + } + } catch (parseErr) { + console.error('Error parsing JSON:', parseErr); + } + }); + } +} + +yargs(process.argv.slice(2)) + .command({ + command: '$0', + description: 'Updates the version const in the index.js files of the packages.', + builder: () => {}, + handler: run, + }) + .help() + .strict(true) + .version(false) + .parse(); From c6542ff496737621c093d6e280f32b80535931d3 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Mon, 5 Aug 2024 17:48:48 -0400 Subject: [PATCH 2/6] Rename releaseConst script to updateVersionConst --- package.json | 2 +- scripts/{releaseConst.mjs => updateVersionConst.mjs} | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) rename scripts/{releaseConst.mjs => updateVersionConst.mjs} (94%) diff --git a/package.json b/package.json index a2dc96a30f9e81..0d593d4b88afa0 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build:public": "lerna run --no-private build", "build:public:ci": "lerna run --no-private build --concurrency 8 --skip-nx-cache", "build:codesandbox": "NODE_OPTIONS=\"--max_old_space_size=4096\" lerna run --concurrency 8 --scope \"@mui/*\" --scope \"@mui-internal/*\" --no-private build", - "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker && node scripts/releaseConst.mjs", + "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker && node scripts/updateVersionConst.mjs", "release:build": "lerna run --concurrency 8 --no-private build --skip-nx-cache", "release:changelog": "node scripts/releaseChangelog.mjs", "release:publish": "pnpm publish --recursive --tag next", diff --git a/scripts/releaseConst.mjs b/scripts/updateVersionConst.mjs similarity index 94% rename from scripts/releaseConst.mjs rename to scripts/updateVersionConst.mjs index 24845d76064e01..afa4cb21dd3e25 100644 --- a/scripts/releaseConst.mjs +++ b/scripts/updateVersionConst.mjs @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ import fs from 'fs'; import yargs from 'yargs'; import { getWorkspaceRoot } from './utils.mjs'; @@ -52,6 +53,8 @@ function run() { if (writeErr) { console.error('Error writing the file:', writeErr); } + + console.info(`Updated the version const in the ${indexPath} file to ${version}.`); }); }); } From 744c5a642de0e83ab326c3925a33e62933caa8a0 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Tue, 6 Aug 2024 08:54:44 -0400 Subject: [PATCH 3/6] Refactor to add version at compilation --- babel.config.js | 6 +++ package.json | 3 +- packages/mui-material/src/index.js | 2 +- packages/mui-system/src/index.js | 2 +- pnpm-lock.yaml | 8 +++ scripts/build.mjs | 5 ++ scripts/updateVersionConst.mjs | 78 ------------------------------ 7 files changed, 23 insertions(+), 81 deletions(-) delete mode 100644 scripts/updateVersionConst.mjs diff --git a/babel.config.js b/babel.config.js index b2ebe2fa58f681..a3be5b3e296d19 100644 --- a/babel.config.js +++ b/babel.config.js @@ -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') { diff --git a/package.json b/package.json index 0d593d4b88afa0..c7585433c7255e 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build:public": "lerna run --no-private build", "build:public:ci": "lerna run --no-private build --concurrency 8 --skip-nx-cache", "build:codesandbox": "NODE_OPTIONS=\"--max_old_space_size=4096\" lerna run --concurrency 8 --scope \"@mui/*\" --scope \"@mui-internal/*\" --no-private build", - "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker && node scripts/updateVersionConst.mjs", + "release:version": "lerna version --no-changelog --no-push --no-git-tag-version --no-private --force-publish=@mui/core-downloads-tracker", "release:build": "lerna run --concurrency 8 --no-private build --skip-nx-cache", "release:changelog": "node scripts/releaseChangelog.mjs", "release:publish": "pnpm publish --recursive --tag next", @@ -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", diff --git a/packages/mui-material/src/index.js b/packages/mui-material/src/index.js index 3b3a2dd7617000..53f7e54af0bbf2 100644 --- a/packages/mui-material/src/index.js +++ b/packages/mui-material/src/index.js @@ -1,7 +1,7 @@ /* eslint-disable import/export */ import * as colors from './colors'; -export const version = '6.0.0-beta.4'; +export const version = process.env.MUI_PACKAGE_VERSION; export { colors }; export * from './styles'; diff --git a/packages/mui-system/src/index.js b/packages/mui-system/src/index.js index 0f066068688d69..9a266017c546a6 100644 --- a/packages/mui-system/src/index.js +++ b/packages/mui-system/src/index.js @@ -1,6 +1,6 @@ import MuiError from '@mui/internal-babel-macros/MuiError.macro'; -export const version = '6.0.0-beta.4'; +export const version = process.env.MUI_PACKAGE_VERSION; export { css, keyframes, StyledEngineProvider } from '@mui/styled-engine'; export { default as GlobalStyles } from './GlobalStyles'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f0feb429fc0fe8..b0640fe31f232e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -153,6 +153,9 @@ importers: babel-plugin-react-remove-properties: specifier: ^0.3.0 version: 0.3.0 + babel-plugin-transform-inline-environment-variables: + specifier: ^0.4.4 + version: 0.4.4 babel-plugin-transform-react-remove-prop-types: specifier: ^0.4.24 version: 0.4.24 @@ -5879,6 +5882,9 @@ packages: babel-plugin-transform-flow-enums@0.0.2: resolution: {integrity: sha512-g4aaCrDDOsWjbm0PUUeVnkcVd6AKJsVc/MbnPhEotEpkeJQP6b8nzewohQi7+QS8UyPehOhGWn0nOwjvWpmMvQ==} + babel-plugin-transform-inline-environment-variables@0.4.4: + resolution: {integrity: sha512-bJILBtn5a11SmtR2j/3mBOjX4K3weC6cq+NNZ7hG22wCAqpc3qtj/iN7dSe9HDiS46lgp1nHsQgeYrea/RUe+g==} + babel-plugin-transform-react-remove-prop-types@0.4.24: resolution: {integrity: sha512-eqj0hVcJUR57/Ug2zE1Yswsw4LhuqqHhD+8v120T1cl3kjg76QwtyBrdIk4WVwK+lAhBJVYCd/v+4nc4y+8JsA==} @@ -16908,6 +16914,8 @@ snapshots: transitivePeerDependencies: - '@babel/core' + babel-plugin-transform-inline-environment-variables@0.4.4: {} + babel-plugin-transform-react-remove-prop-types@0.4.24: {} bail@1.0.5: {} diff --git a/scripts/build.mjs b/scripts/build.mjs index 3a9f594a72844e..ade058eb0cbba1 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -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); @@ -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']; diff --git a/scripts/updateVersionConst.mjs b/scripts/updateVersionConst.mjs deleted file mode 100644 index afa4cb21dd3e25..00000000000000 --- a/scripts/updateVersionConst.mjs +++ /dev/null @@ -1,78 +0,0 @@ -/* eslint-disable no-console */ -import fs from 'fs'; -import yargs from 'yargs'; -import { getWorkspaceRoot } from './utils.mjs'; - -// Packages to add the version to -const packages = ['mui-material', 'mui-system']; - -function run() { - for (const packageName of packages) { - // Define the path to the package folder - const packagePath = `${getWorkspaceRoot()}/packages/${packageName}`; - - // Define the path to the package.json file - const packageJsonPath = `${packagePath}/package.json`; - - // Define the path to the index.js file - const indexPath = `${packagePath}/src/index.js`; - - // Read the file content - fs.readFile(packageJsonPath, 'utf8', (err1, packageJsonData) => { - if (err1) { - console.error('Error reading the file:', err1); - return; - } - - try { - // Parse the JSON content - const packageJson = JSON.parse(packageJsonData); - const version = packageJson.version; - - if (version) { - // Write the updated version to the index.js file - fs.readFile(indexPath, 'utf8', (err2, indexData) => { - if (err2) { - console.error('Error reading the file:', err2); - return; - } - - if (!indexData.includes('export const version =')) { - console.error(`The version const is not found in the ${indexPath} file.`); - return; - } - - // Replace the version in the file content - const result = indexData.replace( - /export const version.*;/g, - `export const version = '${version}';`, - ); - - // // Write the updated content to the file - fs.writeFile(indexPath, result, 'utf8', (writeErr) => { - if (writeErr) { - console.error('Error writing the file:', writeErr); - } - - console.info(`Updated the version const in the ${indexPath} file to ${version}.`); - }); - }); - } - } catch (parseErr) { - console.error('Error parsing JSON:', parseErr); - } - }); - } -} - -yargs(process.argv.slice(2)) - .command({ - command: '$0', - description: 'Updates the version const in the index.js files of the packages.', - builder: () => {}, - handler: run, - }) - .help() - .strict(true) - .version(false) - .parse(); From c1e460d071c831641f2bc2d5ee57c74d629c45c9 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Tue, 6 Aug 2024 11:27:52 -0400 Subject: [PATCH 4/6] Add parsed exports --- packages/mui-material/src/index.d.ts | 5 +++-- packages/mui-material/src/index.js | 5 +++-- packages/mui-material/src/index.test.js | 15 ++++++++++--- packages/mui-material/src/version/index.ts | 25 ++++++++++++++++++++++ packages/mui-system/src/index.d.ts | 5 +++-- packages/mui-system/src/index.js | 4 ++-- packages/mui-system/src/version/index.ts | 25 ++++++++++++++++++++++ 7 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 packages/mui-material/src/version/index.ts create mode 100644 packages/mui-system/src/version/index.ts diff --git a/packages/mui-material/src/index.d.ts b/packages/mui-material/src/index.d.ts index c20f8a57dc583a..dab4161e3304c4 100644 --- a/packages/mui-material/src/index.d.ts +++ b/packages/mui-material/src/index.d.ts @@ -2,8 +2,6 @@ import * as React from 'react'; import { DistributiveOmit } from '@mui/types'; import { StyledComponentProps } from './styles'; -export const version: string; - export { StyledComponentProps }; /** @@ -477,6 +475,9 @@ export * from './useAutocomplete'; export { default as GlobalStyles } from './GlobalStyles'; export * from './GlobalStyles'; +export { default as version } from './version'; +export * from './version'; + /** * @deprecated will be removed in v5.beta, please use StyledEngineProvider from @mui/material/styles instead */ diff --git a/packages/mui-material/src/index.js b/packages/mui-material/src/index.js index 53f7e54af0bbf2..3586991f521f5b 100644 --- a/packages/mui-material/src/index.js +++ b/packages/mui-material/src/index.js @@ -1,8 +1,6 @@ /* eslint-disable import/export */ import * as colors from './colors'; -export const version = process.env.MUI_PACKAGE_VERSION; - export { colors }; export * from './styles'; @@ -418,3 +416,6 @@ export * from './generateUtilityClass'; export { default as generateUtilityClasses } from './generateUtilityClasses'; export { default as Unstable_TrapFocus } from './Unstable_TrapFocus'; + +export { default as version } from './version'; +export * from './version'; diff --git a/packages/mui-material/src/index.test.js b/packages/mui-material/src/index.test.js index df67ee7a06abdf..f015581099d04e 100644 --- a/packages/mui-material/src/index.test.js +++ b/packages/mui-material/src/index.test.js @@ -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', () => { diff --git a/packages/mui-material/src/version/index.ts b/packages/mui-material/src/version/index.ts new file mode 100644 index 00000000000000..5dfb1765e2b19e --- /dev/null +++ b/packages/mui-material/src/version/index.ts @@ -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; diff --git a/packages/mui-system/src/index.d.ts b/packages/mui-system/src/index.d.ts index 01d294e68a4d6d..5f12b5b99a8f34 100644 --- a/packages/mui-system/src/index.d.ts +++ b/packages/mui-system/src/index.d.ts @@ -1,8 +1,6 @@ // disable automatic export export {}; -export const version: string; - export * from './borders'; export { default as breakpoints, handleBreakpoints, mergeBreakpointsInOrder } from './breakpoints'; @@ -124,3 +122,6 @@ export * from './Grid'; export { default as Stack } from './Stack'; export * from './Stack'; + +export { default as version } from './version'; +export * from './version'; diff --git a/packages/mui-system/src/index.js b/packages/mui-system/src/index.js index 9a266017c546a6..50d0639d765e14 100644 --- a/packages/mui-system/src/index.js +++ b/packages/mui-system/src/index.js @@ -1,7 +1,5 @@ import MuiError from '@mui/internal-babel-macros/MuiError.macro'; -export const version = process.env.MUI_PACKAGE_VERSION; - export { css, keyframes, StyledEngineProvider } from '@mui/styled-engine'; export { default as GlobalStyles } from './GlobalStyles'; export { default as borders } from './borders'; @@ -69,6 +67,8 @@ export { default as unstable_createCssVarsTheme } from './cssVars/createCssVarsT export { default as responsivePropType } from './responsivePropType'; export { default as RtlProvider } from './RtlProvider'; export * from './RtlProvider'; +export { default as version } from './version'; +export * from './version'; /** ----------------- */ /** Layout components */ diff --git a/packages/mui-system/src/version/index.ts b/packages/mui-system/src/version/index.ts new file mode 100644 index 00000000000000..5dfb1765e2b19e --- /dev/null +++ b/packages/mui-system/src/version/index.ts @@ -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; From a0c31bd674481079a0e5d37f6452919696f7eb6f Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Tue, 6 Aug 2024 12:07:49 -0400 Subject: [PATCH 5/6] Remove redundant export from index --- packages/mui-material/src/index.d.ts | 1 - packages/mui-material/src/index.js | 1 - packages/mui-system/src/index.d.ts | 1 - packages/mui-system/src/index.js | 1 - 4 files changed, 4 deletions(-) diff --git a/packages/mui-material/src/index.d.ts b/packages/mui-material/src/index.d.ts index dab4161e3304c4..bb38816ee9ea8d 100644 --- a/packages/mui-material/src/index.d.ts +++ b/packages/mui-material/src/index.d.ts @@ -475,7 +475,6 @@ export * from './useAutocomplete'; export { default as GlobalStyles } from './GlobalStyles'; export * from './GlobalStyles'; -export { default as version } from './version'; export * from './version'; /** diff --git a/packages/mui-material/src/index.js b/packages/mui-material/src/index.js index 3586991f521f5b..6ce029cf0e26f5 100644 --- a/packages/mui-material/src/index.js +++ b/packages/mui-material/src/index.js @@ -417,5 +417,4 @@ export { default as generateUtilityClasses } from './generateUtilityClasses'; export { default as Unstable_TrapFocus } from './Unstable_TrapFocus'; -export { default as version } from './version'; export * from './version'; diff --git a/packages/mui-system/src/index.d.ts b/packages/mui-system/src/index.d.ts index 5f12b5b99a8f34..c8498706f467eb 100644 --- a/packages/mui-system/src/index.d.ts +++ b/packages/mui-system/src/index.d.ts @@ -123,5 +123,4 @@ export * from './Grid'; export { default as Stack } from './Stack'; export * from './Stack'; -export { default as version } from './version'; export * from './version'; diff --git a/packages/mui-system/src/index.js b/packages/mui-system/src/index.js index 50d0639d765e14..b3c0bc3a6cd6ed 100644 --- a/packages/mui-system/src/index.js +++ b/packages/mui-system/src/index.js @@ -67,7 +67,6 @@ export { default as unstable_createCssVarsTheme } from './cssVars/createCssVarsT export { default as responsivePropType } from './responsivePropType'; export { default as RtlProvider } from './RtlProvider'; export * from './RtlProvider'; -export { default as version } from './version'; export * from './version'; /** ----------------- */ From e06ebc886dd69de5194a1db8231a806d2feab904 Mon Sep 17 00:00:00 2001 From: DiegoAndai Date: Tue, 6 Aug 2024 16:09:01 -0400 Subject: [PATCH 6/6] Refactor to parse values at build time --- babel.config.js | 9 ++++++- packages/mui-material/src/version/index.ts | 29 +++++---------------- packages/mui-system/src/version/index.ts | 29 +++++---------------- scripts/build.mjs | 7 ++--- scripts/utils.mjs | 30 ++++++++++++++++++++++ 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/babel.config.js b/babel.config.js index a3be5b3e296d19..5940d3b7bb9542 100644 --- a/babel.config.js +++ b/babel.config.js @@ -84,7 +84,14 @@ module.exports = function getBabelConfig(api) { [ 'transform-inline-environment-variables', { - include: ['MUI_PACKAGE_VERSION'], + include: [ + 'MUI_VERSION', + 'MUI_MAJOR_VERSION', + 'MUI_MINOR_VERSION', + 'MUI_PATCH_VERSION', + 'MUI_PRERELEASE_LABEL', + 'MUI_PRERELEASE_NUMBER', + ], }, ], ]; diff --git a/packages/mui-material/src/version/index.ts b/packages/mui-material/src/version/index.ts index 5dfb1765e2b19e..8e49930afb8039 100644 --- a/packages/mui-material/src/version/index.ts +++ b/packages/mui-material/src/version/index.ts @@ -1,25 +1,8 @@ -const version = process.env.MUI_PACKAGE_VERSION || null; +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; -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; diff --git a/packages/mui-system/src/version/index.ts b/packages/mui-system/src/version/index.ts index 5dfb1765e2b19e..8e49930afb8039 100644 --- a/packages/mui-system/src/version/index.ts +++ b/packages/mui-system/src/version/index.ts @@ -1,25 +1,8 @@ -const version = process.env.MUI_PACKAGE_VERSION || null; +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; -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; diff --git a/scripts/build.mjs b/scripts/build.mjs index ade058eb0cbba1..47c7b66ab13378 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -3,8 +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'; +import { getVersionEnvVariables, getWorkspaceRoot } from './utils.mjs'; const exec = promisify(childProcess.exec); @@ -26,13 +25,11 @@ 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, + ...(await getVersionEnvVariables()), }; const babelConfigPath = path.resolve(getWorkspaceRoot(), 'babel.config.js'); diff --git a/scripts/utils.mjs b/scripts/utils.mjs index 7a62fe6c4ad054..d0976a9d5d83da 100644 --- a/scripts/utils.mjs +++ b/scripts/utils.mjs @@ -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. @@ -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, + }; +}