Software Archive
Read-only legacy content
17060 Discussions

preprocessor #ifdef?

Thomas_V_1
Beginner
344 Views

Hi, 

I want to remove all  my debug code and comments before launching. Maybe also sort versions and different platforms.  Is there a preprocessor #ifdef function implemented in xdk or JS, Html, Less? Or is there another way I can do this?

Any experiences? 

Thanks

0 Kudos
6 Replies
Ad
New Contributor III
344 Views

Unfortunately JS has no preprocessor functionality. I's not a compiled language live Java or C# and XDK doesn't do anything with your JS files.

There are a few external options: http://stackoverflow.com/questions/7376131/ifndef-in-javascript
(and maybe more...)

In our project we use a global variable: debugMode and use it in our JS code, but that doesn't work for comments....

 

0 Kudos
Thomas_V_1
Beginner
344 Views

Thanks for your answer. I hope XDK will implement such a function. I would make the apps smaller and faster. I think this would be very welcome by the community. 

 

 

0 Kudos
Thomas_V_1
Beginner
344 Views

I guess it has to be made like special comments otherwise jslint etc would not work. 

0 Kudos
Chris_P_Intel
Employee
344 Views

While JS does not itself support pre-processing, there are lots of preprocessing tools that can remove debug code, comments, etc.  Checkout UglifyJS,  Browserify,  Sweet.js and Google "Javascript minification".    You'll probably also encounter Grunt and Gulp. 

One or more of these tools will do what you need. Sweet.js is the most powerful, and is probably way more than you need, and difficult.  UglifyJS is straightforward and very popular.  

Good luck,

Chris

0 Kudos
Thomas_V_1
Beginner
344 Views

Thanks Chris this is really helpful,

I looked at UglifyJS and the dead_code flag will do the job. 

How do I do this in XDK? My intuitive idea is to make a script that runs UglifyJS and saves the output in a parallel project. Then I test the parallel project. Is this the way?

 

 

0 Kudos
PaulF_IntelCorp
Employee
344 Views

We don't have a good way to do it automatically for you, something like what you are describing could work. There has been talk of adding some scripts directories as part of the project that could be used for such tasks, but that has only been discussion, no commitment at this point.

One guy described in another thread that he keeps two source directories in the project, for example: "dev" and "www" He does his main work in "dev" and then runs scripts that convert and copy his files for production into the "www" directory. He has to change the "source directory" option on the Projects tab to switch between the two setups. See this image for a location of the option that allows you to switch the tag. You have to push the folder icon to make the change.

Screen Shot 2015-07-30 at 7.57.02 AM.png

0 Kudos
Reply