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

[docs] Support demos with side effect imports #35177

Merged
merged 4 commits into from Nov 22, 2022

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Nov 16, 2022

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.

image

Side note: Today I learned that this kind of import is called "side effect import" by MDN.

Related to mui/mui-x#6369 (review)

@m4theushw m4theushw added the docs Improvements or additions to the documentation label Nov 16, 2022
@mui-bot
Copy link

mui-bot commented Nov 16, 2022

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

No bundle size changes

Generated by 🚫 dangerJS against 76e4765

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work scope: docs-infra Specific to the docs-infra product labels Nov 16, 2022
Copy link
Member

@oliviertassinari oliviertassinari left a 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

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.

@m4theushw
Copy link
Member Author

I don't understand. It seems that by only upgrading the monorepo in master solved the problem: https://deploy-preview-6864--material-ui-x.netlify.app/x/react-date-pickers/localization/

@m4theushw m4theushw marked this pull request as draft November 16, 2022 22:55
@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 16, 2022

I don't understand. It seems that by only mui/mui-x#6864 the monorepo in master solved the problem:

@m4theushw I covers a bit of the why in:

Now, it uses the v1 of the live edit, the v2 makes the demo work when no edits are done. So, the bug will be less visible. TL:DR updating the @mui/monorepo will fix a large chunk of the issue.

mui/mui-x#6369 (review)

Try to make an edit in the field, and the demo will still be broken.

@Janpot
Copy link
Member

Janpot commented Nov 17, 2022

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
We have a very basic test suite that you can copy if you want https://github.com/mui/mui-toolpad/blob/3ef124ab46e4ba0682665b6cc93b87d6fc0434d9/packages/toolpad-app/src/utils/strings.spec.ts#L3

@flaviendelangle
Copy link
Member

Is the regexp ready or do we need to wait ?

@m4theushw
Copy link
Member Author

@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 "module-2\s\s" (GitHub doesn't allow me to use 2 spaces). This can be fixed adding \s to the list of characters that shouldn't be matched.

image

@m4theushw m4theushw marked this pull request as ready for review November 22, 2022 16:56
@Janpot
Copy link
Member

Janpot commented Nov 22, 2022

One thing to note: the regex from Toolpad is matching spaces when the import name is "module-2\s\s"

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.

@m4theushw m4theushw merged commit 77b40eb into mui:master Nov 22, 2022
@m4theushw m4theushw deleted the support-side-effect-import-in-demos branch November 22, 2022 21:45
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
bug 🐛 Something doesn't work docs Improvements or additions to the documentation scope: docs-infra Specific to the docs-infra product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants