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 #40280

Closed
wants to merge 1 commit 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)

See also: rebased #39878

@mui-bot
Copy link

mui-bot commented Dec 22, 2023

Netlify deploy preview

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

Bundle size report

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

Generated by 🚫 dangerJS against d169524

@MonstraG
Copy link
Contributor Author

MonstraG commented Dec 22, 2023

Funny, build:modern succeeds, but build fails.

Yea, for every import from mui-utils/src/*.ts, build:umd tries to find `mui-utils/src/*/index.ts, not sure what to do with that,

@danilo-leal danilo-leal changed the title chore: replace all '@mui/utils' in 'mui-material' package [material-ui] Replace all '@mui/utils' imports Dec 22, 2023
@danilo-leal danilo-leal added package: utils Specific to the @mui/utils package package: material-ui Specific to @mui/material labels Dec 22, 2023
@zannager zannager requested a review from michaldudak December 25, 2023 11:46
@michaldudak
Copy link
Member

Sorry about the confusion around these imports. Let us agree internally on the way these should be handled first, and then we can either resurrect the previous PR or continue with this one.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 17, 2024
@MonstraG
Copy link
Contributor Author

MonstraG commented Feb 28, 2024

And my pr about this got sniped again!

#40851

:(

@michaldudak
Copy link
Member

Sorry about that. @siriwatknp did more by importing additional functions from @mui/utils. I hope this won't discourage you from contributing in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material package: utils Specific to the @mui/utils package 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