Skip to content

Commit

Permalink
Sass: Removes post-processing path normalization.
Browse files Browse the repository at this point in the history
LibSass does not support relative paths resolution. Nor does ruby-sass
(unless we use Compass). At present, WE tries to resolve the URLs in
the post-compilation step by using one base path. This misleads in the
scenarios where we have nested imports and the meaning of
'relative path' need to be twisted to justify the outcome. :)

Less provides this functionality OOTB. We will have to wait for LibSass
to provide this functionality; either sass/libsass#674 or
sass/libsass#532 gets addressed;
  • Loading branch information
am11 committed Jun 26, 2015
1 parent 75609c5 commit 1f5c263
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 70 deletions.
5 changes: 0 additions & 5 deletions EditorExtensions/JavaScript/Linters/JsHintCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,5 @@ protected override string GetPath(string sourceFileName, string targetFileName)

return parameters.FlattenParameters();
}

protected override string PostProcessResult(string result, string targetFileName, string sourceFileName)
{
return result;
}
}
}
7 changes: 3 additions & 4 deletions EditorExtensions/Misc/Bundles/BundleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ private async static Task<string> CombineFiles(Dictionary<string, string> files,
source.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0 &&
bundle.AdjustRelativePaths)
source = CssUrlNormalizer.NormalizeUrls(
tree: new CssParser().Parse(source, true),
targetFile: bundleFile,
oldBasePath: actualFile
);
tree: new CssParser().Parse(source, true),
targetFile: bundleFile,
oldBasePath: actualFile);
}
else if (Path.GetExtension(file).Equals(".ts", StringComparison.OrdinalIgnoreCase))
{
Expand Down
31 changes: 1 addition & 30 deletions EditorExtensions/SCSS/Compilers/ScssCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using MadsKristensen.EditorExtensions.Helpers;
using MadsKristensen.EditorExtensions.RtlCss;
using MadsKristensen.EditorExtensions.Settings;
using Microsoft.CSS.Core;
using Microsoft.VisualStudio.Utilities;
using Microsoft.Web.Editor;

Expand Down Expand Up @@ -84,31 +81,5 @@ protected override string GetPath(string sourceFileName, string targetFileName)

return parameters.FlattenParameters();
}

protected override string PostProcessResult(string result, string targetFileName, string sourceFileName)
{
// If the caller wants us to renormalize URLs to a different filename, do so.
if (targetFileName != null &&
WESettings.Instance.Scss.AdjustRelativePaths &&
result.IndexOf("url(", StringComparison.OrdinalIgnoreCase) > 0)
{
try
{
result = CssUrlNormalizer.NormalizeUrls(
tree: new CssParser().Parse(result, true),
targetFile: targetFileName,
oldBasePath: sourceFileName);
}
catch (Exception ex)
{
Logger.Log(ServiceName + ": An error occurred while normalizing generated paths in " + sourceFileName + "\r\n" + ex);
return result;
}
}

Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compiled.");

return result;
}
}
}
19 changes: 8 additions & 11 deletions EditorExtensions/Settings/WESettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,25 +604,22 @@ void OnEnableChainCompilationChanged(EventArgs e)
}
}

public abstract class CssChainableCompilationSettings<T> : ChainableCompilationSettings<T>, IChainableCompilerSettings where T : CssChainableCompilationSettings<T>
{
[Category("Compilation")]
[DisplayName("Adjust Relative Paths")]
[Description("Adjust relative paths in post-processing step. This option only works when \"Custom output directory\" is used.")]
[DefaultValue(true)]
public bool AdjustRelativePaths { get; set; }
}

public sealed class LessSettings : CssChainableCompilationSettings<LessSettings>
public sealed class LessSettings : ChainableCompilationSettings<LessSettings>
{
[Category("Compilation")]
[DisplayName("Strict Math")]
[Description("With this option turned off, LESS will try and process all maths in your CSS.")]
[DefaultValue(false)]
public bool StrictMath { get; set; }

[Category("Compilation")]
[DisplayName("Adjust Relative Paths")]
[Description("Adjust relative paths in post-processing step.")]
[DefaultValue(true)]
public bool AdjustRelativePaths { get; set; }
}

