xoofx / sharpscss Goto Github PK
View Code? Open in Web Editor NEWA portable cross-platform pinvoke .NET wrapper around libsass to convert SCSS to CSS
License: BSD 2-Clause "Simplified" License
A portable cross-platform pinvoke .NET wrapper around libsass to convert SCSS to CSS
License: BSD 2-Clause "Simplified" License
Hi,
I have to .scss
files, using FileSystemWatcher
I detect the file changes and re-comple scss using SharpScss using thread (inside async method). doesn't matter which file changed, I always get first file content as compiled css.
BTW it happens only in Compile file mode.
Seems like SharpScss doesn't work well when use inside async method (I didn't test outside of async method). I did the same with LibSassHost and it works fine.
Cheers!
I'm wanting to use SharpScss to compile Scss for Xamarin.Forms. The issue is that css as implemented for Xamarin.Forms introduces ^base
as valid syntax for css. This isn't generally considered valid syntax though and results in an error:
^contentpage {
margin: 10px;
}
SharpScss.ScssException : Error: Invalid CSS after "}": expected 1 selector or at-rule, was "^contentpage {"
on line 7 of Templates/Scss/style2.scss
Is there a way that I can add support for this syntax without disabling error detection?
This has been an issue for a long time. It appears that invoking libsass.dll keeps it open and the file can't be overwritten during publish or rebuilding unless IIS is restarted. Can you release the DLL after each call to the SharpScss.Scss object?
Why BSD 2 license? Could you change it to MIT? I would love to use this in my open source MIT projects, but don't want to have to mix multiple license types that then anyone wanting to use my code would have to then know about too.
Just a quick thanks Alexandre, this library really helped me out. Cheers!
When testing 3rd party application which referring to the SharpScss nuget package with the latest .NET 9 build, it stops working after accessing page. It is caused by SharpScss.dll when converting scss to css. The exception from Visual Studio output window is: "Exception thrown: 'SharpScss.ScssException' in SharpScss.dll". Please refer to dotnet/runtime#98646 for more reference.
Reproduction Steps
Minimal Repro steps (Demo attached):
ConsoleDemo.zip
The machine has dotnet-sdk-9.0.100-preview.2.24113.6 installed.
using SharpScss;
var result = Scss.ConvertToCss(@"@import ""foo"";", new ScssOptions()
{
OutputStyle = ScssOutputStyle.Compressed,
InputFile = "test.scss",
TryImport = (ref string file, string path, out string scss, out string map) =>
{
scss = "div {color: #FFF;}";
map = null;
return true;
}
});
var css = result.Css.Trim();
Console.WriteLine(css);
Console.ReadKey();
"framework": {
"name": "Microsoft.NETCore.App",
"version": "9.0.0-preview.2.24112.33"
}
Expected behavior
Launch successfully with converted css output in command window:
Actual behavior
App crashed with stop working prompt window.
Update libsass to latest version, so that project like https://github.com/ligershark/WebOptimizer.Sass can use the latest feature of libsass.
HI xoofx,
I'm now working on a web-compiler tools for .NET Core platform and tried to integrate it with .NET Shared Host Framework Service in order to replace old grunt/gulp pipeline. I found this project can provide SCSS compilation in .NET Core platform, that's especially important for tools development. Currently may compiler are using your library to handling SCSS files. However, I've found your library is a P/Invoke wrapper for libsass.dll. I'm wondering it will be not executable in non-Windows platform (although I've not tested on other platforms). Could you tell me if this library can run cross-platform, and if not, do you have any plan or idea to provide a cross-platform version for this library?
Thank you very much for your library and I'm looking forward to your reply :-)
Best wishes,
Iris Sakura from China
Hello,
Is there any plan to support autoprefixer?
Hello and thank you for the library and your attention.
Asp.net Core 2.1, local IIS. Upon calling SharpScss.Scss.ConvertToCss I receive the following error in my web project however not in my integration tests:
DllNotFoundException: Unable to load DLL 'libsass' or one of its dependencies: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) SharpScss.LibSass.sass_alloc_memory(size_t size) SharpScss.LibSass+StringUtf8.op_Implicit(string text) SharpScss.Scss.FromCore(string fromStringOrFile, bool fromFile, ScssOptions options) Tales.Web.ComponentBundler.Scss.SharpScssCompiler.Compile(string filePath, string scss) in SharpScssCompiler.cs
=> Last line is my code and further below is as well.
Upon inspecting the Event Viewer, I see the log item:
The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID {D63B10C5-BB46-4990-A94F-E40B9D520160} and APPID {9CA88EE3-ACB7-47C8-AFC4-AB702511C276} to the user xxxx SID (xxxx) from address LocalHost (Using LRPC) running in the application container Unavailable SID (Unavailable). This security permission can be modified using the Component Services administrative tool.
Please let me know what I'm missing or if this is a bug for my platform.
Thanks very much!
Are there plans for Arm64 support (e.g. Apple Silicon)?
I try to compile app.scss
@import "settings";
@import "sections";
And got the error
SharpScss.ScssException: 'Error: File to import not found or unreadable: settings.
on line 1:1 of stdin
>> @import "settings";
^
'
All the files are embedded and gathered by EmbeddedFileProvider
I'm having problems making the TryImport work. I set scss variable to a valid scss-statement but it is not added to the result
I have the code in this gist: https://gist.github.com/Asken/6d16c09fafacb6be1b0c835865c00546
On the Mac today, libsass.so is being downloaded as the native library dependency. The SharepScss tries to load the library simply using "libsass" with no extension. This means that .NET Core looks for libsass.dylib and libsass on the Mac which results in a DllNotFoundException being thrown.
How can I generate CSS files of a specific folder when I run my visual studio web app automatically?
Hello! When compiling my project, which makes use of this library, to a native binary using CoreRT, I get the following stack trace:
System.Exception: Method '[SharpScss]SharpScss.LibSass.sass_make_data_context(string)' requires marshalling that is not yet supported by this compiler.
at SharpScss.LibSass.sass_make_data_context(String) + 0x30
at SharpScss.Scss.FromCore(String, Boolean, ScssOptions) + 0xb5
at Pigmeat.Program.Build(Boolean) + 0x3cf
at Pigmeat.Program.Main(String[]) + 0x79
I know this sounds like a CoreRT problem, rather than anything to do with SharpScss, but, seeing as you're a way smarter guy than me, and you wrote this library, I have to ask: Is there a way I could work around this, or should I just wait for CoreRT to eventually support this use case? Could something be tweaked with SharpScss or your LibSass fork, or is this solely up to the CoreRT and LibSass maintainers?
I have just successfully made use of this on a Debian Linux "Jessie" x64 machine, using the v1.3.4
NuGet package.
One alteration was required to my dev environment (and I imagine will also be needed on prod too). That was to add a DLL map for kernel32 as such. This was done in /etc/mono/config
, where a number of native DLLs which are present on Windows but not Linux-based systems are re-mapped to equivalents.
The code added was:
<dllmap dll="i:kernel32">
<dllentry dll="libdl.so" name="LoadLibrary" target="dlopen"/>
</dllmap>
This probably isn't an exact like-for-like, and I am probably abusing DLL maps by doing so. Since I'm only running SharpScss as part of a build script (so the process is short-lived) I'm willing to handwave any possible memory leaks or improper resource-disposal that I might be incurring.
You may or may not wish to try any of this out and add it to your docs.
The symptom which a developer will see if the above is NOT added is a TypeLoadException
in LibSass
, stating that it can't load kernel32
.
Does SharpScss support Sass modules?
@use 'sass:math';
@mixin shape($edges-number: 6, $main-radius: 30%, $rounding-radius: 10%, $rotated: true, $precision: 20) {
$central-angle: divide(360deg, $edges-number);
$angle: ($edges-number - 2) * divide(180deg, $edges-number);
$max-var-angle: 2 * (90deg - divide($angle, 2));
$precision: 6;
$unit-var-angle: divide($max-var-angle, $precision);
$r-diff: $main-radius + $rounding-radius;
$points: ();
@for $i from 0 to $edges-number {
$vertex-angle: $i * $central-angle + if($rotated, -90deg, 0deg);
$vertex-x: 50% + $r-diff * math.cos($vertex-angle);
$vertex-y: 50% + $r-diff * math.sin($vertex-angle);
@for $j from 0 through $precision {
$curr-angle: $vertex-angle + ($j - 0.5 * $precision) * $unit-var-angle;
$x: $vertex-x + $rounding-radius * math.cos($curr-angle);
$y: $vertex-y + $rounding-radius * math.sin($curr-angle);
$points: $points, $x $y;
}
}
clip-path: polygon($points);
}
This code returns an error when compiling.
SharpScss.ScssException: Error: Invalid CSS after "... $r-diff * math": expected expression (e.g. 1px, bold), was ".cos($vertex-angle)"
on line 16:34 of wwwroot/lib/metronic/components/mixins/_shape.scss
from line 15:1 of wwwroot/lib/metronic/components/_mixins.scss
from line 9:1 of wwwroot/lib/metronic/_init.scss
from line 9:1 of wwwroot/lib/metronic/style.scss
>> $vertex-x: 50% + $r-diff * math.cos($vertex-angle);
---------------------------------^
Any help to fix this is much appreciated!
Repro with this Powershell
Add-Type -AssemblyName (Join-Path $runFolder "SharpScss.dll")
$options = new-object SharpScss.ScssOptions
$options.OutputStyle = [SharpScss.ScssOutputStyle] "Expanded"
$r1= [SharpScss.Scss]::ConvertFileToCss("file1.css", $options)
# the next line does NOT compile file2.css. It compiles file1.css.
$r2= [SharpScss.Scss]::ConvertFileToCss("file2.css", $options)
The cause of the problem is that the first call had options.InputFile set to null, and SharpScss changed that value to "file1.css". Then the second call was specifying the filename in two ways: options.InputFile and the filename parameter to the call. SharpCss chose the options.FIleName value instead of the parameter value.
IMO:
Error: "env(safe-area-inset-right)" is not a number for `max'
on line 1:6916 of https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css, in function `max`
from line 1:6916 of https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css
from line 16:9 of settings
from line 1:9 of stdin
>> :max(0px)){.fancybox-caption{padding:75px max(44px,env(safe-area-inset-right
------------------------------------------^
This may be a Kentico/ASP.NET issue, but I figured it wouldn't hurt to bring it up here.
I have added SharpScss to Kentico as a module. My understanding is that all packages are deployed to a cache where IIS can manage them.
It seems that only SharpScss.dll
is being deployed while libsass.dll
is not. I can't find libsass.dll
anywhere in the cached assemblies.
Results of the deployed assembly location (GetDirectory() results in the expected path w/o the filename):
typeof (LibSass).Assembly.Location
"C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Temporary ASP.NET Files\\kentico10\\beb69eba\\908847ca\\assembly\\dl3\\57cdd0dd\\2e6de74f_2d77d201\\SharpScss.dll"
libsass.dll is found if I place it in the executing directory.
Environment.CurrentDirectory
"c:\\windows\\system32\\inetsrv"
First Edit:
Found a way to get a valid path
Uri.UnescapeDataString(new UriBuilder(typeof (LibSass).Assembly.CodeBase).Path)
"C:/inetpub/wwwroot/Kentico10/CMS/bin/SharpScss.DLL"
Source: http://stackoverflow.com/a/283917/4096074
Second Edit:
Might not be as valid as I thought. There are empty x86 and x64 folders in C:/inetpub/wwwroot/Kentico10/CMS/bin/
. Not sure if those were created by SharpScss.targets
or not.
Something was up with the build; went back to NuGet package, now folders do have libsass.dll under them. Issue persists.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.