codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Release Mode #102

Closed ForbesLindesay closed 8 years ago

ForbesLindesay commented 8 years ago

I would like to add a "Release" mode, intended for people to use when releasing code flow to npm. I would like it to only add typechecks for arguments (not return types etc.). I would also like to wrap all typechecks in:

if (process.env.NODE_ENV !== 'production') {
  // ...typechecks here...
}

That way, most people would get the benefit of helpful type errors in development, but not have the overhead in production, since you can compile away the process.env.NODE_ENV then do dead-code elimination. Currently I have to publish two versions (dev and prod) of the same npm module to offer this flexibility.

phpnode commented 8 years ago

@ForbesLindesay to accomplish a similar thing I've been creating two builds of my modules and then deciding which to include in an entry point, so my folder structure looks like this:

lib
lib-checked
src
index.js

and index.js looks like:

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./lib');
}
else {
  module.exports = require('./lib-checked');
}

The issue with your suggestion is that most people don't process node_modules with babel and so DCE won't happen. The VM can't do it because process.env.NODE_ENV can change at any time, so the overhead would still be there.

ForbesLindesay commented 8 years ago

In this example though, for client side, you're shipping users (who don't use babel/envify/similar) two complete copies of the code, which is surely worse?

ForbesLindesay commented 8 years ago

I suppose more people use tools like envify/babel on their client side dependencies than for server side node_modules though, so maybe this is the right option.

phpnode commented 8 years ago

I suppose more people use tools like envify/babel on their client side dependencies than for server side node_modules though

Exactly, people deploying to the client are much more likely to be using tools which can do this.