hipstersmoothie / react-docgen-typescript-plugin

A webpack plugin to inject react typescript docgen information
MIT License
68 stars 29 forks source link

Stale __docgenInfo generated due to cache #28

Open feichao93 opened 3 years ago

feichao93 commented 3 years ago

Thanks for the library, it's very helpful when developing a React component library doc site.

https://github.com/hipstersmoothie/react-docgen-typescript-plugin/blob/abe4c74f104212b1d32e7698eab3301fb2b8dbb6/src/plugin.ts#L99-L109

webpackModule._source._value seems to be the output of the ts-loader. So when user only edits the comments in a TS interface, webpackModule._source._value will keep same as before ( because the whole interface code is stripped by ts-loader) . In this case, the produced __docgenInfo will be incorrect, and the doc page which consumes __docgenInfo will display wrong interface comments.

hipstersmoothie commented 3 years ago

This is the same problem as this issue but the cache was introduced after this bug was seen. So i'm not sure it's the caching that is going wrong. If you comment out the caching code the problem can still be seen

Me and @ndelangen tried debugging this to no avail. If you place a console log here you can see that the docs get generated correctly, it just doesn't trigger webpack to reload.

If you can find a solution I will happily merge it!

feichao93 commented 3 years ago

I think the problem is at this line, here cached is stale since the original source code has been changed, though webpackModule._source._value keeps the same due to ts-loader.

In my own project, if I comment out cache-related code in the plugin, no stale __docgenInfo will be generated. I can provide a re-production repo later.

image

This cache makes it worse because the cache is written to disk, so even user restart webpack they may still get stale __docgenInfo.

hipstersmoothie commented 3 years ago

You if you could to ids a reproduction repo that would be great.

You could also try out 0.6.3 before I added the caching

hipstersmoothie commented 3 years ago

But yeah I could see how this would make the problem worse. Is there a way to get the original source value? That would make the cache better

feichao93 commented 3 years ago

webpackModule._source.node().sourceContents[webpackModule.userRequest] can do the work, but I'm not sure whether it is the correct way to do so.

image

feichao93 commented 3 years ago

reproduce demo: https://github.com/shinima/temp-react-docgen-typescript-plugin-issue28

steps to reproduce and debug are listed in readme

feichao93 commented 3 years ago

There are cases that the user edits some types.tsx / interfaces.tsx file which exports some base interfaces that the component extends. The __docgenInfo of the component should be updated even if the source code of component does not change. In such cases, cache is really hard to design/implement... 🤔

An option to disable the cache can be a workaround. I don't known there is a better way to handle this.