react-styleguidist: Examples not displayed when using react-scripts@5
Current behavior
Examples in .md files is not displayed when using react-styleguidist with react-scripts@5.
To reproduce
https://github.com/malcolm-kee/react-styleguidist-cra5-md-bug
Expected behavior
The examples should be displayed.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 22
- Comments: 15
Commits related to this issue
- Add workaround to display markdown examples gh: https://github.com/styleguidist/react-styleguidist/issues/1910#issuecomment-1013763698 — committed to ThomasRoest/styleguidist-test by deleted user 2 years ago
Hello Any plans to fix this issue? Still happens on v11.2.0 for me
Same for me
Problem
I debugged this a bit and the problem here seems to be, the way that Webpack 5 is handling assets. It has a new Asset Module mechanism, which determines how webpack will bundle/treat an import. For Markdown files this seems to be
asset/resourceby default, i.e. it will “copy” the file to a hashed location and make therequirereturn the URL to it.This is problematic, since react-styleguidist actually specifies a custom inline loader (see
getExamples.ts#31andexamples-loader.ts). This loader will return JavaScript, which is now handled by Webpack wrongly as an asset. The solution would be to inform webpack to treat that result as JavaScript via the asset typejavascript/auto(see Workaround below).Also it seems there is a second problem, that in the JavaScript code, that the examples-loader generate absolute paths are used (see
examples-loader.ts#19-20). Those seem to no longer resolve properly and will cause those two modules (requireInRuntimeandevalInContext) to not resolve properly. It seems relative paths would work fine (see workaround below).Workaround
Tested with
react-styleguidist@11.1.7To workaround this, you can put the following into your
styleguide.config.js:Couple of notes to this workaround:
webpackConfigin yourstyleguide.config.js(e.g. because you are using craco and want to inject its webpack config) make sure to properly merge therulesandpluginsinto that config..examples.md(using thegetExampleFilenameconfiguration). You must make sure to write atestthat will match your example markdown files (and also not accidentally other markdown files if you have them in your code). If you can’t achieve this with thetestoption, in the worst case you’d need to use theincludeandexcludeoption to include and exclude individual files.For those struggling to hook up existing webpack config with the workaround, adding the below to the styleguide.config.js worked for me. It’;s not an ideal solution but works for me while waiting for this to get resolved on the styleguidist end.
Thanks @timroes for this ❤️
It took me some time to make this work with Typescript and Tailwind CSS but it was totally worth it!
In case someone is interested here is a gist for that.
Hey there. By ‘the rest of your config’ I am referring to any other configuration you use from here. Things like sections, styles etc. You’re right, you do need to import
webpack, since you are referring to it in the config overrides. You can see more examples of config for react-styleguidist in the gist that @nebomilic posted above 😃