public sealed class ScssSettings : CssChainableCompilationSettings<ScssSettings>
public sealed class ScssSettings : ChainableCompilationSettings<ScssSettings>
{
public enum OutputFormat
{
Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/Shared/Compilers/CssCompilerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected async override Task PostWritingResult(CompilerResult result)

private async Task HandleRtlCss(CompilerResult result)
{
string value = PostProcessResult(result.RtlResult, result.RtlTargetFileName, result.RtlSourceFileName);
string value = result.RtlResult;

// Write output file
if (result.RtlTargetFileName != null && (MinifyInPlace || !File.Exists(result.RtlTargetFileName) ||
Expand Down
19 changes: 8 additions & 11 deletions EditorExtensions/Shared/Compilers/NodeExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ private async Task<CompilerResult> ProcessResult(CompilerResult result, bool onl
return result;
}

string resultString = PostProcessResult(result.Result, result.TargetFileName, result.SourceFileName);
if (this is ILintCompiler)
return result;

Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compiled.");

if ((this is ILintCompiler) && !onlyPreview)
if (!onlyPreview)
{
// Write output file
if (result.TargetFileName != null && (MinifyInPlace || !File.Exists(result.TargetFileName) ||
resultString != await FileHelpers.ReadAllTextRetry(result.TargetFileName)))
result.Result != await FileHelpers.ReadAllTextRetry(result.TargetFileName)))
{
ProjectHelpers.CheckOutFileFromSourceControl(result.TargetFileName);
await FileHelpers.WriteAllTextRetry(result.TargetFileName, resultString);
await FileHelpers.WriteAllTextRetry(result.TargetFileName, result.Result);
ProjectHelpers.AddFileToProject(result.SourceFileName, result.TargetFileName);
}

Expand All @@ -84,7 +87,7 @@ private async Task<CompilerResult> ProcessResult(CompilerResult result, bool onl
await PostWritingResult(result);
}

return CompilerResult.UpdateResult(result, resultString);
return result;
}

public static string GetOrCreateGlobalSettings(string fileName)
Expand All @@ -106,12 +109,6 @@ protected virtual Task PostWritingResult(CompilerResult result)
return Task.Factory.StartNew(() => { });
}

protected virtual string PostProcessResult(string result, string targetFileName, string sourceFileName)
{
Logger.Log(ServiceName + ": " + Path.GetFileName(sourceFileName) + " compiled.");
return result;
}

protected abstract string GetPath(string sourceFileName, string targetFileName);
}
}
6 changes: 0 additions & 6 deletions EditorExtensions/Shared/Compilers/Result/CompilerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,5 @@ public static CompilerResult GenerateResult(string sourceFileName, string target
{
return new CompilerResult(sourceFileName, targetFileName, mapFileName, isSuccess, result, resultMap, errors, hasSkipped);
}

internal static CompilerResult UpdateResult(CompilerResult result, string resultString)
{
result.Result = resultString;
return result;
}
}
}
13 changes: 11 additions & 2 deletions EditorExtensions/Shared/Helpers/Css/CssUrlNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class CssUrlNormalizer : ICssSimpleTreeVisitor
{
readonly string targetFile, oldBaseDirectory;
readonly List<Tuple<TextRange, string>> replacements = new List<Tuple<TextRange, string>>();

private CssUrlNormalizer(string targetFile, string oldBasePath)
{
this.targetFile = Path.GetFullPath(targetFile);
this.oldBaseDirectory = Path.GetDirectoryName(oldBasePath);
targetFile = Path.GetFullPath(targetFile);
oldBaseDirectory = Path.GetDirectoryName(oldBasePath);
}

///<summary>Normalizes all URLs in a CSS parse tree to be relative to the specified directory.</summary>
Expand All @@ -34,6 +35,7 @@ private CssUrlNormalizer(string targetFile, string oldBasePath)
public static string NormalizeUrls(BlockItem tree, string targetFile, string oldBasePath)
{
var normalizer = new CssUrlNormalizer(targetFile, oldBasePath);

tree.Accept(normalizer);

var retVal = new StringBuilder(tree.Text);
Expand All @@ -45,6 +47,7 @@ public static string NormalizeUrls(BlockItem tree, string targetFile, string old
retVal.Remove(range.Start, range.Length);
retVal.Insert(range.Start, HttpUtility.UrlPathEncode(url));
}

return retVal.ToString();
}

Expand All @@ -55,6 +58,7 @@ public VisitItemResult Visit(ParseItem parseItem)
return VisitItemResult.Continue;

var newUrl = FixPath(DecodeStringLiteral(urlItem.UrlString.Text));

if (newUrl == null) // No change
return VisitItemResult.Continue;

Expand All @@ -63,6 +67,11 @@ public VisitItemResult Visit(ParseItem parseItem)
return VisitItemResult.Continue;
}

private string FixPathWithMapping(string p)
{
throw new NotImplementedException();
}

private static string DecodeStringLiteral(string str)
{
if ((str.StartsWith("'", StringComparison.Ordinal) && str.EndsWith("'", StringComparison.Ordinal))
Expand Down

0 comments on commit 1f5c263

Please sign in to comment.