How to get source code unmodified, except for stripping out code inside of false Defines?

Sep 29, 2012 at 1:32 AM

So I have code like:

 

///#IFDEF CDN

var path = "http://cloudfront.net/";

///#ELSE

var path = "/u/";

///#ENDIF

 

If I just use this code as-is in a browser obviously it will fail - path is declared twice. When debugging, I want the source, but I want the defines here handled so that one of these 2 code paths is stripped out, but nothing else is done to the file. How do I do that?

 

This is a follow-up to my bad bug report:

http://ajaxmin.codeplex.com/workitem/18721

Coordinator
Sep 29, 2012 at 1:42 AM

If you want it truly, completely untouched other than evaluating the preprocessor stuff, the EXE has the -pponly option (Preprocess only). Unfortunately, you'll have to wait for the next release (4.68) for the Minify object to have that capability. I actually needed to do just that with the DLL myself earlier this week and was surprised I couldn't. So the update has been made; I just haven't shipped it yet.

But if stripping comments is okay, you can use the -p option and it will set up all the necessary flags to perform hardly anything at all on the source. But it does strip the comments. And probably a few other extremely minor things (like removing nested blocks).

Or you can skip the Minify class altogether and use JSParser directly, and perform the -pponly functionality yourself. There's a PreprocessOnly method on JSParser that takes a CodeSettings object and returns the preprocessed code as a string.

Sep 29, 2012 at 1:46 AM

So it sounds like I can get this functionality right now in code with the Minifier, if I'm OK with losing comments. What do I set to accomplish that? -p on the command line, but what CodeSettings flag do I set to what value to accomplish it in code?

Thanks!

Coordinator
Sep 29, 2012 at 1:58 AM
  • OutputMode to MultipleLines
  • LocalRenaming to KeepAll
  • KillSwitch to ~((long)(TreeModifications.PreserveImportantComments))  (I think that's -2)

I was going to suggest always using the SwitchParser class to generate CodeSettings objects, but I just noticed the -p option is setting Minify to false, too! Holy cow, that's terrible. I'll have to fix that for 4.68, because -p is generating the same issue you were seeing.

 

 

 

Sep 29, 2012 at 4:50 AM

Thanks - I tried this and it works as intended. The KillSwitch value is madness.

Oct 3, 2012 at 2:11 PM

Looking forward to 2.68 - I'm having the same issue - I presume this also effects the removal of debug?

e.g. when I do -p -debug: false.. then debugging stuff is left in

I am looking forward to being able to just strip out debug and do pro-processor statements, that will be awesome. Thanks.

Coordinator
Oct 3, 2012 at 4:43 PM

@lukepage: because the -p switch sets all the kill switches except Important Comments, it was also turning off the preprocessor because there was a kill-switch for that. There was actually a previous check-in (#94461) that was included with the 4.67 release that took the preprocessor out of the kill-switches altogether for precisely this reason. 4.67 still has the problem with named function expression names getting left off, though. That's coming in 4.68.