Skip to content

Commit 77d05c3

Browse files
Krusenmadskristensen
authored andcommitted
Autoprefixer support for SASS compiler (#307)
* Fix source map decoding for babel compiler output Babel compiler source mapping url contains `charset=utf-8;` before `base64,` part * Update babel compiler arguments to work with Node 6 Breaking changes to `path` between Node 5 and 6: https://github.com/nodejs/node/wiki/Breaking-changes-between-v5-and-v6#path * Update Node to 6.10.3 (32 bit) * Fix wrong paths in scss source maps Also fixes issue with node-sass on Node 6 (breaking changes to `path` module) * Add autoprefix support to scss compiler * Update appveyor image to Visual Studio 2017 * Clean up after minify tests
1 parent 57faa1b commit 77d05c3

File tree

14 files changed

+67
-18
lines changed

14 files changed

+67
-18
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
os: Visual Studio 2017 RC
1+
image: Visual Studio 2017
22

33
install:
44
- ps: (new-object Net.WebClient).DownloadString("https://raw.github.com/madskristensen/ExtensionScripts/master/AppVeyor/vsix.ps1") | iex

build/build.cmd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ call npm install --quiet ^
1616
less-plugin-autoprefix ^
1717
less-plugin-csscomb ^
1818
node-sass ^
19+
postcss-cli ^
20+
autoprefixer ^
1921
stylus ^
2022
handlebars ^
2123
> nul
2224

23-
if not exist "node_modules\node-sass\vendor\win32-ia32-14" (
25+
if not exist "node_modules\node-sass\vendor\win32-ia32-48" (
2426
echo Copying node binding...
25-
md "node_modules\node-sass\vendor\win32-ia32-14"
26-
copy binding.node "node_modules\node-sass\vendor\win32-ia32-14"
27+
md "node_modules\node-sass\vendor\win32-ia32-48"
28+
copy binding.node "node_modules\node-sass\vendor\win32-ia32-48"
2729
)
2830

2931
echo Deleting unneeded files and folders...

src/WebCompiler/Compile/BabelCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void RunCompilerProcess(Config config, FileInfo info)
109109
private static string ConstructArguments(Config config)
110110
{
111111
//string relative = FileHelpers.MakeRelative(config.GetAbsoluteOutputFile().FullName, config.GetAbsoluteInputFile().FullName);
112-
string arguments = $"--presets react";
112+
string arguments = $"--presets react --out-file \"\"";
113113

114114
var options = BabelOptions.FromConfig(config);
115115

src/WebCompiler/Compile/SassCompiler.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,21 @@ private void RunCompilerProcess(Config config, FileInfo info)
8888
WindowStyle = ProcessWindowStyle.Hidden,
8989
CreateNoWindow = true,
9090
FileName = "cmd.exe",
91-
Arguments = $"/c \"\"{Path.Combine(_path, "node_modules\\.bin\\node-sass.cmd")}\" {arguments} \"{info.FullName}\"\"",
91+
Arguments = $"/c \"\"{Path.Combine(_path, "node_modules\\.bin\\node-sass.cmd")}\" {arguments} \"{info.FullName}\" \"",
9292
StandardOutputEncoding = Encoding.UTF8,
9393
StandardErrorEncoding = Encoding.UTF8,
9494
RedirectStandardOutput = true,
9595
RedirectStandardError = true,
9696
};
9797

98+
// Pipe output from node-sass to postcss if autoprefix option is set
99+
SassOptions options = SassOptions.FromConfig(config);
100+
if (!string.IsNullOrEmpty(options.AutoPrefix))
101+
{
102+
start.Arguments = start.Arguments.TrimEnd('"') + $" | \"{Path.Combine(_path, "node_modules\\.bin\\postcss.cmd")}\" --use autoprefixer\"";
103+
start.EnvironmentVariables.Add("BROWSERSLIST", options.AutoPrefix);
104+
}
105+
98106
start.EnvironmentVariables["PATH"] = _path + ";" + start.EnvironmentVariables["PATH"];
99107

100108
using (Process p = Process.Start(start))
@@ -104,7 +112,9 @@ private void RunCompilerProcess(Config config, FileInfo info)
104112
p.WaitForExit();
105113

106114
_output = stdout.Result;
107-
_error = stderr.Result;
115+
// postcss outputs "√ Finished stdin (##ms)" to stderr for some reason
116+
if (!stderr.Result.StartsWith("√"))
117+
_error = stderr.Result;
108118
}
109119
}
110120

