cytoscape / cyjShiny

An R/shiny widget for cytoscape.js
Other
92 stars 28 forks source link

Use of `console.log()` #7

Open maxkfranz opened 5 years ago

maxkfranz commented 5 years ago

While useful for debugging, these should be removed at least in prod builds.

You can use NODE_ENV and the environment webpack plugin for this, e.g.

const log = function(){
  if(process.env.NODE_ENV === 'development'){
    console.log.apply(arguments);
  } else {
    // no logging in prod
  }
};

// in the code...
log('some debug message');

An approach like above should be used or the messages should be removed altogether.

paul-shannon commented 5 years ago

@maxkfranz great tip. thanks.

paul-shannon commented 5 years ago

@maxkfranz I implemented a simple version of your suggestion. Adequate? It avoids adding another npm module. Manipulation of development|production status is hard-coded, but perhaps not too awful.

var executionMode = "beta";
const log = function(msg)
{
  if(executionMode == "devel")
      console.log(msg);
}
maxkfranz commented 5 years ago

If you're fine with hard-coding that value, that's fine. Otherwise it's best to use something like process.env.NODE_ENV === 'development' with the webpack env plugin: https://webpack.js.org/plugins/environment-plugin/

NODE_ENV is the standard environment variable, but you could use something different if that would integrate better with the other things you're building in your Makefile.

paul-shannon commented 5 years ago

@maxkfranz I'll give it a try. webpack plugins are a little opaque to me at this point. Are these steps about right?

Am I close?

maxkfranz commented 5 years ago

use npm to install webpack.EnvironmentPlugin

It should be built-in

configure webpack.config.js to load that plugin by adding it to the module.exports.plugins array

yes

create a make target (in ~/github/cyjShiny/browserCode/makefile) which sets and exports the bash environment variable NODE_ENV=development|prod

yes

that value controls the logic of the new log function

yes

I don't understand the use of arguments in your log function, without obvious prior assignment

arguments is a keyword that can be used for variadic functions in js. It's an array-like object that contains all the arguments passed to the function. It's useful if your logging function accepts multiple arguments like console.log() does, e.g. console.log('this is the value of foo', foo).