Skip to content

Commit

Permalink
Merge pull request #75 from Taritsyn/js-engine-switcher-instance-from-di
Browse files Browse the repository at this point in the history
Get an instance of the JS engine switcher by using DI
[release]
  • Loading branch information
madskristensen authored Feb 6, 2024
2 parents 62a6b23 + ecf6a18 commit d09542e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ First of all, you need to install the [JavaScriptEngineSwitcher.Extensions.MsDep
Then you need to install one of the NuGet packages containing a JS engine provider:

* [JavaScriptEngineSwitcher.ChakraCore](https://nuget.org/packages/JavaScriptEngineSwitcher.ChakraCore)
* [JavaScriptEngineSwitcher.Jint](https://www.nuget.org/packages/JavaScriptEngineSwitcher.Jint) (prerelease versions only)
* [JavaScriptEngineSwitcher.Jint](https://www.nuget.org/packages/JavaScriptEngineSwitcher.Jint)
* [JavaScriptEngineSwitcher.Msie](https://nuget.org/packages/JavaScriptEngineSwitcher.Msie) (only in the Chakra “Edge” JsRT mode)
* [JavaScriptEngineSwitcher.V8](https://nuget.org/packages/JavaScriptEngineSwitcher.V8)

Expand All @@ -37,7 +37,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddMvc();

// Add JavaScriptEngineSwitcher services to the services container.
services.AddJsEngineSwitcher(options => options.DefaultEngineName = V8JsEngine.EngineName)
services.AddJsEngineSwitcher(options =>
{
options.AllowCurrentProperty = false;
options.DefaultEngineName = V8JsEngine.EngineName;
})
.AddV8()
;

Expand Down
7 changes: 5 additions & 2 deletions sample/WebOptimizer.Sass.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Extensions.MsDependencyInjection;
using JavaScriptEngineSwitcher.V8;
using Microsoft.AspNetCore.Builder;
Expand All @@ -16,7 +15,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddRazorPages();

// Add JavaScriptEngineSwitcher services to the services container.
services.AddJsEngineSwitcher(options => options.DefaultEngineName = V8JsEngine.EngineName)
services.AddJsEngineSwitcher(options =>
{
options.AllowCurrentProperty = false;
options.DefaultEngineName = V8JsEngine.EngineName;
})
.AddV8()
;

Expand Down
20 changes: 10 additions & 10 deletions sample/WebOptimizer.Sass.Sample/WebOptimizer.Sass.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JavaScriptEngineSwitcher.Extensions.MsDependencyInjection" Version="3.19.0" />
<PackageReference Include="JavaScriptEngineSwitcher.V8" Version="3.20.10" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-arm" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-arm64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-x64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.osx-arm64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.osx-x64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-arm64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-x64" Version="7.3.7 " />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-x86" Version="7.3.7 " />
<PackageReference Include="JavaScriptEngineSwitcher.Extensions.MsDependencyInjection" Version="3.24.0" />
<PackageReference Include="JavaScriptEngineSwitcher.V8" Version="3.23.2" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-arm" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-arm64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.linux-x64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.osx-arm64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.osx-x64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-arm64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-x64" Version="7.4.4" />
<PackageReference Include="Microsoft.ClearScript.V8.Native.win-x86" Version="7.4.4" />

<ProjectReference Include="..\..\src\WebOptimizer.Sass.csproj" />
</ItemGroup>
Expand Down
8 changes: 6 additions & 2 deletions src/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DartSassHost;
using JavaScriptEngineSwitcher.Core;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
Expand Down Expand Up @@ -53,7 +54,10 @@ public Compiler(IAsset asset, WebOptimizerScssOptions options = null)
public Task ExecuteAsync(IAssetContext context)
{
var content = new Dictionary<string, byte[]>();
var env = (IWebHostEnvironment)context.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment));
var serviceProvider = context.HttpContext.RequestServices;
var env = (IWebHostEnvironment)serviceProvider.GetService(typeof(IWebHostEnvironment));
var engineSwitcher = (IJsEngineSwitcher)serviceProvider.GetService(typeof(IJsEngineSwitcher));

IFileProvider fileProvider = context.Asset.GetFileProvider(env);

var settings = new CompilationOptions();
Expand Down Expand Up @@ -83,7 +87,7 @@ public Task ExecuteAsync(IAssetContext context)
fileManager = new ManifestFileManager(fileProvider);
}

using (var sassCompiler = new SassCompiler(fileManager, settings))
using (var sassCompiler = new SassCompiler(engineSwitcher.CreateDefaultEngine, fileManager, settings))
{
foreach (string route in context.Content.Keys)
{
Expand Down
5 changes: 5 additions & 0 deletions src/PipelineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ private static IAsset minifyCssWithOptions(this IAsset asset, WebOptimizerScssOp

private static void CheckJsEngineRegistration()
{
if (!JsEngineSwitcher.AllowCurrentProperty)
{
return;
}

IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
if (engineSwitcher == null
|| !engineSwitcher.EngineFactories.Any(e => e.EngineName == engineSwitcher.DefaultEngineName))
Expand Down
3 changes: 2 additions & 1 deletion src/WebOptimizer.Sass.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="DartSassHost" Version="1.0.1" />
<PackageReference Include="DartSassHost" Version="1.0.10" />
<PackageReference Include="JavaScriptEngineSwitcher.Core" Version="3.24.0" />
<PackageReference Include="LigerShark.WebOptimizer.Core" Version="3.0.372" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions test/CompilerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public async Task Compile_Success()
context.Setup(s => s.HttpContext.RequestServices.GetService(typeof(IWebHostEnvironment)))
.Returns(env.Object);

context.Setup(s => s.HttpContext.RequestServices.GetService(typeof(IJsEngineSwitcher)))
.Returns(JsEngineSwitcher.Current);

var inputFile = new PhysicalFileInfo(new FileInfo("site.css"));

context.SetupGet(s => s.Asset)
Expand Down
2 changes: 1 addition & 1 deletion test/WebOptimizer.Sass.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JavaScriptEngineSwitcher.Jint" Version="3.20.9-preview" />
<PackageReference Include="JavaScriptEngineSwitcher.Jint" Version="3.23.9" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.18.1" />
Expand Down

0 comments on commit d09542e

Please sign in to comment.