GithubHelp home page GithubHelp logo

Prod Mode about seamless-immutable HOT 9 CLOSED

rtfeldman avatar rtfeldman commented on July 20, 2024
Prod Mode

from seamless-immutable.

Comments (9)

alanhogan avatar alanhogan commented on July 20, 2024

sounds legit

from seamless-immutable.

diegovilar avatar diegovilar commented on July 20, 2024

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

from seamless-immutable.

crudh avatar crudh commented on July 20, 2024

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?

from seamless-immutable.

RangerMauve avatar RangerMauve commented on July 20, 2024

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?

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

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.

from seamless-immutable.

alanhogan avatar alanhogan commented on July 20, 2024

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 [email protected] 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.

from seamless-immutable.

crudh avatar crudh commented on July 20, 2024

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?

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

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?

from seamless-immutable.

crudh avatar crudh commented on July 20, 2024

Sounds good!

from seamless-immutable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.