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

[code-infra] Update package layout for better ESM support #43264

Draft
wants to merge 54 commits into
base: master
Choose a base branch
from

Conversation

Janpot
Copy link
Member

@Janpot Janpot commented Aug 11, 2024

closes #44055
closes #43980

Add a new mode to the build:

  • Removes nested package.json files
  • Stores ESM files with .mjs extension
  • Add exports field to package.json
  • Leave icon package layout as is, but add exports field. Next time we generate it we'll have to update the exports field
  • Use rollup instead of babel to compile the modules. this makes it fully specify imports (made this optional for now)

Notes:

To Do:

Bundle size increase

The bundle size increase can be fully attributed to the inclusion of the following ESM/CJS interop helper in the webpack output:

Code
/******/ 	/* webpack/runtime/create fake namespace object */
/******/ 	(() => {
/******/ 		var getProto = Object.getPrototypeOf ? (obj) => (Object.getPrototypeOf(obj)) : (obj) => (obj.__proto__);
/******/ 		var leafPrototypes;
/******/ 		// create a fake namespace object
/******/ 		// mode & 1: value is a module id, require it
/******/ 		// mode & 2: merge all properties of value into the ns
/******/ 		// mode & 4: return value when already ns object
/******/ 		// mode & 16: return value when it's Promise-like
/******/ 		// mode & 8|1: behave like require
/******/ 		__webpack_require__.t = function(value, mode) {
/******/ 			if(mode & 1) value = this(value);
/******/ 			if(mode & 8) return value;
/******/ 			if(typeof value === 'object' && value) {
/******/ 				if((mode & 4) && value.__esModule) return value;
/******/ 				if((mode & 16) && typeof value.then === 'function') return value;
/******/ 			}
/******/ 			var ns = Object.create(null);
/******/ 			__webpack_require__.r(ns);
/******/ 			var def = {};
/******/ 			leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];
/******/ 			for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {
/******/ 				Object.getOwnPropertyNames(current).forEach((key) => (def[key] = () => (value[key])));
/******/ 			}
/******/ 			def['default'] = () => (value);
/******/ 			__webpack_require__.d(ns, def);
/******/ 			return ns;
/******/ 		};
/******/ 	})();

This is caused by the following import

const maybeReactUseSyncExternalStore: undefined | any = (React as any)['useSyncExternalStore' + ''];

A potential switch to using use-sync-external-store has been explored in #43476 but ultimately it makes sense to keep it, it manifests in the webpack runtime.

@Janpot Janpot added the scope: code-infra Specific to the core-infra product label Aug 11, 2024
@Janpot Janpot changed the title [core] Try new build mode that creates package exports [core] Update package layout for better ESM support Aug 11, 2024
@mui-bot
Copy link

mui-bot commented Aug 11, 2024

Netlify deploy preview

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

Menu: parsed: +1.74% , gzip: +1.90%
Unstable_NumberInput: parsed: +4.98% , gzip: +5.77%
@material-ui/core/useMediaQuery: parsed: +5.13% , gzip: +4.61%
Option: parsed: +6.75% , gzip: +7.22%
MenuItem: parsed: +6.47% , gzip: +6.62%
Select: parsed: +1.45% , gzip: +1.55%
Tab: parsed: +7.22% , gzip: +7.37%
PigmentHidden: parsed: +1.10% , gzip: +1.02%
TabPanel: parsed: +12.28% , gzip: +11.45%
useAutocomplete: parsed: +5.06% , gzip: +5.46%
TablePagination: parsed: +7.54% , gzip: +8.23%
@material-ui/unstyled: parsed: +0.43% , gzip: +0.40%
@material-ui/system: parsed: +0.77% , gzip: +0.71%
Tooltip: parsed: +0.52% , gzip: +0.48%
@mui/joy/MenuList: parsed: +0.74% , gzip: +0.59%
@mui/joy/Tooltip: parsed: +0.61% , gzip: +0.54%
@mui/joy/Radio: parsed: +0.80% , gzip: +0.63%
@mui/joy/Menu: parsed: +0.48% , gzip: +0.50%
@mui/joy/Checkbox: parsed: +0.78% , gzip: +0.58%
@mui/joy/Drawer: parsed: +0.65% , gzip: +0.56%
and 24 more changes

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 32a2605

@Janpot Janpot changed the title [core] Update package layout for better ESM support [code-infra] Update package layout for better ESM support Aug 12, 2024
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 12, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 23, 2024
"import": "./esm/*.js",
"require": "./*.js"
},
"./esm/*": {
Copy link
Member Author

Choose a reason for hiding this comment

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

Maintain backwards compatibility for users that have done

resolve: {
alias: [
{
find: /^@mui\/icons-material\/(.*)/,
replacement: '@mui/icons-material/esm/$1',
},
],
},


const exportedNames = new Set(Object.keys(packageExports));
for (const exportedName of exportedNames) {
const modernName = exportedName.replace(/^\./, legacyModernPrefix);
Copy link
Member Author

Choose a reason for hiding this comment

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

Maintain backwards compatibility for the legacy ./modern bundles. Just a re-export of the main exports, but with a ./modern prefix

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 3, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 4, 2024
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 23, 2024
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 27, 2024
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: out-of-date The pull request has merge conflicts and can't be merged scope: code-infra Specific to the core-infra product
Projects
None yet
3 participants