0

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.

artfuldev
  • 1,118
  • 1
  • 13
  • 25
  • If cleanup needs to run multiple times then can't a,b,c,d etc. just call the cleanup code when they are finished? – jamesSampica Jun 03 '15 at 16:02
  • The a, b, c and d.js files are auto-generated when the project is built, by a third party tool which cannot be hacked into. Which is why the problem. – artfuldev Jun 04 '15 at 05:04
  • One thing you could do is create individual bundles for `a.js, cleanup.js`, `b.js, cleanup.js` etc.. A bit of an annoyance but would work. – jamesSampica Jun 04 '15 at 13:29
  • That will result in multiple script bundles being rendered which I would like to avoid. – artfuldev Jun 08 '15 at 07:30

1 Answers1

0

Use in bundleConfig.cs following line:

BundleTable.EnableOptimizations = true;
wscourge
  • 10,657
  • 14
  • 59
  • 80
cseppd
  • 11
  • 2