-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[docs] Support demos with side effect imports #35177
[docs] Support demos with side effect imports #35177
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
It might make sense to create a simple test for it in docs/packages/markdown/extractImports.test.js
, not sure.
I wonder if there isn't a way to share the same abstraction with
material-ui/docs/src/modules/sandbox/Dependencies.ts
Lines 117 to 124 in 04d2a26
const re = /^import\s'([^']+)'|import\s[\s\S]*?\sfrom\s+'([^']+)/gm; | |
let m: RegExpExecArray | null = null; | |
// eslint-disable-next-line no-cond-assign | |
while ((m = re.exec(raw))) { | |
const fullName = m[2] ?? m[1]; | |
// handle scope names | |
const name = | |
fullName.charAt(0) === '@' ? fullName.split('/', 2).join('/') : fullName.split('/', 1)[0]; |
It feels like these two functions aim for saving the same problem.
I don't understand. It seems that by only upgrading the monorepo in |
@m4theushw I covers a bit of the why in:
Try to make an edit in the field, and the demo will still be broken. |
For reference, this is what we use in toolpad: https://github.com/mui/mui-toolpad/blob/3ef124ab46e4ba0682665b6cc93b87d6fc0434d9/packages/toolpad-app/src/utils/strings.ts#L80-L104 |
Is the regexp ready or do we need to wait ? |
@Janpot Thanks! I managed to reuse them here by doing slightly modifications to ignore capturing groups, characters from template strings and spaces. One thing to note: the regex from Toolpad is matching spaces when the import name is |
We were specifically looking to match on any import specifier (e.g. anything between ""). The idea being that it's up to subsequent validation to verify whether it can resolve to a module or not. |
In the X repo there's a demo that imports a locale from
dayjs
, but this import is not used along the code. This causes the demo to crash because: 1. the demos are loaded with react-runner; which 2. requires all imports to be specified at front, but since the regex that searches the imports to be loaded doesn't match imports not used along the code the module can't be found.Side note: Today I learned that this kind of import is called "side effect import" by MDN.
Related to mui/mui-x#6369 (review)