4

I am working on an MVC 5 Web App. JavaScript code is being managed in these 3 different ways,

  1. Inline JavaScript code in the Views (To Read ASP.Net Server Side Variables i.e. ViewBag, ResourceFile etc.)

  2. Referencing some external JavaScript files (One JS File for Each View/Page).

  3. Rest of the JavaScript is being references by Bundles.

I want to minify #1 and #2, I don't think I should create a bundle for each page in my application as there are dozens of pages so that's why I think I cannot use MVC Bundling and Minificatin feature for this.

Can you please suggest a solution.

Thanks

2 Answers 2

2

Gulp and Grunt are becoming pretty standard industry tools for this. Latest versions of Visual Studio have extensions to support them now too.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't know much about those, but I think those are PackageManagers like NuGet, If that's true then, are you suggesting we should leave NuGet and move to Gulp or Grunt?
They do both. Using one to do both allows, for example, you to write a single script that would update all your packages and process, minify, and bundle your javascript files all in one go. Or you can stick to nuget, whatever works for you.
(further clarification: Gulp and Grunt are task runners that run plugins, with package managers and minifiers being two of the common kinds of plugins, but there are lots of other plugins that are quite helpful too).
Also, I believe Microsoft is deprecating the MVC bundling in ASP.NET 5--they're encouraging developers to use Grunt or Gulp or something similar instead.
Good info, +1. I will look into both of them.
1

You can not use MVC Bundling and Minification feature to minify inline js or external JavaScript files(This is one of the reasons not to use inline javascript).

Although if you still want to use inline JavaScript you can take a look at YUICompressor .NET or some other minifaction library that can help you with that.

Minifying external scripts doesn't make a lot of sense. Pretty much any publicly available JS has a minified version so instead of referencing a full version you can reference it directly. Other solution could be including those external scripts in your MVC project (adding them to the Scripts folder) and then using MVC Bundling and Minification feature on them

2 Comments

I don't like to use inline JavaScript, but what else can we do to access ViewBag/ResourceFile in JavaScript? YUICompressor .NET is a good idea actually, if it's not a legacy tool. Is it? "Minifying external scripts" does not mean "Minifying third party libraries", but "One JS module file for Each View/Page/". Last, are you sure that all files in Scripts folder get minified, no matter we are using them as a bundle or not? Thanks for your time.
Is YUICompressor .NET a legacy tool - I have no answer for this but I know that it's widely used. "One JS module file for Each View/Page/" - I would construct bundles based from scripts that are supposed to be used together as one module for example validation bundle could include both jquery validation and jquery unobtrusive validation scrips. are you sure all files in Scripts folder get minified - They are not getting minified - but if you include them in bundles and enable optimization yoir bundles will be minified. You can minify scripts manually by using Web Essentials plugin as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.