Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have different javascript files for debugging and production in web application?

I'd like to have different versions of .js javascript files in my ASP.NET WebForms application (VS 2008):

  • the full-blown version, which comments and so on
  • the minified version, for production

Is there a way to automatically get the full-blown version while debugging and the minified version when the application is deployed to production? I don't mind if the answer is a hack.

Is it possible in VS 2010?

like image 509
JotaBe Avatar asked Oct 19 '25 14:10

JotaBe


1 Answers

Of course it is possible, but the question here is "how you going to set it up."

I start with the key here that is the DEBUG definition. So let say that you like to set it up on the page.

So on page you can do something like:

<% #if DEBUG %>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<% #else %>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<% #endif %>

Alternative, you can use a Literal and make this switch on code behind.

<asp:Literal runat="server" ID="txtScripts" EnableViewState="false" />

and

protected void Page_Load(object sender, EventArgs e)
    {
#if DEBUG
        txtScripts.Text = "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js\"></script>";
#else
        txtScripts.Text = "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></script>";
#endif
    }

Other way is to use a handler and read all the javascript files and make minified using code, and use again the same key DEBUG to send the one or the other version.

Personally I use the MS Ajax MInified http://www.asp.net/ajaxlibrary/AjaxMinDocumentation.ashx I read all my javascript files, make them one file, and use this parameters to minified them or not.

Minifier MyMin = new Minifier();
CodeSettings cs = new CodeSettings();
#if DEBUG
    cs.MinifyCode = false;
    cs.OutputMode = OutputMode.MultipleLines;
    cs.PreserveFunctionNames = true;
    cs.RemoveFunctionExpressionNames = false;
    cs.RemoveUnneededCode = false;
    cs.StripDebugStatements = false;
#else                   
    cs.MinifyCode = true;
    cs.OutputMode = OutputMode.SingleLine;              
#endif
Write(MyMin.MinifyJavaScript(AllMyJavascript, cs))
like image 104
Aristos Avatar answered Oct 22 '25 02:10

Aristos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!