Code Monkey home page Code Monkey logo

Comments (12)

atomiks avatar atomiks commented on June 28, 2024 2

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.

misteroh10302 avatar misteroh10302 commented on June 28, 2024 1

I'm getting the same issue but with useSyncExternalStore

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

from material-ui.

brijeshb42 avatar brijeshb42 commented on June 28, 2024

@patrykszwed Could you also provide a minimal repro for the repo where you finally use this built package ?

from material-ui.

patrykszwed avatar patrykszwed commented on June 28, 2024

@brijeshb42 sure thing. I've updated the one linked in the description. It is enough to:

  1. cd final-app
  2. Run npm i
  3. 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.

brijeshb42 avatar brijeshb42 commented on June 28, 2024

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.

patrykszwed avatar patrykszwed commented on June 28, 2024

@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.

oliviertassinari avatar oliviertassinari commented on June 28, 2024

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:

SCR-20240225-opxl
  • Next.js uses SWC as a minifier, but it looks super dumb so we are good
SCR-20240225-oqwp

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 fix useId and useSyncExternalStore.

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://npm.anvaka.com/#/view/2d/%2540mui%252Fbase


SCR-20240225-ooqf

@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.

atomiks avatar atomiks commented on June 28, 2024

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.

brijeshb42 avatar brijeshb42 commented on June 28, 2024

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.

brijeshb42 avatar brijeshb42 commented on June 28, 2024

@patrykszwed Just curious, why not keep @mui/material as external when building your own package ?

from material-ui.

patrykszwed avatar patrykszwed commented on June 28, 2024

@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 links to link the local package version.

Juggling between different webpack and rollup configurations results, in most cases, in either:

  1. Invalid hook call. error (multiple react versions detected). We solve this by specifying an alias for react in our webpack config, that is: alias: { react: require.resolve('react') }
  2. 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 an alias for react, but once that's done we're going back to the first issue. We've been trying to fix that by using webpack fallback and/or alias options but without luck so far.

from material-ui.

patrykszwed avatar patrykszwed commented on June 28, 2024

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
image

esbuild v0.20.2:
https://esbuild.github.io/try/#dAAwLjIwLjIALS1taW5pZnkAcmV0dXJuICdzZWYnLnRyaW0oKS50b1N0cmluZygpOw
image

from material-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.