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] Use custom webpack loader for markdown #26774

Merged
merged 18 commits into from
Jun 25, 2021
Merged

Conversation

eps1lon
Copy link
Member

@eps1lon eps1lon commented Jun 15, 2021

Problem: Previous usage of require-context with inline loaders is no longer supported in webpack 5 (webpack/webpack#12900)
Solution: Replace with a single import + custom loader that fakes require.context

Preview: https://deploy-preview-26774--material-ui.netlify.app/components/tables/

Ignore 1a6775e since this is just the change from using npx @codemod/cli docs/pages --remote-plugin https://astexplorer.net/#/gist/5a4cf1d9e15657695ae270f5c7819b2c/2e489edc6b0014506602f34c069d6428b448e1e5 (https://astexplorer.net/#/gist/5a4cf1d9e15657695ae270f5c7819b2c/2e489edc6b0014506602f34c069d6428b448e1e5)

Additional benefits:

  1. parsing markdown at compile time
  2. less boilerplate in the docs pages (at the cost of magically creating variables via loader)
  3. Enables Import markdown components on demand (e.g ComponentLinkHeader is always imported in MarkdownDocs even though it might now be used. The code is not tree-shakeable at the moment)
  4. hot reloading of markdown (getInitialProps did not automatically update the page when markdown changed)

Isn't this tying us more to webpack?
Just as much as we were tied before since require.context is webpack specific.

TODO:

  • convert to async loader
  • apply to all require.context('!raw-loader!') usages
  • render markdown directly in the loader

Follow-Up:

  • Remove requireRaw mock (needs work in prepareMarkdown)
  • Import markdown components (like ComponentLinkHeader) on demand

@eps1lon eps1lon added docs Improvements or additions to the documentation performance labels Jun 15, 2021
@mui-pr-bot
Copy link

mui-pr-bot commented Jun 15, 2021

No bundle size changes (experimental)

Generated by 🚫 dangerJS against 8ab8bec

@eps1lon eps1lon marked this pull request as ready for review June 15, 2021 20:24
@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 15, 2021

As a side note, we broke HMR support for the markdown of the page a while ago. I don't think that this change will help or harm this aspect, I'm only raising it as it's may be relevant in the future.

Is reading the markdown on its editor good enough? I don't think so, nor that it's close to good enough. HMR of the markdown allows to work on the actual content developers will see when consuming the content. Losing sight of this is a good way to write markdown that looks OK on its editor but KO once rendered (e.g. title too long to fit, wrong code indentation, overwhelming code sections, etc.). I personally frequently reload the page (it's slower), to make sure it's OK, as I work on the content. It would be interesting to see if other contributors have the same frustration.

@eps1lon
Copy link
Member Author

eps1lon commented Jun 15, 2021

Yep that's a problem. But it improves user experience by a lot which is more important. For live reloading of markdown you could always use the preview in your code editor (vscode has this built-int).

@eps1lon eps1lon marked this pull request as draft June 16, 2021 14:54
@eps1lon
Copy link
Member Author

eps1lon commented Jun 16, 2021

Makes more sense to move rendering markdown to the loader in this PR as well. Some pages don't follow the pages/somePage <-> src/pages/somePage mapping. The follow-up would be just as big and these concerns are tightly coupled anyway.

@eps1lon eps1lon changed the title [docs] Use custom webpack loader for loading demos [docs] Use custom webpack loader for markdown Jun 16, 2021
@eps1lon eps1lon force-pushed the docs/demo-loader branch 4 times, most recently from ef7625f to e94dc96 Compare June 16, 2021 15:53
@eps1lon eps1lon marked this pull request as ready for review June 16, 2021 16:43
@eps1lon
Copy link
Member Author

eps1lon commented Jun 17, 2021

I don't think that this change will help or harm this aspect, I'm only raising it as it's may be relevant in the future.

Pushed "render markdown at bundle time" to make it clearer that hot reloading is going to be improved. Changes to parseMarkdown will not trigger hot-reloading but changing the markdown content will.

@eps1lon
Copy link
Member Author

eps1lon commented Jun 25, 2021

Has been open for a while. Will merge after the updating the branch and then we'll see if we discover any problems.

@eps1lon eps1lon merged commit 98050d7 into mui:next Jun 25, 2021
@eps1lon eps1lon deleted the docs/demo-loader branch June 25, 2021 07:00
Comment on lines +194 to +195
// FIXME: Revert before merging
if (process.env.PULL_REQUEST === 'false' && !l10nPRInNetlify && !vercelDeploy) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm taking care of it #26970.

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.

@eps1lon It looks pretty cool! 💯 for the HMR of the markdown. I hold on to the release because I wanted to try dynamic pages with Next.js, and see how this change fits into the equation, but I didn't get the time, so it's fine.

Why do I mean by dynamic page? It seems inefficient to build each API page (>100) when it always renders the same type of content. So my thought was: why don't we create one dynamic /api page. This way, we can build it once, saving x minutes (to be measured) of builds and dynamically render the .json of the different components. We didn't touch the API pages, so 🙆‍♂️

@oliviertassinari
Copy link
Member

The changes seem to have broken the docs in development on Windows: #21053 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants