Skip to content

Commit

Permalink
Refactor to parse values at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoAndai committed Aug 6, 2024
1 parent a0c31bd commit e06ebc8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 52 deletions.
9 changes: 8 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
},
],
];
Expand Down
29 changes: 6 additions & 23 deletions packages/mui-material/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 6 additions & 23 deletions packages/mui-system/src/version/index.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 2 additions & 5 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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');
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,
};
}

0 comments on commit e06ebc8

Please sign in to comment.