1

I have a DynamicFoldewrBundle :

bundles.Add(new DynamicFolderBundle("userScripts", "*.js", true, new JsMinify()));

So If I run :

http://optimization.localtest.me/files/templates/designs/HelloTreble/userScripts

...It will render all Javascript files bundled and minified in the HelloTreble folder.

OK.

Question :

But how can I add custom ordered bundles to DynamicFolderBundle?

I have tried this :

bundles.Add(new DynamicFolderBundle("userScripts", "*.js",new JsMinify())
       .Include("~/Scripts/A.js").Include("~/Scripts/B.js"))

Which does work.

But the output is :

  • First : A.JS
  • Second : B.js
  • Third : [All HelloTreble's js files , minified]

(In one file of course)

But what I'm really after is :

  • First : [All HelloTreble's js files , minified]
  • Second : A.js
  • Third : B.js

(In one file of course)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • You can try to implement your own custom `IBundleOrderer` and set it when creating the `DynamicFolderBundle`: `var userScripts = new DynamicFolderBundle("userScripts", "*.js",new JsMinify()) .Include("~/Scripts/A.js").Include("~/Scripts/B.js"); userScripts.Orderer = new MyBundlerOrderer(); bundles.Add(userScripts);` – nemesv Sep 09 '15 at 09:11
  • @RoyiNamir I just checked with my code, mvc v5.2.3 and my order of scripts is same as what you are looking for. A.js,B.js, then minified scripts. – Dandy Sep 09 '15 at 19:35

1 Answers1

3

You can implement IBundleOrderer interface like

public class MyBundleOrderer : IBundleOrderer 
{ 
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files) 
    { 
        var list = new List<BundleFile>(files); 
        list.Reverse(); 
        return list; 
    } 
}

and then attach this to your DynamicFolderBundle like

var myDynamicBundle = new DynamicFolderBundle("userScripts", "*.js",true,new JsMinify())
.Include("~/Scripts/bootstrap.js")
.Include("~/Scripts/respond.js");

myDynamicBundle.Orderer = new MyBundleOrderer();
bundles.Add(myDynamicBundle);

Your scripts will be rendered in order respond.js, then bootstrap.js, then your scripts in userScripts bundle.

Dandy
  • 2,177
  • 14
  • 16
  • But what if I want my included files in the order I included them, then the dynamic folder bundle? Reverse only gets me part of the way – Jeremy May 10 '18 at 14:33