koenvzeijl / AspNetCore.SassCompiler

Sass Compiler Library for .NET Core 3.1/5.x/6.x/7.x without node, using dart-sass as a compiler
https://www.nuget.org/packages/AspNetCore.SassCompiler/
MIT License
215 stars 26 forks source link

How to include files generated in a library project in the referencing project? #185

Closed Cherepoc closed 5 months ago

Cherepoc commented 5 months ago

Hi! I think this might not be a bug,, but I can't get the compiled files to be copied properly into the output directories of my executable projects. I'm compiling my scss in the library project that is referenced by several executable projects, and also other library projects too. I managed to make it work in my pipeline with docker, but there's a way to create conditions for the file not being copied. If you manually remove the bin directory of the executable project and try to run it again locally, then the compiled files won't be placed into the newly regenerated bin directory. Here's an example project: SassCompileTest.zip

There's a SassCompileTest project, it's a console app that prints if the compiled style was found or not. It references library project SassCompileLib, it has a source sass file style-sources/style.scss. On build, a new file is generated - style-compiled/style.css.

Steps to reproduce the bug:

  1. Run the SassCompileTest console project.
  2. The first time it should print true, which indicates that the file was correctly copied to the output directory.
  3. Go to the SassCompileTest project directory and remove the bin directory.
  4. Run the SassCompileTest console project again.
  5. Now it should return false. The file was not copied.

Expected: The SassCompileTest app prints true, the newly regenerated bin directory has style-compiled/style.css.

I'm using a pc with windows 10, and .NET 8.0.204 sdk.

I've used suggestions from the issue https://github.com/koenvzeijl/AspNetCore.SassCompiler/issues/170 Thanks!

sleeuwen commented 5 months ago

Hi @Cherepoc If you want to have the css styles from a library project included in the output directory of your executables, you should create the library project as a razor class library. Please take a look at the docs for a razor class library here.

We also have a sample in the repo for a Razor Class Library and a Blazor WebAssembly Sample, the latter references the former, which will make sure that the css files are copied to the output folder of the Blazor project.

The fact that it works the first time might have something to do with the fact that the second time you run the project, it doesn't rebuild the library, in which case we rely on the functionality provided by a razor class library to include the .css files.

Cherepoc commented 5 months ago

I found the solution, and it's much simpler then I thought - I just need to add another item group with the same content, but outside of the target.

    <ItemGroup>
        <None Include="style-compiled\*" CopyToOutputDirectory="PreserveNewest"/>
    </ItemGroup>

    <Target Name="IncludeCompiledSassStyles" DependsOnTargets="Compile Sass" BeforeTargets="BeforeBuild">
        <ItemGroup>
            <None Include="style-compiled\*" CopyToOutputDirectory="PreserveNewest"/>
        </ItemGroup>
    </Target>

It works now.