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, + }; +}