How to include a file multiple times in an ASP.NET MVC5 script bundle?

608 Views Asked by At

In ASP.NET minification and bundling feature, I have a script that cleans up certain things and has to be run after loading multiple independent files.

So I create a bundle,

new ScriptBundle(virtualPath, cdnPath).Include(
    "~/Scripts/a.js",
    "~/Scripts/cleanup.js",
    "~/Scripts/b.js",
    "~/Scripts/cleanup.js",
    "~/Scripts/c.js",
    "~/Scripts/cleanup.js",
    "~/Scripts/d.js",
    "~/Scripts/cleanup.js",
);

Later I render this using the format <script defer="defer" src="{0}"></script>, expecting that the cleanup will occur both in debug and release mode as expected, once after every script's code. However ASP.NET Bundling strips the repetitive calls of the same file path hence the output is

<script defer="defer" src="/Scripts/a.js"></script>
<script defer="defer" src="/Scripts/cleanup.js"></script>
<script defer="defer" src="/Scripts/b.js"></script>
<script defer="defer" src="/Scripts/c.js"></script>
<script defer="defer" src="/Scripts/d.js"></script>

in Debug mode, whereas I expected,

<script defer="defer" src="/Scripts/a.js"></script>
<script defer="defer" src="/Scripts/cleanup.js"></script>
<script defer="defer" src="/Scripts/b.js"></script>
<script defer="defer" src="/Scripts/cleanup.js"></script>
<script defer="defer" src="/Scripts/c.js"></script>
<script defer="defer" src="/Scripts/cleanup.js"></script>
<script defer="defer" src="/Scripts/d.js"></script>
<script defer="defer" src="/Scripts/cleanup.js"></script>

This also tells me that the file will be left out in the bundled version which is not what I desire.

1

There are 1 best solutions below

0
On

Use in bundleConfig.cs following line:

BundleTable.EnableOptimizations = true;