Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundling a linked JavaScript file

I'm working with Visual Studio 2012 and MVC4. I've added a linked file (from another project) to my MVC4 application. Here are the properties of the file:

  • Build Action: Content
  • Copy to Output Directory: Do not copy

Here is an example of my bundle:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.unobtrusive*","~/Scripts/jquery.validate*","~/Scripts/FolderA/*.js"));

For testing, I've also added an empty JavaScript file (temp.js) to that folder. This is not a linked file. When inspecting the source of the page this file appears but the linked file does not. I cannot navigate directly to this file either. The other files in the bundle appear just fine.

Can linked files be bundled?

like image 950
Mark Meisel Avatar asked Jul 23 '13 15:07

Mark Meisel


People also ask

How do I bundle JavaScript files?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script. But that's just the bare minimum it can do.

What is JavaScript bundling?

JavaScript bundling is an optimization technique you can use to reduce the number of server requests for JavaScript files. Bundling accomplishes this by merging multiple JavaScript files together into one file to reduce the number of page requests.

Can you include one JavaScript file in another?

We can include a JavaScript file in another JavaScript file using the native ES6 module system. This allows us to share code between different JavaScript files and achieve modularity in the code. There are other ways to include a JS file like Node JS require, jQuery's getScript function, and Fetch Loading.


1 Answers

Short answer: No in debug mode, yes in release mode.

File linking is a Visual Studio concept used to include files stored elsewhere into code and resource compilation. Clearly, linking a file will work if you need to compile it (it's a source file), if you need to embed it as a resource or you need it copied to target directory (if Copy to Output Directory is set to Copy).


Why it doesn't work in debug mode

In debug mode, bundling is disabled and scripts are linked to individually. Since files are not copied to root of your web application, they will not be accessible to user through IIS. If you try to enable copying of the script file every time you build the application, file will be copied to bin directory of web application. This directory is not accessible through IIS, and again this won't work.


Why it works in release mode

In release mode, bundling of scripts is performed. Script files are not linked to individually from web pages, and therefore user does not need to have access to them directly. Only bundling code needs to be able to access it. But you have to be sneaky about configuring this. You need to:

  • Set Copy to Output Directory of linked scripts to Copy always. If you store your linked scripts in ~/Scripts, once you compile the application they will be copied to ~/bin/Scripts folder.
  • Configure bundling path to include bin directory.

Note ~/bin/Scripts/ in following line:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.unobtrusive*","~/Scripts/jquery.validate*","~/bin/Scripts/FolderA/*.js"));

Disabling debug mode

Debug mode mentioned here is not compiler setting in Visual Studio. This is an element in web.config file.

<system.web>
    <compilation debug="false" targetFramework="4.5" />
like image 85
Nikola Radosavljević Avatar answered Oct 15 '22 08:10

Nikola Radosavljević