Your partner in IT success

A specialized company in providing high quality consultancy on analysis, architecture and development projects

 
22
Apr

A contribution to SquishIt

I’m using SquishIt, a friendly ASP.NET JavaScript and CSS squisher, on different projects (on this web site too). I love the library: it’s so easy to use, thanks to the fluent API, and the results are great. CSS and JavaScript files are combined and minified in one file at runtime.

The only drawbacks I could mention is that SquishIt is not perfectly integrated with ASP.NET MVC. I recently decided to contribute to the library and to publish a NuGet package to propose a better integration with MVC projects.

To use it, it’s quite simple, just install SquishIt.Contrib.Mvc with the package manager or by adding a library package reference inside Visual Studio. Then, edit the web.config file located under the Views directory. Locate the system.web.webPages.razor section and modify the pageBaseType attribute to SquishIt.Contrib.Mvc.SquishItBasePage.

Now, inside the layout and the views, you have access to a new helper: SquishIt. Use it to add the CSS and the JavaScript you’ll like to include in your web site. See this example:

_Layout.cshtml

@{
    SquishIt.Css.Add("~/Content/Site.css");
    SquishIt.JavaScript.Add("~/Scripts/jquery-1.4.4.min.js");
}
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
 
    @SquishIt.Css.Render("~/Content/Site.min.css")
</head>
 
<body>
    @RenderBody()
 
    @SquishIt.JavaScript.Render("~/Scripts/Site.min.js")
</body>
</html>

Index.cshtml

@{
    SquishIt.Css.Add("~/Content/Home.css");
    SquishIt.JavaScript.Add("~/Scripts/jquery-ui.js");
    
    ViewBag.Title = "Index";
}
 
<h2>Index</h2>

And voila, a nice integration of SquishIt with ASP.NET MVC!

Don’t wait a minute, and give a try to SquishIt.Contrib.Mvc. I’ll read your comment with pleasure!

Posted by Fabian Vilers, Consulting Principal

Comments

2 years ago, Jonas Hovgaard said:
gravatar

Nice contribute, thanks a lot! Is the source available? I really wanna add .ForceRelease() :-)

2 years ago, Fabian Vilers said:
gravatar

Jonas, I'm glad that that you like it. Releasing the source code is on the schedule; just a matter of time (I'm quite busy on my day to day work). Feel free to follow me on twiter (@fvilers) for more news about it.

last year, Alex said:
gravatar

I can't see a way of specifying the media.. is this excpected?

last year, Fabian Vilers said:
gravatar

Sorry, but there's no way to specify the media. I think this is a limitation of SquishIt itself and not the contribution package but I'll verify this assumption.

By the way, thanks for your feature idea, I'll implement it as soon as possible.

last year, Alex said:
gravatar

I think it can... might be a later release though: https://github.com/jetheredge/SquishIt/issues/1

last year, Fabian Vilers said:
gravatar

OK, I'll check for that but I can't give estimation on delivery (my right hand is broken atm :-( )

last year, Alex Brown said:
gravatar

No worries, no rush! If you wanted to put the source on GitHub, I'd gladly assist

Hope your hand gets better!

last year, Fabian Vilers said:
gravatar

Opening the source code is planned (Jonas already asked me about that) as soon as I can use a keyboard without making a typo every two letters ;-)

last year, Alex said:
gravatar

Ok cool, If you need any help, feel free to let me know. Always willing to lend a hand. Excuse the pun.....

last year, Laurent said:
gravatar

Very nice, I love it, but there is one important problem with it. In debug, the script tags are outputted in the opposite order that the files were added. For example, this:

SquishIt.JavaScript.Add("~/js/jquery.js");
SquishIt.JavaScript.Add("~/js/jquery.validate.js");
SquishIt.JavaScript.Add("~/js/jquery.validate.unobtrusive.js");

Generates this:

&gt;script type="text/javascript" src="/js/jquery.validate.unobtrusive.js"&lt;&gt;/script&lt;&gt;script type="text/javascript" src="/js/jquery.validate.js"&lt;&gt;/script&lt;&gt;script type="text/javascript" src="/js/jquery.js"&lt;&gt;/script&lt;

This is very problematic. I don't know if it does the same thing in a deployed environment.

last year, Fabian Vilers said:
gravatar

Thanks Laurent for your feedback. This is "by design" as the files you add to the collection are popped out in the reverse order (a stack, behind the stages). This is useful when you add library files (like jquery) in your layout and specific library (those who uses jquery for example) in the views as the layout is processed after the views.

Btw, I'm working on a new version that fixes this oddity by explicitly allow order in the added files.

Stay tuned (I've my right hand still broken, and on holidays in a few days, don't expect anything new before Augustus).

last year, Bryan said:
gravatar

Can you explain why the Add methods take a string array? What are we supposed to do with those? Also, is there any support for CDNs?

last year, Fabian Vilers said:
gravatar

It accepts an array of string because you can specify different files with the same method call.

last year, Javier said:
gravatar

Thank you very much for this! I look forward to more updates particularly the order change.

last year, Rodrigo Dumont said:
gravatar

Thanks for this great complement to SquishIt! But I second that request for the source. When do you think it will be released?

last year, Fabian Vilers said:
gravatar

Thanks, I did not had the time do it theses days but I should plan to do it. I have a huge release to do this week, I promise to publish the code source as soon as it is launched!

last year, Fabian Vilers said:
gravatar

Dear commenters, I'm glad to announce that I've shared the source code to GitHub: https://github.com/fvilers/SquishIt.Contrib.Mvc.

Don't hesitate to comment as it is the first time I'm using GitHub ;-)

last year, Jason said:
gravatar

If i already have base page(I am using mvc.contrib ) for my views how could i use quishIt.Contrib.Mvc?

last year, Kurt said:
gravatar

Great Project!

Are you planning to change the method signatures, of the Render methods, to return a MvcHtmlString as opposed to string?

last year, Fabian Vilers said:
gravatar

Hi Kurt, I'm not maintening this anymore as I'm working on a fully revamped "squisher" library for MVC.

last year, fendi said:
gravatar

hi, do you have an ETA for this new 'squisher' library?

last year, Fabian Vilers said:
gravatar

I published something under NuGet knows as MvcOptimizationPack. Still in beta and probably abandoned as the new ASP.NET stack will have its own bundler manager.

Add your comment