From 7c2583bc503020836ee1106f7aece5eff8406128 Mon Sep 17 00:00:00 2001 From: Adeel Date: Fri, 26 Jun 2015 01:58:21 +0300 Subject: [PATCH] Sass: Removes post-processing path normalization. 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; --- .../JavaScript/Linters/JsHintCompiler.cs | 5 --- .../Misc/Bundles/BundleGenerator.cs | 7 ++--- .../SCSS/Compilers/ScssCompiler.cs | 31 +------------------ EditorExtensions/Settings/WESettings.cs | 19 +++++------- .../Shared/Compilers/CssCompilerBase.cs | 2 +- .../Shared/Compilers/NodeExecutorBase.cs | 19 +++++------- .../Shared/Compilers/Result/CompilerResult.cs | 6 ---- .../Shared/Helpers/Css/CssUrlNormalizer.cs | 8 +++-- 8 files changed, 27 insertions(+), 70 deletions(-) diff --git a/EditorExtensions/JavaScript/Linters/JsHintCompiler.cs b/EditorExtensions/JavaScript/Linters/JsHintCompiler.cs index 1d1617914..f8aa264fd 100644 --- a/EditorExtensions/JavaScript/Linters/JsHintCompiler.cs +++ b/EditorExtensions/JavaScript/Linters/JsHintCompiler.cs @@ -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; - } } } diff --git a/EditorExtensions/Misc/Bundles/BundleGenerator.cs b/EditorExtensions/Misc/Bundles/BundleGenerator.cs index 910f0d474..d6348a2b2 100644 --- a/EditorExtensions/Misc/Bundles/BundleGenerator.cs +++ b/EditorExtensions/Misc/Bundles/BundleGenerator.cs @@ -106,10 +106,9 @@ private async static Task CombineFiles(Dictionary 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)) { diff --git a/EditorExtensions/SCSS/Compilers/ScssCompiler.cs b/EditorExtensions/SCSS/Compilers/ScssCompiler.cs index 7dd949462..4dd52f561 100644 --- a/EditorExtensions/SCSS/Compilers/ScssCompiler.cs +++ b/EditorExtensions/SCSS/Compilers/ScssCompiler.cs @@ -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; @@ -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; - } } } diff --git a/EditorExtensions/Settings/WESettings.cs b/EditorExtensions/Settings/WESettings.cs index c9681884a..06b94ecf2 100644 --- a/EditorExtensions/Settings/WESettings.cs +++ b/EditorExtensions/Settings/WESettings.cs @@ -604,25 +604,22 @@ void OnEnableChainCompilationChanged(EventArgs e) } } - public abstract class CssChainableCompilationSettings : ChainableCompilationSettings, IChainableCompilerSettings where T : CssChainableCompilationSettings - { - [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 + public sealed class LessSettings : ChainableCompilationSettings { [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 + public sealed class ScssSettings : ChainableCompilationSettings { public enum OutputFormat { diff --git a/EditorExtensions/Shared/Compilers/CssCompilerBase.cs b/EditorExtensions/Shared/Compilers/CssCompilerBase.cs index 80a1510e7..0062d7d60 100644 --- a/EditorExtensions/Shared/Compilers/CssCompilerBase.cs +++ b/EditorExtensions/Shared/Compilers/CssCompilerBase.cs @@ -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) || diff --git a/EditorExtensions/Shared/Compilers/NodeExecutorBase.cs b/EditorExtensions/Shared/Compilers/NodeExecutorBase.cs index 13ba6053c..7f30e0c3a 100644 --- a/EditorExtensions/Shared/Compilers/NodeExecutorBase.cs +++ b/EditorExtensions/Shared/Compilers/NodeExecutorBase.cs @@ -59,16 +59,19 @@ private async Task 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); } @@ -84,7 +87,7 @@ private async Task ProcessResult(CompilerResult result, bool onl await PostWritingResult(result); } - return CompilerResult.UpdateResult(result, resultString); + return result; } public static string GetOrCreateGlobalSettings(string fileName) @@ -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); } } diff --git a/EditorExtensions/Shared/Compilers/Result/CompilerResult.cs b/EditorExtensions/Shared/Compilers/Result/CompilerResult.cs index 3dadd54df..1e08094c3 100644 --- a/EditorExtensions/Shared/Compilers/Result/CompilerResult.cs +++ b/EditorExtensions/Shared/Compilers/Result/CompilerResult.cs @@ -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; - } } } diff --git a/EditorExtensions/Shared/Helpers/Css/CssUrlNormalizer.cs b/EditorExtensions/Shared/Helpers/Css/CssUrlNormalizer.cs index 812dacd0f..daead801c 100644 --- a/EditorExtensions/Shared/Helpers/Css/CssUrlNormalizer.cs +++ b/EditorExtensions/Shared/Helpers/Css/CssUrlNormalizer.cs @@ -12,10 +12,11 @@ class CssUrlNormalizer : ICssSimpleTreeVisitor { readonly string targetFile, oldBaseDirectory; readonly List> replacements = new List>(); + private CssUrlNormalizer(string targetFile, string oldBasePath) { - this.targetFile = Path.GetFullPath(targetFile); - this.oldBaseDirectory = Path.GetDirectoryName(oldBasePath); + targetFile = Path.GetFullPath(targetFile); + oldBaseDirectory = Path.GetDirectoryName(oldBasePath); } ///Normalizes all URLs in a CSS parse tree to be relative to the specified directory. @@ -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); @@ -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(); } @@ -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;