Question

When working with web applications I often have code that is specifically designed for production/server vs local environments.

I also often need to deactivate specific functions (ie js, ruby, rails, java etc).

The problem I'm facing is that sometimes when releasing new version I miss to activate or inactivate these features.

The ideal thing (I think) would be to have like on/off function that you run before deploying to a server. So you don't miss anything.

Example.

I comment out a function when developing.

JS:

// foobar() FOR PRODUCTION

Ruby On Rails (production.rb):

# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # FOR PRODUCTION

The latter, is when I need to test something local with production settings but because I don't have specific environment installed (nginx, appache) I can't use it's features.

I use Sublime Text 2,3 and it would be great if it had a feature defining specific functions/code blocks to be uncommented/commented.

How do you work so you won't miss anything?

Was it helpful?

Solution

This question is not specific to Sublime Text at all, or any text editor, but a generic question how to preprocess and debug (JavaScript) source code.

First, the easiest approach would be have some kind of global debug flag in your codebase.

  window.DEBUG = true;

  function log(a, b, c, d) {
       if(DEBUG) {
           console.log(a, b, c, d);
       }
  } 

  if(DEBUG) {
      // Do dev stuff
  } else {
     // Do produection stuff
  }

  log("foobar") // Does not need to be wrapper, as log() itself is functional only in debug mode

You can set the debug flag depending on your URL:

 if(window.location.href.indexOf("localhost") >= 0) {
     window.DEBUG = true;
 } 

When you deploy your JavaScript to production, you usually run it through minimizing and bundling tool. One popular tool is UglifyJS 2 which features constant elimination. When running and deploying your JavaScript code with this tool, if(DEBUG) { } parts of your code gets eliminated.

You can connect any tool to Sublime Text using Project > Build scripts.

For the server-side code, you simply need to set debug flag depending on your environment. Each framework (Ruby on rails) have their own way of doing this, and you need to ask specific details in framework specific question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top