@@ -115,7 +125,7 @@ private static string ConstructArguments(Config config)
115125
SassOptions options = SassOptions.FromConfig(config);
116126

117127
if (options.SourceMap || config.SourceMap)
118-
arguments += " --source-map=false --source-map-embed=true";
128+
arguments += " --source-map-embed=true";
119129

120130
arguments += " --precision=" + options.Precision;
121131

src/WebCompiler/Compile/SassOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ protected override void LoadSettings(Config config)
2020
{
2121
base.LoadSettings(config);
2222

23+
var autoPrefix = GetValue(config, "autoPrefix");
24+
if (autoPrefix != null)
25+
AutoPrefix = autoPrefix;
26+
2327
if (config.Options.ContainsKey("outputStyle"))
2428
OutputStyle = config.Options["outputStyle"].ToString();
2529

@@ -55,6 +59,13 @@ protected override string CompilerFileName
5559
get { return "sass"; }
5660
}
5761

62+
/// <summary>
63+
/// Autoprefixer will use the data based on current browser popularity and
64+
/// property support to apply prefixes for you.
65+
/// </summary>
66+
[JsonProperty("autoPrefix")]
67+
public string AutoPrefix { get; set; } = "";
68+
5869
/// <summary>
5970
/// Path to look for imported files
6071
/// </summary>

src/WebCompiler/Node/binding.node

194 KB
Binary file not shown.

src/WebCompiler/Node/node.7z

1.19 MB
Binary file not shown.

src/WebCompilerTest/Compile/ScssTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using Microsoft.VisualStudio.TestTools.UnitTesting;
67
using WebCompiler;
78

@@ -35,9 +36,10 @@ public void CompileScss()
3536
Assert.IsTrue(File.Exists("../../artifacts/scss/test.css"));
3637
Assert.IsTrue(first.CompiledContent.Contains("/*# sourceMappingURL=data:"));
3738
Assert.IsTrue(result.ElementAt(1).CompiledContent.Contains("url(foo.png)"));
39+
Assert.IsTrue(result.ElementAt(1).CompiledContent.Contains("-webkit-animation"), "AutoPrefix");
3840

3941
string sourceMap = DecodeSourceMap(first.CompiledContent);
40-
Assert.IsTrue(sourceMap.Contains("../scss/test.scss"), "Source map paths");
42+
Assert.IsTrue(sourceMap.Contains("scss/test.scss"), "Source map paths");
4143
}
4244

4345
[TestMethod, TestCategory("SCSS")]
@@ -80,10 +82,10 @@ public void MultiLineComments()
8082

8183
public static string DecodeSourceMap(string content)
8284
{
83-
string ident = "sourceMappingURL=data:application/json;base64,";
84-
if (content.Contains(ident))
85+
Match match = Regex.Match(content, @"sourceMappingURL=data:application/json;.*base64,");
86+
if (match.Success)
8587
{
86-
int start = content.IndexOf(ident) + ident.Length;
88+
int start = match.Index + match.Length;
8789
string map = content.Substring(start).Trim('*', '/');
8890
byte[] data = Convert.FromBase64String(map);
8991
return Encoding.UTF8.GetString(data);

src/WebCompilerTest/Minify/CssMinifierTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public class CssMinifierTests
1111
{
1212
private const string processingConfigFile = "../../Minify/artifacts/css/";
1313

14+
[TestCleanup]
15+
public void Cleanup()
16+
{
17+
File.Delete("../../Minify/artifacts/css/site.css");
18+
File.Delete("../../Minify/artifacts/css/site.min.css");
19+
}
20+
1421
/// <summary>
1522
/// Tests that '.min' is automatically appended to a minified file name.
1623
/// </summary>

src/WebCompilerTest/Minify/JavaScriptMinifierTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public class JavaScriptMinifierTests
1111
{
1212
private const string processingConfigFile = "../../Minify/artifacts/javascript/";
1313

14+
[TestCleanup]
15+
public void Cleanup()
16+
{
17+
File.Delete("../../Minify/artifacts/javascript/site.js");
18+
File.Delete("../../Minify/artifacts/javascript/site.min.js");
19+
}
20+
1421
/// <summary>
1522
/// Tests that '.min' is automatically appended to a minified file name.
1623
/// </summary>

0 commit comments

Comments
 (0)