Skip to content

Commit

Permalink
Added support of @import directives in sass (#378)
Browse files Browse the repository at this point in the history
* Added support of @import directives in sass

* Fixed multi-level import

* Fix typo
  • Loading branch information
asvishnyakov authored Feb 7, 2020
1 parent b718191 commit 87767d9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
9 changes: 9 additions & 0 deletions VirtoCommerce.LiquidThemeEngine/ISassFileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LibSassHost;

namespace VirtoCommerce.LiquidThemeEngine
{
public interface ISassFileManager: IFileManager
{
string CurrentDirectory { get; set; }
}
}
50 changes: 50 additions & 0 deletions VirtoCommerce.LiquidThemeEngine/SassFileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.IO;
using LibSassHost;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.StaticContent;

namespace VirtoCommerce.LiquidThemeEngine
{
public class SassFileManager: ISassFileManager
{
private readonly IContentBlobProvider _contentBlobProvider;

public bool SupportsConversionToAbsolutePath { get; } = false;

public string CurrentDirectory { get; set; }

public SassFileManager(IContentBlobProvider contentBlobProvider)
{
_contentBlobProvider = contentBlobProvider;
}

public string GetCurrentDirectory() => CurrentDirectory;

public bool FileExists(string path)
{
// Workaround for directories
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
return false;
}
return _contentBlobProvider.PathExists(path);
}

public bool IsAbsolutePath(string path)
{
return Path.GetDirectoryName(path).StartsWith(CurrentDirectory);
}

public string ToAbsolutePath(string path)
{
throw new NotImplementedException();
}

public string ReadFile(string path)
{
return _contentBlobProvider.OpenRead(path).ReadToString();
}
}
}
11 changes: 7 additions & 4 deletions VirtoCommerce.LiquidThemeEngine/ShopifyLiquidThemeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public class ShopifyLiquidThemeEngine : ILiquidThemeEngine, ITemplateLoader
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IStorefrontMemoryCache _memoryCache;
private readonly IContentBlobProvider _themeBlobProvider;
private readonly ISassFileManager _sassFileManager;

public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContextAccessor workContextAccessor,
IHttpContextAccessor httpContextAccessor,
IStorefrontUrlBuilder storeFrontUrlBuilder, IContentBlobProvider contentBlobProvder, IOptions<LiquidThemeEngineOptions> options)
public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContextAccessor workContextAccessor, IHttpContextAccessor httpContextAccessor,
IStorefrontUrlBuilder storeFrontUrlBuilder, IContentBlobProvider contentBlobProvider, ISassFileManager sassFileManager, IOptions<LiquidThemeEngineOptions> options)
{
_workContextAccessor = workContextAccessor;
_httpContextAccessor = httpContextAccessor;
UrlBuilder = storeFrontUrlBuilder;
_options = options.Value;
_memoryCache = memoryCache;
_themeBlobProvider = contentBlobProvder;
_themeBlobProvider = contentBlobProvider;
_sassFileManager = sassFileManager;
SassCompiler.FileManager = sassFileManager;
}

/// <summary>
Expand Down Expand Up @@ -184,6 +186,7 @@ public async Task<Stream> GetAssetStreamAsync(string filePath)
try
{
//handle scss resources
_sassFileManager.CurrentDirectory = Path.GetDirectoryName(filePath);
var result = SassCompiler.Compile(content);
content = result.CompiledContent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public static void AddLiquidViewEngine(this IServiceCollection services, Action<
{
throw new ArgumentNullException(nameof(services));
}


services.AddSingleton<ISassFileManager, SassFileManager>();
services.AddSingleton<ILiquidThemeEngine, ShopifyLiquidThemeEngine>();
services.AddSingleton<ILiquidViewEngine, LiquidThemedViewEngine>();
if (setupAction != null)
Expand Down

0 comments on commit 87767d9

Please sign in to comment.