Comments (12)
I think the goal is to make it impossible to statically analyze. I saw a screenshot where React used Math.random()
to trick bundlers, and it seems like the only future-proof method. This should hopefully work forever, regardless of new tricks bundlers perform:
React[`useId${Math.random()}`.slice(0, 5)]
The other technique that might be future proof is:
const _React = {...React};
const useReactId = _React.useId;
from material-ui.
I'm getting the same issue but with useSyncExternalStore
from material-ui.
@patrykszwed Could you also provide a minimal repro for the repo where you finally use this built package ?
from material-ui.
@brijeshb42 sure thing. I've updated the one linked in the description. It is enough to:
cd final-app
- Run
npm i
- Run
npm run build
A warning about useId
import should appear, and some additional ones about useInsertionEffect
with the same message. Apparently useInsertionEffect
causes a similar issue. Here are the logs:
Logs
➜ final-app git:(main) npm run build
[email protected] build
webpack
asset bundle.js 421 KiB [emitted] [minimized] [big] (name: main) 1 related asset
orphan modules 417 KiB [orphan] 1 module
runtime modules 221 bytes 1 module
cacheable modules 684 KiB
modules by path ../ 134 KiB
modules by path ../node_modules/react/ 7.63 KiB 4 modules
modules by path ../node_modules/react-dom/ 119 KiB 2 modules
modules by path ../node_modules/scheduler/ 4.91 KiB 2 modules
../node_modules/object-assign/index.js 2.06 KiB [built] [code generated]
modules by path ./ 550 KiB
modules by path ./node_modules/react-dom/ 119 KiB 2 modules
modules by path ./node_modules/react/ 6.48 KiB 2 modules
modules by path ./node_modules/scheduler/ 4.91 KiB 2 modules
./src/index.js + 1 modules 417 KiB [built] [code generated]
./node_modules/object-assign/index.js 2.06 KiB [built] [code generated]
WARNING in ../lib/esm/index.js 1864:24-48
export 'useInsertionEffect' (imported as 'React') was not found in 'react' (possible exports: Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, useCallback, useContext, useDebugValue, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, version)
@ ./src/index.js 2:0-56 4:42-54
WARNING in ../lib/esm/index.js 1864:51-75
export 'useInsertionEffect' (imported as 'React') was not found in 'react' (possible exports: Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, useCallback, useContext, useDebugValue, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, version)
@ ./src/index.js 2:0-56 4:42-54
WARNING in ../lib/esm/index.js 2431:22-34
export 'useId' (imported as 'React6') was not found in 'react' (possible exports: Children, Component, Fragment, Profiler, PureComponent, StrictMode, Suspense, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, cloneElement, createContext, createElement, createFactory, createRef, forwardRef, isValidElement, lazy, memo, useCallback, useContext, useDebugValue, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useReducer, useRef, useState, version)
@ ./src/index.js 2:0-56 4:42-54
WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
Assets:
bundle.js (421 KiB)
WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
main (421 KiB)
bundle.js
WARNING in webpack performance recommendations:
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/
webpack 5.90.3 compiled with 6 warnings in 8831 ms
Here's a link to the repo itself - mui-repro-lib. Let me know in case you need anything else and thanks for looking into this.
from material-ui.
Thanks. I will investigate.
Edit: @patrykszwed Can you update the demo app to be runnable in dev mode. Right now, it just renders Cannot GET /
with a server error. Ideally, I should be able to see the Autocomplete component or some error in the browser console.
from material-ui.
@brijeshb42 sure, I've added and setup a dev
script so you should be able to run npm run dev
now. It will run the app with errors unless Autocomplete
import and usage is removed/commented out in the index.js
file. These changes are merged into the repo and the sandbox.
from material-ui.
Reproduction
I can reproduce on https://esbuild.github.io/try/#dAAwLjE5LjYALS1taW5pZnkAcmV0dXJuICdzZWYnLnRvU3RyaW5nKCk7
Screen.Recording.2024-02-25.at.16.15.33.mov
This is since esbuild v0.19.6 (Nov 19, 2023).
Context
So If I understand correctly:
- Webpack warns logic isn't smart enough: webpack/webpack#14814
- Webpack doesn't resolve 'a' + 'b'. webpack/webpack#14814 (comment) so we used it
- But esbuild <v0.19.6 does resolve resolve 'a' + 'b': #37182, so we used ''.toString();
- Now esbuild resolves ''.toString().
- Terser still doesn't support ''.toString() https://try.terser.org/
![SCR-20240225-opxl](https://private-user-images.githubusercontent.com/3165635/307603091-4bd1454e-24e3-4195-8415-fb4238efaf45.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0OTM5ODQsIm5iZiI6MTcxODQ5MzY4NCwicGF0aCI6Ii8zMTY1NjM1LzMwNzYwMzA5MS00YmQxNDU0ZS0yNGUzLTQxOTUtODQxNS1mYjQyMzhlZmFmNDUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTVUMjMyMTI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZmQzNDY0OWFjOTM4NzBjMWViN2Y5N2FhNzQ5Y2E4NjYyZGQ4MWMwZGY1MmNlNjgyYzVlMmNmYzI5OGZkYzljMyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.92Eit2tkGYvcRnDuq6kpcRzzLpOeruncnUAVslETQTE)
- Next.js uses SWC as a minifier, but it looks super dumb so we are good
![SCR-20240225-oqwp](https://private-user-images.githubusercontent.com/3165635/307603362-ebb81526-6eb3-4b98-a7a6-a6e75869619e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0OTM5ODQsIm5iZiI6MTcxODQ5MzY4NCwicGF0aCI6Ii8zMTY1NjM1LzMwNzYwMzM2Mi1lYmI4MTUyNi02ZWIzLTRiOTgtYTdhNi1hNmU3NTg2OTYxOWUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTVUMjMyMTI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OTRlMGU5YmZlM2M0NWQ3ODM2YjAxNjYwZGFkNmFlN2Y0ODE2MzNhYzdiYjJjNGM3MTJmYWE4MmY3NjAzYjhiZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.8r-RxJASG4dDCckK-rbERw1nu6YOCMN1ZGNpH18B3dI)
Options
- Maybe
atob('dXNlSWQ=')
would do it, but can have limitations on the server: mui/mui-x#11046. - This is resolved by Terser but not esbuild
const use = 'use';
return `${use}Id`;
- The simplest seems to be
'useId'.toString(10)
. Per #37183 (comment), we should fixuseId
anduseSyncExternalStore
.
cc @atomiks too as https://github.com/floating-ui/floating-ui/blob/495954da10d2abe8a5e6a0f0247582f071d6f031/packages/react/src/hooks/useId.ts#L30 is a dependency of @mui/base
![SCR-20240225-pafg](https://private-user-images.githubusercontent.com/3165635/307605085-066df8bf-a251-4b2a-adc3-b96514628437.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0OTM5ODQsIm5iZiI6MTcxODQ5MzY4NCwicGF0aCI6Ii8zMTY1NjM1LzMwNzYwNTA4NS0wNjZkZjhiZi1hMjUxLTRiMmEtYWRjMy1iOTY1MTQ2Mjg0MzcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTVUMjMyMTI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YTVhYzAyMmUwMDdlOTk1YzgzNWY5NjdhZTE4MTZhYTg2YTQwNDdmMWJlNGRiZTJhMTExMzM2OTc3MjVhYmQxZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.r_IXGOXVXdIkJoy55K1SXWmQn4t21rufvVqITflvzBY)
https://npm.anvaka.com/#/view/2d/%2540mui%252Fbase
![SCR-20240225-ooqf](https://private-user-images.githubusercontent.com/3165635/307602837-950cebcf-f4e6-4c81-a3e3-f1aa778b3567.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0OTM5ODQsIm5iZiI6MTcxODQ5MzY4NCwicGF0aCI6Ii8zMTY1NjM1LzMwNzYwMjgzNy05NTBjZWJjZi1mNGU2LTRjODEtYTNlMy1mMWFhNzc4YjM1NjcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYxNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MTVUMjMyMTI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NWQ0YzFhZGI4OGQyNThjNGVlZGUxMTIzYjMxYWQzYzI3ZTRiM2IyMzUyMTJjMzU2MTIxMWUzZGVhZjllYmIxMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.l5WP1RYpwuUQcRKDYRVpw_kE8_R8zpTQ2EaICTLJVKc)
@brijeshb42 The @mui/utils package is mostly an npm dependency of Base UI, I fixed the labels (a bit MUI System too but it's rarer). I fixed this.
from material-ui.
cc @atomiks too as https://github.com/floating-ui/floating-ui/blob/495954da10d2abe8a5e6a0f0247582f071d6f031/packages/react/src/hooks/useId.ts#L30 is a dependency of @mui/base
useId
is not part of @floating-ui/react-dom
, only @floating-ui/react
, so the dependency won't affect @mui/base
.
So simply passing an argument like 'useId'.toString(0)
is what works now? There's a chance more bundler minifier updates will break it in the future, so maybe an alternative method is worth looking into.
Here's how Ariakit does it:
import * as React from "react";
// See https://github.com/webpack/webpack/issues/14814
const _React = { ...React };
const useReactId = _React.useId;
from material-ui.
I've also verified that React['useId '.trim()]
works but it'll be a cat and mouse game. We'll need to also get the similar issue fixed in emotion
related to its usage of useInsertionEffect
as that is also part of the error right now.
from material-ui.
@patrykszwed Just curious, why not keep @mui/material
as external when building your own package ?
from material-ui.
@brijeshb42 our setup is pretty wild and we're using yarn
in the app repo while using npm
in the components package. When working locally, we're using link
s to link the local package version.
Juggling between different webpack and rollup configurations results, in most cases, in either:
Invalid hook call.
error (multiplereact
versions detected). We solve this by specifying an alias forreact
in our webpack config, that is:alias: { react: require.resolve('react') }
- Or when defining
@mui/material
as an external dependency -Module not found: Error: Can't resolve 'react/jsx-runtime' in '{MY_LOCAL_PATH}/node_modules/@mui/system/esm/cssVars'
. This one can be solved by removing analias
forreact
, but once that's done we're going back to the first issue. We've been trying to fix that by using webpackfallback
and/oralias
options but without luck so far.
from material-ui.
Hey @brijeshb42, I just wanted to ask if there's a plan to fix that issue in the nearest future? I took a look into this PR in the radix-ui
repo. It looks like the solution proposed by its author (calling .trim()
before the .toString()
call) works so it could potentially be reused here as well.
esbuild v0.19.6:
https://esbuild.github.io/try/#dAAwLjE5LjYALS1taW5pZnkAcmV0dXJuICdzZWYnLnRyaW0oKS50b1N0cmluZygpOw
esbuild v0.20.2:
https://esbuild.github.io/try/#dAAwLjIwLjIALS1taW5pZnkAcmV0dXJuICdzZWYnLnRyaW0oKS50b1N0cmluZygpOw
from material-ui.
Related Issues (20)
- [Breadcrumbs] Allow overriding `onClick` button base of ellipsis icon
- [Alert] TS error when using slotProps with customised component HOT 4
- [react 19][tooltip] Accessing element.ref was removed in React 19
- [material-ui] Prototype pollution security vulnerabililty in @mui/utils HOT 8
- [material-ui] Sign-in template improvements
- [material-ui] Dashboard template improvements
- [material-ui][Masonry] layout forms a single column when the first item is hidden HOT 4
- MUI Joy UI Autocomplete crashing my app
- [Snackbar][docs] Stop recommending the usage of notistack, the library is seemingly no longer (properly) maintained
- Include the fix for pollution vulnerability in deepmerge in version 5 HOT 3
- ChromeVox unable to linearly navigate into Dialog
- SwipeableDrawer Flickering behavior HOT 2
- Autocomplete: Impossible to set aria-label on html input of TextField HOT 1
- [Stack] Allows clearing of `margin-left` property of child items.
- Material Icons has bug in syntax HOT 2
- [material-ui] Add `PropsProvider` for Pigment CSS integration
- [docs] Popover flip HOT 3
- Boolean Value Not Rendered in MUI Typography Component HOT 1
- Typograhpy - Text Formatting - Please add native support for configurable text highlighting to override native <mark> tag
- [docs] Sign-in side template image not loading (503 error) HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from material-ui.