alexreardon / tiny-invariant

A tiny invariant function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question: how (should I) to replace a message with just a code in production

gcloeval opened this issue · comments

Hi,
This is more of a question for best practice - I am writing a library for the first time, and couldnt find much information and would like to hear your thoughts.

Both tiny-invariant and tiny-warning and working as expected - and they are indeed shaving off quite nice chunks of kbs from production build.

However, when some errors (in client apps that are using my library) do slip through development phase and happen in production, the error thrown as expected is just a generic invarianat failed with a stack trace pointing to the library.

Is there a mechanism wherby I could at least introduce/keep some error code i.e. E343 instead of a whole message being stripped away for production?
For ex, in development builds the message would be:
"E343: You forgot to wrap the components with the top level provider. Please ensure x, y, z"

And in production it would be just "E343?"

Does that make sense? I tried to investigate some of your libraries to see how you use them, but for example in react-beautiful-dnd, besides tiny-invariant, it seems you also have a more customized version "dev-warning.js" - Is dev-warning just early iteration before you came up with tiny-warning? Can you maybe provide some general guidelines/best practices on using these two for library authors and any gotchas you experiences?

It sounds like this would be possible. But you would need to create your own babel plugin (or similar).

Something like this: https://www.npmjs.com/package/babel-plugin-dev-expression

You might be able to write a babel macro to do it for you: https://github.com/kentcdodds/babel-plugin-macros

I'll close this for now. But happy for people to continue the discussion