2

I have a master file which has references to the JS and the CSS file used for my website. These references are pointing to the non-crunched version of the files. Now, when I hit the Publish button through Visual Studio for my project I want to change the references in my master file to point to the crunched version of the JS and CSS.

Eg: During development if it was pointing to http://www.example.com/main.js, during publish it should change the reference to http://www.example.com/main_min.js. Is there a way to do this?

Also, before changing the reference I need to run my current js file(main.js) through the tool which outputs the crunched js file(main_min.js).

Any help on this is appreciated!

Thanks.

Sampat
  • 1,301
  • 6
  • 20
  • 34

2 Answers2

1

You could go to the project's properties and in the Build Events tab enter the following into the "Post-build event command line":

$(SolutionDir)..\YourJsCruncher.exe $(ProjectDir)\content\js\debug\ $(ProjectDir)\content\js\release\

and then have a custom HTML helper:

public static MvcHtmlString IncludeJs(this UrlHelper helper, string javascriptFile)
{
#if DEBUG
    var subfolder = "debug";
#else
    var subfolder = "release";
#endif
    var path = helper.Content("~/Content/js/{1}/{2}.js", subfolder, javascriptFile);
    return MvcHtmlString.Create(string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", path));
}

and then in your view:

<%= Url.IncludeJs("foo.js") %>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

This question has most of the answers you need:

Concatenate and minify JavaScript on the fly OR at build time - ASP.NET MVC

Customizing the publich process for one click publish is a little trickier. The best I could answer is linking to this blog: http://vishaljoshi.blogspot.com/ When I was working with one-click-publish customization this is the place I found the most answers. If I remember correctly this wasn't trivial and its better to integrate this into your build process like the answers in the first question I link explain.

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • Thanks jfar,The first link talks about minifying js files but I could not find any answers/examples to writing something on publish process to change the reference in the master pages in the other link. – Sampat Jan 25 '11 at 04:58