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

Typical use case isn't well documented #21

Open
markrian opened this issue Dec 16, 2016 · 4 comments
Open

Typical use case isn't well documented #21

markrian opened this issue Dec 16, 2016 · 4 comments

Comments

@markrian
Copy link

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
Copy link

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/polyfiBundling index.ios.js``

Anyone know why?

react-native: v0.42.3

@zebulonj
Copy link

zebulonj commented Mar 9, 2018

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
Copy link

zebulonj commented Mar 9, 2018

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
Copy link

flasd commented Jun 14, 2018

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 😉

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants