Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"if debug" in JavaScript?

Is there anything in JavaScript or Visual Studio to detect if the code is used in debug-mode? Something like "#if DEBUG" in C#, but for JavaScript?

like image 380
Martin Avatar asked Mar 12 '14 06:03

Martin


Video Answer


1 Answers

A bit late, but I needed the same and could not give up until a viable solution.

I have a kind of "main" javascript file, where I have a line like:

Site.DEBUG = false;

Then in the code I can check for this constant. Now I needed to solve that at build time, some automation would set this for me according to project configuration. Here I've found fnr.exe command-line tool for find and replace in files. It's a quite good piece of utility, would be worth to check out anyway. So at this point I've created a folder in the project directory called BuildScripts, I've copied the fnr.exe file into it, and created a batch file like this.

switch_client_debug.bat

REM Params: path to folder, filename, change-DEBUG-from-this, to-this
fnr.exe --cl --dir "%1" --fileMask "%2" --caseSensitive --showEncoding --find "DEBUG = %3" --replace "DEBUG = %4"

Then I defined the corresponding pre-build events at the web project like this:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts false true

and its pair at Release config:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts true false

Now everything works like a charm and I can have logging, tracing, special logic for Debug and for Release configuration in Javascript.

like image 124
Zoltán Tamási Avatar answered Sep 20 '22 19:09

Zoltán Tamási