ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
764 stars 112 forks source link

CssBundler fails #109

Open atriusfox opened 5 years ago

atriusfox commented 5 years ago

AddCssBundler is failing. AddJavaScriptBundler works fine.

I tried copy pasting the exact CSS from the sample and paths (/css/a.css and /css/b.css)

This was post upgrade from .net 2.1 to .net 3.0 going from WebOptimizer version 1.0.236 to 3.0.250 to support .net 3.0

Everything worked pre-upgrade

Used this line of code for the pipeline call:

pipeline.AddCssBundle("/css/bundle.css", "css/a.css", "css/b.css");

Attached picture of error when trying to access https://localhost/css/bundle.css

Capture

I'm unsure how to debug this further.

I attempted to confirm through tracing the sample project that it should be getting the correct fullpath name when attempting to get FileInfo and when I do it manually I think I'm getting it right but I'm unable to step into the WebOptimizer middleware code to see what's different in my project vs the sample.

dannyyy commented 4 years ago

I'm experience the exact same behavior. Haven't found a solution yet. Please help.

ndtien84 commented 4 years ago

I got the same issue. Currently use: ASP.NET Core 3.0 and LigerShark.WebOptimizer.Core 3.0.250

skimedic commented 4 years ago

I just tested this (CSS and JS bundling) with what's in master as well as what's in NuGet. Both succeeded. Please look at the WebOptimizer.Core.Mvc3 sample for reference.

dannyyy commented 4 years ago

I just tested this (CSS and JS bundling) with what's in master as well as what's in NuGet. Both succeeded. Please look at the WebOptimizer.Core.Mvc3 sample for reference.

I tried your sample as well an it's working. But I have several projects which this is not the case. Haven't found the differences.

For now I have found a Workaround @ndtien84 @atriusfox I configured for each stylesheet the absolut path /wwwroot/src/css/foo.css instead of src/css.foo.css and used the extension method UseContentRoot(). For consistency I did the same for my js files even it's not necessary for those. With this adjustment everythin is working fine.

ndtien84 commented 4 years ago

thanks @skimedic @dannyyy

example project (work great) image

my project (not work) image

j-ghadiri commented 4 years ago

Hi @skimedic When I use the following frameworks👇 in ASP.NET Core 3.1 together LigerShark.WebOptimizer.Core There is a problem and it cannot work properly

`

 <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.0" />`

I think an error in LigerShark.WebOptimizer.Core There is a use with these frameworks

How should we fix the problem?

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null. (Parameter 'fileName')

error When Use LigerShark WebOptimizer Core with other framework

j-ghadiri commented 4 years ago

for Improve This Error Convert this Line of Code content[key] = Adjust(config.Content[key].AsString(), input.PhysicalPath, output.PhysicalPath);

To 👇 : content[key] = Adjust(config.Content[key].AsString(), input.PhysicalPath, output.Name);

640774n6 commented 4 years ago

From what I can tell, this issue is caused when using a CompositeFileProvider. WebRootFileProvider is often a CompositeFileProvider by default when using a razor class library with static assets.

ComposteFileProvider.GetFileInfo() iterates through it's providers and returns the first non-null file info it finds where Exists = true. If it doesn't find one, it returns a NotFoundFileInfo which returns null for PhysicalPath (https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.fileproviders.notfoundfileinfo?view=dotnet-plat-ext-3.1).

RelativePathAdjuster attempts to get a file info from the provider for the output (which will never exist, or shouldn't). It gets a NotFoundFileInfo back which returns null when attempting to get PhysicalPath for the Adjust() call. Adjust() then attempts to create a new FileInfo using the outputPath (which is null) and throws an exception.

robertmuehsig commented 2 years ago

Had the same problem and somehow this "works":

                bundle.AddCssBundle("/Content/css.css",
                    "/wwwroot/Content/metro-bootstrap.css",
                    "/wwwroot/Content/jquery.fileupload-ui.css",
                    "/wwwroot/Content/font-awesome.css",
                    "/wwwroot/Content/tablesorter.theme.default.css",
                    "/wwwroot/Content/jquery.bonsai.css",
                    "/wwwroot/Content/DataTables/css/jquery.dataTables.css",
                    "/wwwroot/Content/DataTables/css/buttons.dataTables.css",
                    "/wwwroot/Content/DataTables/css/buttons.bootstrap.css",
                    "/wwwroot/Content/Site.css").UseContentRoot().AdjustRelativePaths();
hhankj2u commented 2 years ago

@robertmuehsig In blazor, image url in css file will have prefix "/wwwroot", which is not found. I found that when bundle process go through Concatenator(), filePath has been change to Guid, and CssFingerprinter() cannot get a PhysicalPath. My "working" solution is use Concatenate() or FingerprintUrls(), or even call FingerprintUrls() before Concatenate() !!! (not recommend from docs /// NOTE: Make sure to call Concatinate() before this method)

    pipeline.AddBundle("/bundle.css", "text/css; charset=UTF-8",
            "css/bootstrap.min.css",
            "css/style.css")
        .EnforceFileExtensions(".css")
        .AdjustRelativePaths()
        .Concatenate()
        //.FingerprintUrls()
        .AddResponseHeader("X-Content-Type-Options", "nosniff")
        .MinifyCss(new CssSettings());
dahari87 commented 2 years ago

as @robertmuehsig suggested also works for me pipeline.AddCssBundle("/bundle.css", "wwwroot/css/*/.css").UseContentRoot().AdjustRelativePaths();

mikalai-sauchanka commented 2 years ago

For me it was IWebHostEnvironment.WebRootPath, which was null. This happens when you don't have 'wwwroot' folder in your project.