ligershark / WebOptimizer

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

Using Weboptimizer with Razor Class Libraries #249

Open tskong opened 1 year ago

tskong commented 1 year ago

Hi,

I've got weboptimizer working nicely with my web app, but when I try to pull in .js or .css files from Razor Class Libraries, the page renders, but I'm getting a 404 on the javascript and css.

Is there some configuration required to get it work with RCL?

Thanks

pavloss commented 1 year ago

Without a code sample it's hard to advise. But are you putting your js and css files in the wwwroot of your RCL and then referencing them in your project as _content/{name of RCL}/{filename}.css ?

tskong commented 1 year ago

Hi, Yes exactly that. I can get it to work with the non weboptimized versions of the files, but when I try and get the minified versions sing _content/RazorClassLibrary1/site.min.css, it can't find it.

It's fine in the main web application, it can pick up it's minified files no problem, just the RCL.

jaoord commented 1 year ago

Not sure if you've found a solution already but I was having a similar issue. I fixed it by adding app.UseWebOptimizer(); to the Startup.cs (in the Configure method, right before app.UseStaticFiles();. I also had to change the bundled URL to something else than /css/myfilename.css because /css is the folder I have my css files in. I changed it to the root, so for example pipeline.AddCssBundle("/_myfilename.css", "css/file1.css", "css/file2.css");

hammond13 commented 1 year ago

I'm running into the same problem. WebOptimizer was working before I moved files into the RCL. If I disable minification, then the file works, so I know I have the path right. Responses above suggest that it should work, so I'm not sure what I'm doing wrong.

This is a .NET 6 project.

Example url: https://localhost:7264/_content/RCL.Project/Content/jquery.validate.password.css

DirectoryNotFoundException: Could not find a part of the path 'C:\PathToMainProject\wwwroot\_content\RCL.Project\Content\jquery.validate.password.css'.

Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.OSFileStreamStrategy..ctor(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
Microsoft.Extensions.FileProviders.Physical.PhysicalFileInfo.CreateReadStream()
WebOptimizer.Asset.LoadFileContentAsync(IFileProvider fileProvider, AssetContext config, string sourceFile)
WebOptimizer.Asset.ExecuteAsync(HttpContext context, IWebOptimizerOptions options)
WebOptimizer.AssetBuilder.BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
WebOptimizer.AssetMiddleware.HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Any ideas?

hammond13 commented 1 year ago

Update: my problem appears to be specific to .NET 6. I was able to fix the problem by passing in the environment parameter as suggested here:

https://github.com/ligershark/WebOptimizer/issues/232#issuecomment-1355031387

I still ran into a problem with bundling (https://github.com/ligershark/WebOptimizer/issues/172#issue-899098831). Fortunately for me, that only affected one library file that I was able to get around.

gumbarros commented 11 months ago

I think my PR fixes the problem. #287