javascriptc#jqueryasp.net-mvcmicrosoft-ajax-minifier

Ajax Minifier Breaks javascript on minification


I am using ajax minifier to reduce code requests and HTML overhead, and so far its working fine with CSS file. But when I try to use the minified output of js files my HTML page stops working.

Here are the js files I include in combined output

<script src="/content/template/js/AuswellScript/jquery-1.11.0.min-f1cd7227-4b48-423d-ba00-6a814fc588e3.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/bootstrap.min-54c76ae6-1272-464d-b3b4-e5c8e13cd2ad.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/own-menu-d7c1ca2a-6a7e-4bc3-b7e9-f51e92136f20.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/owl.carousel.min-5ea2a8c2-1a05-4f29-aca3-b71f936aaa25.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.superslides-d8526db0-813c-4372-9699-3c829e696ccc.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/masonry.pkgd.min-8f6e02f7-17a3-442f-a7db-a2f68ec24cb2.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.stellar.min-f9b2d588-2c90-489a-ab22-800f41f7df17.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery-ui-1.10.3.custom-0406d77e-4eed-4e42-b1ef-370e24378d13.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.magnific-popup.min-7e2779cb-0844-4b67-acae-2a1053cb0401.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.isotope.min-0148ab04-7187-43c6-accf-7d7ffd4224fa.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.flexslider-min-ec6844aa-338c-4ce9-a67c-eaff0948809d.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/appointment-a7ff2a91-9cfb-4dc9-95a0-585033435e12.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/Slider-568c53bd-f8a3-440a-b2d4-1969699f1d7b.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.downCount-8ba17b7f-82ca-4b65-a29e-517b3e3d51af.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/counter-08aa3c8c-b65f-4efb-842b-dbbd019a11ce.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/jquery.prettyPhoto-d3012702-c0cc-439a-8b7e-22e0d2211a15.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/main-4310f5f3-278c-463e-bc98-edbbc307fbe9.js" type="text/javascript"></script>
<script src="/content/template/js/AuswellScript/bootbox.min-8eea52d3-baa1-47fd-b4df-5be51462a2e9.js" type="text/javascript"></script>

the generate combined file gets saved as

<script src="http://auswell2.xoxo-it.com/content/template/js/AuswellScript/combined.min.js" type="text/javascript"></script>

but using this file vs. separate file is causing error $ not defined on my page, I deleted and recreated combined out several times to ensure no sequence or incorrect data was making the issue.

here is the c# code

Minifier min = new Minifier();
StringBuilder minified = new StringBuilder();
script.head_files.ForEach(js =>
{
    if (!string.IsNullOrWhiteSpace(js))
        minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js))));
});
if (minified.Length > 0)
{
    var minJS = Server.MapPath("~/content/template/js/" + objHeads.head_name + "/combined.min.js");
    System.IO.File.WriteAllText(minJS, minified.ToString());
}

Solution

  • I've pasted the code in jsfiddle, clicked on "tidy", then copied the tidied output to babel repl ... seems like that's a lot of places that are missing a ; - probably "between" the input files - is there a way with the minifier to specify you want to add a ; to the beginning (or end) of each input file?

    perhaps add minified.Append(";"); after minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js))));

    i.e.

    Minifier min = new Minifier();
    StringBuilder minified = new StringBuilder();
    script.head_files.ForEach(js => {
        if (!string.IsNullOrWhiteSpace(js)) {
            minified.Append(min.MinifyJavaScript(System.IO.File.ReadAllText(Server.MapPath(js))));
            minified.Append(";"); // add this
        }
    });
    if (minified.Length > 0)
    {
        var minJS = Server.MapPath("~/content/template/js/" + objHeads.head_name + "/combined.min.js");
        System.IO.File.WriteAllText(minJS, minified.ToString());
    }