0

Directory bundles can be created in this way...

bundles.Add(new ScriptBundle("~/bundles/mybundle").IncludeDirectory(
            "~/Scripts/app/", "*.js"));

The output (without token) of @Scripts.Url("~/bundles/mybundle") is this..

/bundles/mybundle

Which directs to a single file with the entire directory contents bundled and minified.

Is there a way to have the bundler create a bundle such that the files are minified but not bundled into a single file?

/bundles/mybundle/myScript1.js
/bundles/mybundle/myScript2.js
/bundles/mybundle/myScript3.js

Scripts.Url("~/bundles/mybundle") would then reference the root directory of the virtual path

jamesSampica
  • 12,230
  • 3
  • 63
  • 85
  • 1
    Make one bundle for each file? It's tedious, but it would work. – gunr2171 Aug 22 '14 at 19:45
  • @gunr2171 If you specify the same bundle path again, won't that override other virtual paths? I think I would get 1 file at `/bundles/mybundle/` with the last file added to the bundle. – jamesSampica Aug 22 '14 at 20:06
  • The bundle paths would have to be `~/bundles/mybundle/myScript1` and so on. I don't like it, but that's my suggestion. If you have under 10, shouldn't be much of a problem. – gunr2171 Aug 22 '14 at 20:09
  • 1
    The point of the Bundler is *bundling*, i.e. grouping all your files together - minification is just gravy. If you don't want to bundle, don't use the Bundler, and just directly reference minified versions of your scripts or use a post-build script to run an Grunt/Ant task or something. – Chris Pratt Aug 22 '14 at 21:52
  • What @ChrisPratt said. Why are you using bundling if you don't want to bundle? If you explain why you need individual minified files, perhaps we can provide an appropriate answer. Right now you are asking about [your attempted solution rather than your actual problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – MikeSmithDev Aug 23 '14 at 00:51
  • @ChrisPratt Minification is half the point of bundling. One is to keep the number of requests down and the other to keep each request small. – jamesSampica Aug 23 '14 at 00:51
  • @Shoe, yes so why do you not care about keeping the requests down? – MikeSmithDev Aug 23 '14 at 00:52
  • @MikeSmithDev My scripts are large and I have a lazy loading technique in place. Specifically I am using dojo modules which load js files as needed. – jamesSampica Aug 23 '14 at 00:55
  • @Shoe that makes sense. Maybe what you are looking for is how to automate minifying files like what Chris suggested. Bundler wouldn't be a good solution for this unless you make a "bundle" for each file, which would be tedious... but doable. – MikeSmithDev Aug 23 '14 at 00:57

1 Answers1

-1

You could do as @gunr2171 says and create a bundle for each file, but a far cleaner solution would be to create your own custom bundling transform like so:

public class MinifyTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        string bundleResponse = string.Empty;
        foreach (FileInfo file in response.Files)
        {
            //Minify file
        }
        response.Content = bundleResponse ;
    }
}

You can then use your bundler transformer like so:

Bundle myBundle = new Bundle("~/bundles/mybundle", new MinifyTransform());
Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
  • 1
    This is essentially the same outcome as before though, no? All the files in the bundle will be put into `response.Content`, so a single file. – jamesSampica Aug 23 '14 at 02:53