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

[core] Convert scripts to ES modules #35036

Merged
merged 16 commits into from Nov 14, 2022
Merged

Conversation

michaldudak
Copy link
Member

@michaldudak michaldudak commented Nov 7, 2022

Converted applicable scripts from the /scripts directory to ESM.
Contents of the scripts/sizeSnapshot directory were not converted because they were used by Danger. The dangerfile can't be an ES module.

Also renamed a few scripts for better consistency (kebab-case to camelCase).

@michaldudak michaldudak added the core Infrastructure work going on behind the scenes label Nov 7, 2022
@mui-bot
Copy link

mui-bot commented Nov 7, 2022

Fails
🚫

node failed.

Messages
📖 Netlify deploy preview: https://deploy-preview-35036--material-ui.netlify.app/

Log

danger-results://tmp/danger-results-0d77b8cd.json

Generated by 🚫 dangerJS against 6c8e1e8

@michaldudak
Copy link
Member Author

@mui/toolpad, @mui/x please verify if your projects are affected by these changes.

@michaldudak michaldudak marked this pull request as ready for review November 8, 2022 13:47
@michaldudak michaldudak requested a review from a team November 8, 2022 13:47
@Janpot
Copy link
Member

Janpot commented Nov 8, 2022

Minor changes required mui/mui-toolpad#1307

edit: pleasantly surprised to see you didn't have to resort to createRequire even once

@Janpot
Copy link
Member

Janpot commented Nov 8, 2022

Contents of the scripts/sizeSnapshot directory were not converted because they were used by Danger. The dangerfile can't be an ES module.

@michaldudak I notice the dangerfile runs async code. It's possible to dynamically import() ESM inside commonjs, this shouldn't be too hard to convert. (keep the dangerfile cjs, but all imports ESM)

https://nodejs.org/api/esm.html#import-statements

import statements are permitted only in ES modules, but dynamic import() expressions are supported in CommonJS for loading ES modules.

@michaldudak
Copy link
Member Author

@Janpot thanks for the tip! I'll try it out.

@Janpot
Copy link
Member

Janpot commented Nov 8, 2022

🤦 looks like danger is compiling it down to commonjs before running it

edit: danger/danger-js#1180

@michaldudak
Copy link
Member Author

Yep, it won't fly this way..

@michaldudak
Copy link
Member Author

I reverted the changes done to the sizeComparison directory. It's a CJS module again.
It's not problematic for now but will be if any of the dependencies these scripts use are converted to ESM only package.

@michaldudak
Copy link
Member Author

I also reverted changes to buildColorTypes. It's not really a proper ES module as it imports packages\mui-material\src\colors\index.js, which is a CJS module that uses export syntax and thus must be transpiled by Babel. The .mjs extension could cause confusion, so I decided not to use it.

@Janpot I assume there's no way to import such a not-ESM-but-also-not-CJS-either file in Node but if you happen to know any tricks, I'll be happy to learn.

@Janpot
Copy link
Member

Janpot commented Nov 9, 2022

The module packages\mui-material\src\colors\index.js is in ESM format, but the package @mui/material is not. When node tries to resolve @mui/material/colors, it can't find it because:

  • @mui/material is not ESM (no "type": "module" in package.json)
  • @mui/material doesn't have an export defined for colors in package.json

For this to resolve correctly, we first need to make the MUI packages proper ESM (I started that effort here, but it's gone a bit stale by now #30510, basically we need to add "type": "module" and "exports" in all package.json and make sure our import specifiers use file extensions everywhere)

It's possible to import CJS in ESM, so if the package has been built, the following works:

// @mui/material/build/colors/index.js is a commonjs module
import * as colors from '@mui/material/build/colors/index.js';

Not a great solution though, and eslint won't like it neither

@michaldudak
Copy link
Member Author

The module packages\mui-material\src\colors\index.js is in ESM format

The way I understand it is it doesn't have file extensions in exports, so it's not exactly a valid ES module. Babel does handle it, but Node needs file extensions.

@Janpot
Copy link
Member

Janpot commented Nov 9, 2022

it doesn't have file extensions

Right, good catch 👍.

edit: If I'm not mistaken, module resolution is not part of the ecmascript specification. It's valid ESM, node.js just doesn't know how to handle the specifier (Not that it matters in how we handle the problem here 🙂 )

@michaldudak
Copy link
Member Author

@LukasTy, @Janpot now that this is merged, you can go ahead and merge the PRs in your projects.

the-mgi pushed a commit to the-mgi/material-ui that referenced this pull request Nov 17, 2022
daniel-rabe pushed a commit to daniel-rabe/material-ui that referenced this pull request Nov 29, 2022
feliperli pushed a commit to jesrodri/material-ui that referenced this pull request Dec 6, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants