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] Replace all '@mui/utils' imports #39878

Closed
wants to merge 3 commits into from

Conversation

MonstraG
Copy link
Contributor

This is an attempt at doing #35840 with a limited scope:

  • only imports from '@mui/utils'
  • only imports in '@mui/material'

All of these changes were done automatically with:

node.js code
const fs = require("fs").promises;
const path = require("path");

const packages = ["@mui/utils"];

/**
 * @param line {string}
 * @returns {string}
 */
const processLine = (line) => {
	for (let pkg of packages) {
		const regex = new RegExp(`import {(.*?)} from '${pkg}';`);
		const match = line.match(regex);

		if (match) {
			const importsArray = match[1].split(",").map((str) => str.trim());

			return importsArray
				.map((importItem) => {
					const [originalName, aliasName] = importItem
						.split(" as ")
						.map((name) => name.trim());
					const importedAs = aliasName || originalName;
					const importedFrom = `${pkg}/${importedAs}`;

					return `import ${importedAs} from '${importedFrom}';`;
				})
				.join("\n");
		}
	}

	return line;
};

/**
 * @param filePath {string}
 * @returns {Promise<void>}
 */
const processFile = async (filePath) => {
	const data = await fs.readFile(filePath, "utf8");
	const result = data.toString().split("\n").map(processLine).join("\n");
	await fs.writeFile(filePath, result, "utf8");
};

/**
 * @param directoryPath {string}
 * @returns {Promise<void>}
 */
const processDirectory = async (directoryPath) => {
	const files = await fs.readdir(directoryPath, { withFileTypes: true });

	for (const file of files) {
		const filePath = path.join(directoryPath, file.name);

		if (file.isDirectory()) {
			await processDirectory(filePath);
		} else {
			await processFile(filePath).catch((err) =>
				console.error(`Error processing file ${filePath}: ${err.message}`)
			);
		}
	}
};

processDirectory("/material-ui/packages/mui-material/src").catch(console.error);

If this is a replacement that we are looking for, I can try running this on all packages, and start adapting it to other imports (like system, base, but they would require more work)

@mui-bot
Copy link

mui-bot commented Nov 14, 2023

Netlify deploy preview

https://deploy-preview-39878--material-ui.netlify.app/

Bundle size report

Bundle size will be reported once CircleCI build #614978 finishes.

Generated by 🚫 dangerJS against a24a9e5

@mnajdova mnajdova added package: material-ui Specific to @mui/material core Infrastructure work going on behind the scenes labels Nov 15, 2023
@mnajdova
Copy link
Member

I am creating #39882 to fix the errors for exports not being modules.

@zannager zannager requested a review from michaldudak November 15, 2023 15:26
@danilo-leal danilo-leal changed the title chore: replace all '@mui/utils' in material-ui package [material-ui] Replace all '@mui/utils' imports Nov 21, 2023
@@ -1,4 +1,4 @@
import { unstable_generateUtilityClasses as generateUtilityClasses } from '@mui/utils';
import generateUtilityClasses from '@mui/utils/generateUtilityClasses';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With generateUtilityClasses, we could import the local Material UI version (similarly to generateUtilityClass). It would allow us to customize the class name pattern.

Suggested change
import generateUtilityClasses from '@mui/utils/generateUtilityClasses';
import generateUtilityClasses from '../generateUtilityClasses';

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 20, 2023
@michaldudak
Copy link
Member

@MonstraG could you please rebase on top of the latest master and check the failing tests?

@MonstraG
Copy link
Contributor Author

MonstraG commented Dec 22, 2023

I'll open a new pull request then.

It seems that somebody already merged #40254,

Which reverses this decision:

With generateUtilityClasses, we could import the local Material UI version (similarly to generateUtilityClass). It would allow us to customize the class name pattern.

so I'm not doing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes package: material-ui Specific to @mui/material PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants