taritsyn / dartsasshost Goto Github PK
View Code? Open in Web Editor NEW.NET wrapper around the Dart Sass.
License: MIT License
.NET wrapper around the Dart Sass.
License: MIT License
Hello,
I'm using this library to compile some Sass to CSS inside an F# script.
My code looks somewhat like:
#r "nuget: JavaScriptEngineSwitcher.ChakraCore.Native.win-x64"
#r "nuget: JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64"
#r "nuget: JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64"
#r "nuget: JavaScriptEngineSwitcher.ChakraCore, 3.19.0"
#r "nuget: DartSassHost, 1.0.0-preview8"
open System
open System.IO
open DartSassHost
open DartSassHost.Helpers
open JavaScriptEngineSwitcher.ChakraCore
open FSharp.Control.Reactive
let sassCompiler = new SassCompiler (new ChakraCoreJsEngineFactory ())
let (</>) a b = Path.Combine (a, b)
let inputFileToolpage = __SOURCE_DIRECTORY__ </> "online-tool.sass"
let inputFileTemplate = __SOURCE_DIRECTORY__ </> "template.sass"
let inputFolder = __SOURCE_DIRECTORY__
let outputToolPage = __SOURCE_DIRECTORY__ </> ".." </> ".tool" </> "online-tool.css"
let outputTemplate = __SOURCE_DIRECTORY__ </> "../assets/" </> "template.css"
let compileSass () =
try
let homepage =
sassCompiler.CompileFile (inputFileToolpage, ?outputPath = Some outputToolPage)
let template =
sassCompiler.CompileFile (inputFileTemplate, ?outputPath = Some outputTemplate)
File.WriteAllText (outputToolPage, homepage.CompiledContent)
printfn "Compiled %s at %A" outputToolPage DateTime.Now
File.WriteAllText (outputTemplate, template.CompiledContent)
printfn "Compiled %s at %A" outputTemplate DateTime.Now
with
| :? SassCompilerLoadException as sclex ->
printfn
"During loading of Sass compiler an error occurred. See details:\n%s"
(SassErrorHelpers.GenerateErrorDetails sclex)
| :? SassCompilationException as sce ->
printfn
"During compilation of SCSS code an error occurred. See details:\n%s"
(SassErrorHelpers.GenerateErrorDetails sce)
| :? SassException as e ->
printfn
"During working of Sass compiler an unknown error occurred. See details:\n%s"
(SassErrorHelpers.GenerateErrorDetails e)
| ex -> printfn "Unexpected exception during Sass compilation: %A" ex
compileSass ()
This all works fine but when executing the script on Linux, I always exit with code 139
.
I was able to isolate it to sassCompiler.CompileFile
.
The moment I remove that line, I get exit code 0
.
In every case, my Sass is compiled to CSS and I can save it to disk.
I'm just a bit annoyed with that exit code in my CI system.
It does not occur on Windows, is there anything I should take into account when running on Linux?
Creating a new SassCompiler using the 7.3.5 version of the V8 Engine switcher results in an exception when attempting to use the compiler (or even view it's properties in the debugger).
The problem appears to be present in both .net6 and .net7 on the MacOS. The application closes due to a hard fault (no exception thrown, force closes the debugger as well). The console output states only
Fatal error in , line 0
Check failed: 12 == (*__error()).
FailureMessage Object: 0x7ff7baf9f590
The problem is reproducible by running the webcompiler here: https://github.com/excubo-ag/WebCompiler as is, compiling any .sass file.
Hi!
it is not really an issue, it is just a question and a question is too simple:
Your project contains compiled files of dart sass. How did you compile dart sass into js file or where do you get this files from?
Attempting to import a partial file that does not have an extension causes an error to be thrown by the DartSassHost code which is NOT handled with a SassException. At first I thought this might be a Dart Sass issue, but now it appears to be a DartSassHost bug:
Using a virtual filesystem that allows files without extensions and file structure like this:
@import "mixins/banner";
... contents of this file do not matter for this issue ...
Attempting to compile bootstrap.scss results in the following error message:
System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at DartSassHost.Helpers.PathHelpers.MakeRelativePath(String relativeTo, String path)
at DartSassHost.Helpers.PathHelpers.PrettifyPath(String currentDirectory, String path)
at DartSassHost.JsonConverters.CompilationResultConverter.ReadStackFrame(Utf8JsonReader& reader, String currentDirectory)
at DartSassHost.JsonConverters.CompilationResultConverter.ReadStackFrames(Utf8JsonReader& reader, String currentDirectory)
at DartSassHost.JsonConverters.CompilationResultConverter.ReadError(Utf8JsonReader& reader)
at DartSassHost.JsonConverters.CompilationResultConverter.ReadFirstError(Utf8JsonReader& reader)
at DartSassHost.JsonConverters.CompilationResultConverter.ReadResult(Utf8JsonReader& reader)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at DartSassHost.SassCompiler.InnerCompile(String content, Boolean indentedSyntax, String inputPath, String outputPath, String sourceMapPath, CompilationOptions options)
at DartSassHost.SassCompiler.Compile(String content, String inputPath, String outputPath, String sourceMapPath, CompilationOptions options)
at ...
Aside from the fact that in most system having files without extensions is generally not a good idea (let's just say I don't have any control over that), this code seems to be supported by Dart Sass and can be compiled successfully using other tools (including the LibSassHost library).
In a .Net Framework 4.8 project I added the following 3 NuGet packages:
I configured the Javascript engine switcher in a static function:
IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
var factory = new V8JsEngineFactory();
engineSwitcher.EngineFactories.Add(factory);
engineSwitcher.DefaultEngineName = factory.EngineName;
Then I attempted to compile a stylesheet using the following code:
var options = new CompilationOptions { SourceMap = false, OutputStyle = OutputStyle.Compressed };
var virtualFileReader = new CustomVirtualFileManager("/");
var compiler = new SassCompiler(virtualFileReader, options);
var initialContent = "@import \"subdirectory/filename.scss\"; ...";
var result = compiler.Compile(initialContent, "/main.scss");
Which resulted in an error: Message: During Compilation of Sass code by using the Sass compiler a JavaScript error has occurred: TypeError: e.GetCurrentDirectory is not a function
Switching from V8JsEngineFactory to ChakraCoreJsEngineFactory with no other changes caused the stylesheet to compile successfully.
Since I can still use this library with the Chakra Core JS Engine this is not a showstopper issue for me, but I thought that this bug should be documented somewhere so that you have an opportunity to fix it and also to help other people who might encounter the same issue to know that there is a workaround.
Hi,
I was wondering if you managed to make any progress on this project or if you abandoned work on this?
LibSassHost is used in https://github.com/johan-v-r/LibSassBuilder and if possible I would like to upgrade that project to use dart sass.
Regards,
Jelle Hissink
I did a quick performance test on LibSassHost (c++) vs DartSassHost (js):
CSS output size: 43kb - here the time is a little over double and as one would expect.
LibSass: 0.3s
DartSass: 0.7s
CSS output size: 131kb - here it's over 10 time as slow although the output size is only 3 times as big.
LibSass: 1.6s
DartSass: 17.2s
I suspect that the big one is so much slower because of more calls to file manager. I tried with DisableGlobalMembers = true
and DisableDynamicBinding = true
but no difference.
This is not an issue post as such as the problem can be Dart or V8JsEngine via ClearScript.V8 - but maybe you have a suggestion for a tweak? Else just close and thank you for your wonderful contributions! :)
Hi @Taritsyn,
I'm currently trying to integrate DartSassHost into https://github.com/excubo-ag/WebCompiler/. I have one test failure on linux systems, which completely baffles me. I've reduced it now to this tiny case:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DartSassHost" Version="1.0.0-preview3" />
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore" Version="3.9.1" />
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64" Version="3.9.1" />
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore.Native.win-x64" Version="3.9.1" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0-release-20210626-04" />
</ItemGroup>
</Project>
using DartSassHost;
using NUnit.Framework;
using JavaScriptEngineSwitcher.ChakraCore;
using System.IO;
namespace Namespace
{
public class Class
{
[Test]
public void Method()
{
File.WriteAllText("test.scss", "");
var result = new DartSassHost.SassCompiler(new ChakraCoreJsEngineFactory())
.CompileFile("test.scss");
Assert.AreNotEqual("hello", "hello");
}
}
}
As you can see, this simply compiles an empty scss file (so nothing should go wrong with that, same happens when the file isn't empty). It then continues to check whether "hello" is not equal to "hello", which on Windows systems produces the expected output
Failed Method [70 ms]
Error Message:
Expected: not equal to "hello"
But was: "hello"
Stack Trace:
at Namespace.Class.Method() in /src/test.cs:line 26
However, on linux, this happens:
The active test run was aborted. Reason: Test host process crashed
Test Run Aborted with error System.Exception: One or more errors occurred.
---> System.Exception: Unable to read beyond the end of the stream.
at System.IO.BinaryReader.Read7BitEncodedInt() in System.Private.CoreLib.dll:token 0x60056bc+0x0
at System.IO.BinaryReader.ReadString() in System.Private.CoreLib.dll:token 0x60056b2+0x6
at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.LengthPrefixCommunicationChannel.NotifyDataAvailable() in Microsoft.TestPlatform.CommunicationUtilities.dll:token 0x6000026+0x8
at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TcpClientExtensions.MessageLoopAsync(TcpClient client, ICommunicationChannel channel, Action`1 errorHandler, CancellationToken cancellationToken) in Microsoft.TestPlatform.CommunicationUtilities.dll:token 0x6000064+0xa0
--- End of inner exception stack trace ---.
Getting things to this stage, I would have thought this has nothing to do with DartSassHost. However, if you remove the compilation step, the behavior is as expected (i.e. test failure, rather than crash).
using DartSassHost;
using NUnit.Framework;
using JavaScriptEngineSwitcher.ChakraCore;
using System.IO;
namespace Namespace
{
public class Class
{
[Test]
public void Method()
{
File.WriteAllText("test.scss", "");
//var result = new DartSassHost.SassCompiler(new ChakraCoreJsEngineFactory())
// .CompileFile("test.scss");
Assert.AreNotEqual("hello", "hello");
}
}
}
Do you have any idea what's going on here?
Best regards,
Stefan
DartSassHost crashed silently when trying to use it as part of the local dotnet tool
.
Steps to reproduce:
dotnet pack dartsasshost-compiler.csproj
, save the path where is a package created, for example:Successfully created package '/Users/sergeyzwezdin/Temp/dotnet-tool-DartSassHost/dartsasshost-compiler/bin/Debug/dartsasshost-compiler.1.0.0.nupkg'.
In this example we need /Users/sergeyzwezdin/Temp/dotnet-tool-DartSassHost/dartsasshost-compiler/bin/Debug/
dotnet new tool-manifest
dotnet tool install dartsasshost-compiler --version 1.0.0 --add-source=/Users/sergeyzwezdin/Temp/dotnet-tool-DartSassHost/dartsasshost-compiler/bin/Debug/
< replace path heretest.scss
file into the directorydotnet tool run dartsasshost-compiler test.scss
Normally, this should return compiled code like this:
But if we use it as a local dotnet tool it will fail silently and nothing is displayed:
Also, it is interesting that it works okay if we install the tool globally. ๐ค
Hi Taritsyn,
Are you planning a non-preview release of this library at some point in time?
DartSassBuilder
has a dependency on your package's v1.0.0-preview7
and was wondering when we could look at a full release
Thanks
For a while now, there is a dart-sass-embedded that is, for the lack of a better word, faster, when compared to dart-sass.js. (I realize that at the start of the project, native dart binaries were not available and dart-sass.js was used for that reason.)
There is a node integration example that's actually the sass-embedded npm package. It should give an idea how the recommended way, using embedded sass protocol, should work... it node environment that is.
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.