WebOptimizer icon indicating copy to clipboard operation
WebOptimizer copied to clipboard

Bundling without minification is not working

Open titiBeOne opened this issue 2 years ago • 4 comments

Hello,

I want to create bundle without minification for developpement (in order to have one configuration), and this code create bundle minified :

builder.Services.AddWebOptimizer(builder.Environment, 
    new CssBundlingSettings { Minify = false}, 
    new CodeBundlingSettings { Minify = false }, 
    pipeline => pipeline.AddJavaScriptBundle("/js/bundle.js", "js/site.js", "js/site2.js")
    );

Is it possible create bundle unminified ?

titiBeOne avatar Sep 02 '22 14:09 titiBeOne

Ok I create this extensions methods to create bundle minified or not in order to use like this in the startup :

List<Bundle> bundles = new()
{
  new Bundle{ Route="/js/lib1.js", SourceFiles = new string[] { "Scripts/myfirstLib.js", "Scripts/libmySecondlib.js" } },
  new Bundle{ Route="/css/lib2.css",  SourceFiles = new string[] { "Content/css/mythirdlib.css", "Content/css/myfourthlib.css"}
}};

 services.AddWebOptimizer((assetPipeline) =>
 {
    assetPipeline.AddBundle(!CurrentEnvironment.IsDevelopment(), bundles);
 });

A bundle Class :

    public class Bundle
    {
        public string Route { get; set; }
        public string[] SourceFiles { get; set; }
    }

And this extension method :

    public static class WebOptimizerExtensions
    {
        public static void AddBundle(
          this IAssetPipeline pipeline,
          bool minify,
          IEnumerable<Bundle> bundles)
        {
            foreach (Bundle bundle in bundles)
            {
                pipeline.AddBundle(minify, bundle);
            }
        }
         public static IAsset AddBundle(
            this IAssetPipeline pipeline,
            bool minify,
            Bundle bundle)
        {
            return bundle.Route.ToLower().EndsWith(".css") ?
                pipeline.AddCssBundle(minify, bundle) :
                pipeline.AddJavaScriptBundle(minify, bundle);
        }
        public static IAsset AddCssBundle(
            this IAssetPipeline pipeline,
            bool minify,
            Bundle bundle)
        {
            return minify ? pipeline.AddCssBundle(bundle.Route, bundle.SourceFiles).UseContentRoot() :
                 pipeline.AddBundle(bundle.Route, "text/css; charset=UTF-8", bundle.SourceFiles)
                  .AdjustRelativePaths()
                  .AddSecurityToAsset(".css")
                  .FingerprintUrls();
        }
        public static IAsset AddJavaScriptBundle(
            this IAssetPipeline pipeline,
            bool minify,
             Bundle bundle)
        {
            return minify ? pipeline.AddJavaScriptBundle(bundle.Route, bundle.SourceFiles).UseContentRoot() :
               pipeline.AddBundle(bundle.Route, "text/javascript; charset=UTF-8", bundle.SourceFiles).
               AddSecurityToAsset(".js", ".jsx", ".es5", ".es6");
        }
        private static IAsset AddSecurityToAsset(this IAsset asset, params string[] extensions)
        {
            return asset.EnforceFileExtensions(extensions)
                 .Concatenate()
                 .AddResponseHeader("X-Content-Type-Options", "nosniff")
                 .UseContentRoot();
        }
    }

titiBeOne avatar Sep 13 '22 07:09 titiBeOne

Looks like there is a fix waiting for the next release https://github.com/ligershark/WebOptimizer/pull/245

dove avatar Sep 22 '22 10:09 dove

The fix has now been released by the looks of things which is nice thought I'd mention it as I checked today to see if it had been

TechLiam avatar Oct 06 '22 19:10 TechLiam

Seems to work, but it would be great to have the same property for the css, for the css i have to work with OutputMode, SingleLine for the minify version MultipleLines for the unminified.

My code :

    public static IAsset AddBundle(
        this IAssetPipeline pipeline,
        bool minify,
        Bundle bundle)
   {
       if (bundle.Route.ToLower().EndsWith(".css"))
        {
            CssSettings cssMinifySettings = new() 
                     {  
                       OutputMode = minify ? NUglify.OutputMode.SingleLine: NUglify.OutputMode.MultipleLines, 
                        CommentMode = minify ? CssComment.None : CssComment.All
                    };
            return pipeline.AddCssBundle(bundle.Route, cssMinifySettings, bundle.SourceFiles).UseContentRoot();
        }
        CodeSettings codeCodeSettings = new() 
        { 
                   MinifyCode = minify, 
                   PreserveImportantComments = minify 
         };
        return pipeline.AddJavaScriptBundle(bundle.Route, codeCodeSettings, bundle.SourceFiles).UseContentRoot();
    }

`

titiBeOne avatar Oct 09 '22 21:10 titiBeOne

I am still facing the above issue even though I specify CodeSettings. Here is how my code settings look. var codeSettings = new CodeSettings { MinifyCode = false };

prashanthbachu avatar Mar 22 '23 15:03 prashanthbachu

@prashanthbachu into which method are you passing codeSettings and which version of the package are you using?

TechLiam avatar Mar 22 '23 17:03 TechLiam

I'm passing it to AddJavaScriptBundle() method. I am using 3.0.380 version. I think I am able to get around it by passing CodeBundlingSettings with Minify false on AddWebOptimizer. If I do that and use AddBundle instead of _AddJavaScriptBundle it seems to work.

prashanthbachu avatar Mar 22 '23 18:03 prashanthbachu

So this works for me.

pipeline .AddJavaScriptBundle("/Scripts/app.js", new CodeSettings() { MinifyCode = false }, "/Scripts/main.js", "/Scripts/app/*.js") .UseContentRoot();

I'm doing UseContentRoot as my files are not in the wwwroot folder thing as I'm old school like that, but if your files are in the wwwroot folder you shouldn't need that.

TechLiam avatar Mar 22 '23 21:03 TechLiam