Skip to content

[docs-infra] Add recursively the relative modules in the demos#44150

Merged
mnajdova merged 20 commits intomui:masterfrom
mnajdova:docs/demos-fix
Oct 30, 2024
Merged

[docs-infra] Add recursively the relative modules in the demos#44150
mnajdova merged 20 commits intomui:masterfrom
mnajdova:docs/demos-fix

Conversation

@mnajdova
Copy link
Copy Markdown
Member

@mnajdova mnajdova commented Oct 18, 2024

BRREAKING CHANGE

For the demos that used relative imports, make sure that you have a TypeScript version of the relative modules too, as the infra was updated to look for TypeScript files in the TypeScript version of the demo. In Core, the only usage was in the Autocomplete component related to the top100films.js file. You can likely easily search for this in the docs/data directory of your projects.


This PR adds support for complex demos that need a specific folder structure in order to improve the collocation of the files in the demos. I copied the Advanced ListView demo from MUI X.

List of needed changes to enable this feature:

  • update the markdown loader logic to include the relative modules of all files used in the demo (not just the root demo file)
  • a small change in the CodeSandbox & Stackblitz to update imports statements of all files, not just the Demo.js|tsx file
  • updated the docs:typescript:formatted script to create a JS version for all TypeScript files, not just the component ones

I've tested that:

  • all files are present in the file tabs for both the JS and TS version of the demos
  • CodeSandbox export works as expected
  • Stackblitz export works as expected

TODO:

  • Remove the test demo before merging

@mnajdova mnajdova added the scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305). label Oct 18, 2024
@mui-bot
Copy link
Copy Markdown

mui-bot commented Oct 18, 2024

Netlify deploy preview

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

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 07f45cf

Comment thread docs/scripts/formattedTSDemos.js Outdated
Comment thread docs/scripts/formattedTSDemos.js
Comment thread docs/src/modules/sandbox/CodeSandbox.ts Outdated
content: curr.raw,
content: flattenRelativeImports(
curr.raw,
demoData.relativeModules?.map((file) => file.module),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do the same as the Demo.js file as we now allow relatives imports in all files.

newCode = newCode.replace(pathWithoutExtension, newPath);

// Replace the relative import towards parent directory
newCode = newCode.replace('../', './');
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We need to replace nested imports toward the parent directory, e.g. a file importing from "../utils". Not sure if this is the best way to fix this, but I am open for suggestions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I got confused when writing this piece of code. I thaught path would be the path as expressed in the file. But it's the path from the root of the Demo.

  1. We could move the replace outside of the forEach by using a replaceAll. Currently, it works because we apply this Replace once per file existing in the demo

  2. Another option would be to use similar Regexp as the one to compute newPath

newCode = newCode.replace(/from '.*[\\/]/g, `from './`);

It replaces any sequence of . and / to support multiple levels of nested components (I hope we
will not reach that level 🙈)

And the from ' prevent replacing a ../ that could be present in the comments

Comment thread docs/src/modules/sandbox/StackBlitz.ts Outdated
...acc,
// Remove the path and keep the filename
[`${curr.module.replace(/^.*[\\/]/g, '')}`]: curr.raw,
[`${curr.module.replace(/^.*[\\/]/g, '')}`]: flattenRelativeImports(
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same change as in the CodeSandbox file.

}
}
}
return relativeModuleFilename;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I decided to update the method to return the file as it is needed when iterating the relative modules.

} catch {
throw new Error(
`Could not find a module for the relative import "${relativeModuleID}" in the demo "${demoName}"`,
for (const variant of variants) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This only iterates through ['JS', 'TS']

@mnajdova mnajdova marked this pull request as ready for review October 24, 2024 12:52
Copy link
Copy Markdown
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

I left few comments, but for me it's good.

I just wait for the removal of the test demo before approving to be sure nobody merge it by mistake 🙈

Comment thread docs/scripts/formattedTSDemos.js Outdated
Comment thread docs/scripts/formattedTSDemos.js
newCode = newCode.replace(pathWithoutExtension, newPath);

// Replace the relative import towards parent directory
newCode = newCode.replace('../', './');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I got confused when writing this piece of code. I thaught path would be the path as expressed in the file. But it's the path from the root of the Demo.

  1. We could move the replace outside of the forEach by using a replaceAll. Currently, it works because we apply this Replace once per file existing in the demo

  2. Another option would be to use similar Regexp as the one to compute newPath

newCode = newCode.replace(/from '.*[\\/]/g, `from './`);

It replaces any sequence of . and / to support multiple levels of nested components (I hope we
will not reach that level 🙈)

And the from ' prevent replacing a ../ that could be present in the comments

Comment thread packages/markdown/loader.js
@mnajdova
Copy link
Copy Markdown
Member Author

@alexfauquette all comments were resolved in separate commits, you can checke the changes commit per commit. Everything was working as expected (I've tested after all the changes). I noticed one breaking change tough, I described it in the PR description. I am tagging @bharatkashyap too to be aware for it for the Toolpad project. Tagging also @michaldudak I am not sure if this affects the new Base UI docs, but just in case.

@mnajdova mnajdova added the breaking change Introduces changes that are not backward compatible. label Oct 25, 2024
Copy link
Copy Markdown
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Nice reduction of the flattenRelativeImports 👍

BY the way @bharatkashyap this PR might close some of the followup issues

Copy link
Copy Markdown
Collaborator

@bharatkashyap bharatkashyap left a comment

Choose a reason for hiding this comment

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

What a great improvement! Thanks for working on this. Added some small suggestions

Comment thread packages/markdown/loader.js
Comment thread packages/markdown/loader.js Outdated
Comment thread packages/markdown/loader.js Outdated
Comment thread packages/markdown/loader.js Outdated
Comment thread packages/markdown/loader.js Outdated
@michaldudak
Copy link
Copy Markdown
Member

For the record, the demo loader used in Base UI docs has this functionality built-in already, so once we propagate it to other repos, it should still work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Introduces changes that are not backward compatible. scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants