zertosh / invariant

invariant
MIT License
1.22k stars 70 forks source link

Typical use case isn't well documented #21

Open markrian opened 7 years ago

markrian commented 7 years ago

It'd be useful if the README referred to babel-plugin-dev-expression, or similar, as the way to actually strip out the messages passed to invariant when NODE_ENV=production.

As it is, the source simply states:

    * The invariant message will be stripped in production, but the invariant
    * will remain to ensure logic does not differ in production.

but doesn't explain how that actually happens.

The README doesn't clear it up either; it says how to install invariant, but that alone won't result in the intended benefit of this module, that is, fewer bytes in production builds.

Unless I'm missing something?

ywongweb commented 7 years ago

I am trying to use this in a react-native project. I've added babel-plugin-dev-expression and in .babelrc -> plugins added babel-plugin-dev-expression. But get the error below when running the app.

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3099): SyntaxError: TransformError: /Users/aaa/dev/bbb-app/node_modules/react-native/packager/src/Resolver/polyfills/prelude_dev.js: /Users/aaa/dev/bbb-app/node_modules/react-native/packager/src/Resolver/polyfiBundlingindex.ios.js``

Anyone know why?

react-native: v0.42.3

zebulonj commented 6 years ago

Came here looking for the same thing (what's the recommended approach for removing the messages in production builds). Only thing that immediately comes to mind:

function broadcast( message ) {
  invariant( typeof message === 'string', process.env.NODE_ENV !== 'production' ? "Message must be defined." : undefined ); 
  // Do some stuff here...
}

Is there not a better way?

zebulonj commented 6 years ago

Found this babel transform: https://github.com/bloodyowl/strip-invariant

Suspect Facebook is doing something similar (stripping message arguments in build), but don't have time to investigate at the moment.

flasd commented 6 years ago

shameless plug I've written a babel plugin that solves this. It's based on the transform @zebulonj mentioned, but re-written for babel v6 and v7. Plus a few customization options :wink:

https://github.com/husscode/babel-plugin-strip-invariant :rocket: