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

Prod Mode #6

Closed
rtfeldman opened this issue Nov 4, 2014 · 9 comments · Fixed by #29
Closed

Prod Mode #6

rtfeldman opened this issue Nov 4, 2014 · 9 comments · Fixed by #29
Assignees

Comments

@rtfeldman
Copy link
Owner

In production, it's probably unnecessary to actually freeze objects and throw exceptions when mutating methods are invoked. Those are mainly necessary in development, but by the time the code hits production, if any invariants aren't being followed, you should already know about it.

With that in mind, having a way to turn off the freezing and overriding should speed up performance, particularly in Safari. Adding a quick flag to turn this on would be useful to that end.

@rtfeldman rtfeldman self-assigned this Nov 4, 2014
@rtfeldman rtfeldman mentioned this issue Nov 4, 2014
@alanhogan
Copy link

sounds legit

@diegovilar
Copy link

Agreed. Those constraints are mostly not supposed to be runtime errors and should be catch in development.

@crudh
Copy link
Collaborator

crudh commented Mar 19, 2015

I'm interested in working on this but I am a bit unsure on how to pass the flag to the library?

Would you use the export code:

  if (typeof module === "object") {
    module.exports = Immutable;
  } else if (typeof exports === "object") {
    exports.Immutable = Immutable;
  } else if (typeof window === "object") {
    window.Immutable = Immutable;
  } else if (typeof global === "object") {
    global.Immutable = Immutable;
  }

To do different things depending on the environment, like (using global and window as examples):

  var prodMode = false;

  if (typeof module === "object") {
    module.exports = Immutable;
  } else if (typeof exports === "object") {
    exports.Immutable = Immutable;
  } else if (typeof window === "object") {
    window.Immutable = Immutable;
    prodMode = window.SEAMLESS_PROD_MODE === true;
  } else if (typeof global === "object") {
    global.Immutable = Immutable;
    prodMode = global.SEAMLESS_PROD_MODE === true;
  }

Or is there a better way?

@RangerMauve
Copy link

What about a method on Immutable that looks like Immutable.setProduction(true/false), and then have people explicitly enable it and define their own way of detecting production mode?

@rtfeldman
Copy link
Owner Author

Awsesome!

The best practice I've seen (which is what React does) is to make it envify-friendly.

Basically you do this:

if (process.env.NODE_ENV === "development") {
  // do some stuff we wouldn't do in prod mode
}

...then run envify on it twice, once with {NODE_ENV: "development"} and once with {NODE_ENV: "production"} to get two output files: seamless-immutable.development.js and seamless-immutable.production.min.js

In the development build, envify will replace the above with this:

if ("development" === "development") {
  // do some stuff we wouldn't do in prod mode
}

....whereas in the production build, it will instead replace it with this:

if ("production" === "development") {
  // do some stuff we wouldn't do in prod mode
}

Uglify will then see that this conditional will always fail and will strip this code out entirely, meaning the production build will not only avoid adding the extra attributes, it won't even run any conditionals to tell if it's in prod mode!

This implicitly means we'd need to introduce a build process to generate the two different files for library consumers.

@alanhogan
Copy link

I've been wondering about how to do this kind of thing for ever. Very nice solution. No non-JS syntax needed.

Alan

On Mar 19, 2015, at 2:27 PM, Richard Feldman notifications@github.com wrote:

Awsesome!

The best practice I've seen (which is what React does) is to make it envify-friendly.

Basically you do this:

if (process.env.NODE_ENV === "development") {
// do some stuff we wouldn't do in prod mode
}
...then run envify on it twice, once with {NODE_ENV: "development"} and once with {NODE_ENV: "production"} to get two output files: seamless-immutable.development.js and seamless-immutable.production.min.js

In the development build, envify will replace the above with this:

if ("development" === "development") {
// do some stuff we wouldn't do in prod mode
}
....whereas in the production build, it will instead replace it with this:

if ("production" === "development") {
// do some stuff we wouldn't do in prod mode
}
Uglify will then see that this conditional will always fail and will strip this code out entirely, meaning the production build will not only avoid adding the extra attributes, it won't even run any conditionals to tell if it's in prod mode!

This implicitly means we'd need to introduce a build process to generate the two different files for library consumers.


Reply to this email directly or view it on GitHub.

@crudh
Copy link
Collaborator

crudh commented Mar 30, 2015

Should we then move seamless-immutable.js to a src directory to indicate that it shouldn't be used directly? And place the output files of envify in the root?

@rtfeldman
Copy link
Owner Author

That makes sense to me. Let's not check in changes to the compiled files on every commit, though, as that will make the diffs miserable.

Instead just leave them out of normal commits, and once per release I'll include updated compiled files in the commit that bumps the version number.

Sound good?

@crudh
Copy link
Collaborator

crudh commented Mar 31, 2015

Sounds good!

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

Successfully merging a pull request may close this issue.

5 participants