5

I have set up a simple test using ASP.NET's bundling and minification feature. I have two very simple js files:

~/JS/Site.js

(function echo1() {
    alert("this is site.js");
})();

~/JS/test.js

(function echo2(value) {
    alert("and this is test.js");
})();

I've created a bundle as follows:

bundles.Add(new ScriptBundle("~/bundles/scripts/site-globals").Include(
    "~/JS/Site.js", 
    "~/JS/test.js"));

and referenced the bundle on the page using @Scripts.Render("~/bundles/scripts/site-globals")


When I run the site under debug (<compilation debug="true" targetFramework="4.5" /> in web.config) I get the expected result - two alert boxes show one after the other.

However, when I run change to release (<compilation debug="false" targetFramework="4.5" />) - I only see the first alert box, and the combined JS file that gets sent to the browser has completely ignored the content of test.js.

The "combined and minified" output is as below, and wrongly only includes the code from Site.js:

(function(){alert("this is site.js")})()

Any ideas on why this is happening would be much appreciated! Thanks

2
  • Try renaming the second js file, there is some special handling for certain filenames and I wonder if "test" has a special meaning. Just a guess. Double check that casing on the name matches, although usually on a windows platform that doesn't matter. Commented Dec 28, 2012 at 17:01
  • @AaronLS - thanks for your suggestions - I have tried using "firstthing.js" and "secondthing.js" and checked the case but it still doesn't work unfortunately. I think there must be something dodgy with my solution because I can't replicate this in an empty project. Even removing all other scripts/css/bundles doesn't seem to change anything though :S Commented Jan 6, 2013 at 14:49

2 Answers 2

5

I've found out what was causing this problem. If any of the javascript files have a comment as the last line, they will be combined together without a newline, causing the first line of the next file to be commented out.

Here's a link to another question on stack overflow which demonstrates this: https://stackoverflow.com/a/14223945/11459631

In my case, I was using the Web Essentials Visual Studio plugin to minify my javascript files. It was creating a .js.map file for each JS file, and at the end of each .min.js file was a commented line like this, which turned out to be causing the problem:

//@ sourceMappingURL=somefile.min.js.map

Since I didn't need the mapping files, I turned this feature off using Options -> Web Essentials -> Javascript -> Set Generate source maps (.map) to false

Hope this helps anyone who finds this problem!

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

Comments

1

Matt, your bundling code looks correct, do you receive any js errors in the browser console when running in Release mode? Also as a side note, are you aware that you can simply define the folder path to your JS files and they will all be included?

1 Comment

Thanks for your reply. There are no js errors - it just seems to be ignoring the content of the second file in the bundled output. Frustratingly I can only seem to replicate the problem in this particular solution and I can't seem to reproduce it in an empty project! I did know that you can define a folder path, but I presume you would then lose control over the order of the bundled files (I don't want to have the hassle of renaming my files with numeric prefixes etc)?

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.