Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving bundled JavaScript with a pure AppHost implementation of ServiceStack

I would like to use ServiceStack as a pure AppHost implementation without using MVC. I want to use it to drive a Single Page App.

Serving the SPA's single HTML shell page is straightforward enough and I've found plenty of examples on doing that. However, I also need to serve a number of JavaScript files and I'm assessing the best way of doing this. I can simply put script tags in the HTML shell page but then I don't get the benefits of bundling and minification, and I would have to maintain this every time I add a new JavaScript file.

All these problems are solved with bundling solutions such as Cassette or ASP.NET MVC4 Bundles. But how would I use these with ServiceStack AppHost?

The ServiceStack.Bundler project is great but it seems to have dependencies on ASP.NET MVC, e.g. as a base for the HTML Helpers which render the JavaScript tags in the HTML.

I'd like to be able to do this without any dependency on MVC, if possible.

like image 448
Holf Avatar asked Mar 24 '23 22:03

Holf


1 Answers

If you haven't taken a look at GruntJS yet, it's worth a look (http://gruntjs.com/). By creating some simple tasks, it can combine & minify your HTML, JS, and CSS and has no dependency on .NET. There are a lot of other really useful tasks available to GruntJS as well (js lint checks, JS unit test running, and tons more). You can easily setup different tasks for your environments as well (ie, don't combine/minify when deploying to dev server).

What it allows you to do is create a purely static HTML, CSS, and JS SPA, and you can manage that in a completely different solution/project than your ServiceStack AppHost.

So in your example, you'd just reference the scripts in your index.html file like you normally would and when you're ready to deploy to staging/production you'd run your grunt task which would bundle/minify your code for you and output the static html, min.css, and min.js files for you to some deployment directory. It's really powerful and flexible.

I used to use Bundler and I recently made the switch to GruntJS and I haven't looked back.

like image 134
Mike Pugh Avatar answered Apr 06 '23 00:04

Mike Pugh