Closed titiBeOne closed 1 year ago
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();
}
}
Looks like there is a fix waiting for the next release https://github.com/ligershark/WebOptimizer/pull/245
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
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();
}
`
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 into which method are you passing codeSettings and which version of the package are you using?
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.
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.
Hello,
I want to create bundle without minification for developpement (in order to have one configuration), and this code create bundle minified :
Is it possible create bundle unminified ?