Skip to content

Add Tailwind CSS support #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for changes and roadmap.

### Getting started

Right-click any `.less`, `.scss`, `.styl`, `.jsx`, `.es6` or `.coffee` file in Solution Explorer to
Right-click any `.less`, `.scss`, `.styl`, `.jsx`, `.es6`, `.coffee`, or `.css` file in Solution Explorer to
setup compilation.

![Compile file](art/contextmenu-compile.png)
Expand All @@ -46,7 +46,7 @@ run all the configured compilers.

### Compile on save

Any time a `.less`, `.scss`, `.styl`, `.jsx`, `.es6` or `.coffee` file is modified within
Any time a `.less`, `.scss`, `.styl`, `.jsx`, `.es6`, `.coffee`, or `.css` file is modified within
Visual Studio, the compiler runs automatically to produce the compiled output file.

The same is true when saving the `compilerconfig.json` file where
Expand Down Expand Up @@ -133,7 +133,18 @@ Here's an example of what that file looks like:
"options":{
"sourceMap": true
}
},
{
"outputFile": "output/tailwind.css",
"inputFile": "input/tailwind.css",
"minify": {
"enabled": true
},
"includeInProject": true,
"options":{
"sourceMap": true
}
}
]
```
Default values for `compilerconfig.json` can be found in the `compilerconfig.json.defaults` file in the same location.
Default values for `compilerconfig.json` can be found in the `compilerconfig.json.defaults` file in the same location.
6 changes: 5 additions & 1 deletion src/WebCompiler/Compile/CompilerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class CompilerService
private static object _syncRoot = new object(); // Used to lock on the initialize step

/// <summary>A list of allowed file extensions.</summary>
public static readonly string[] AllowedExtensions = new[] { ".LESS", ".SCSS", ".SASS", ".STYL", ".COFFEE", ".ICED", ".JS", ".JSX", ".ES6", ".HBS", ".HANDLEBARS" };
public static readonly string[] AllowedExtensions = new[] { ".LESS", ".SCSS", ".SASS", ".STYL", ".COFFEE", ".ICED", ".JS", ".JSX", ".ES6", ".HBS", ".HANDLEBARS", ".CSS" };

/// <summary>
/// Test if a file type is supported by the compilers.
Expand Down Expand Up @@ -67,6 +67,10 @@ internal static ICompiler GetCompiler(Config config)
case ".ES6":
compiler = new BabelCompiler(_path);
break;

case ".CSS":
compiler = new TailwindCompiler(_path);
break;
}

return compiler;
Expand Down
121 changes: 121 additions & 0 deletions src/WebCompiler/Compile/TailwindCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace WebCompiler
{
class TailwindCompiler : ICompiler
{
private static Regex _errorRx = new Regex(@".+\.css:\s(?<message>.+)\((?<line>[0-9]+):(?<column>[0-9]+)\)", RegexOptions.Compiled);
private string _path;
private string _output = string.Empty;
private string _error = string.Empty;

public TailwindCompiler(string path)
{
_path = path;
}

public CompilerResult Compile(Config config)
{
string baseFolder = Path.GetDirectoryName(config.FileName);
string inputFile = Path.Combine(baseFolder, config.InputFile);

FileInfo info = new FileInfo(inputFile);
string content = File.ReadAllText(info.FullName);

CompilerResult result = new CompilerResult
{
FileName = info.FullName,
OriginalContent = content,
};

try
{
RunCompilerProcess(config, info);

result.CompiledContent = _output;

if (_error.Length > 0)
{
CompilerError ce = new CompilerError
{
FileName = info.FullName,
Message = _error.Replace(baseFolder, string.Empty),
IsWarning = !string.IsNullOrEmpty(_output)
};

var match = _errorRx.Match(_error);

if (match.Success)
{
ce.Message = match.Groups["message"].Value.Replace(baseFolder, string.Empty);
ce.LineNumber = int.Parse(match.Groups["line"].Value);
ce.ColumnNumber = int.Parse(match.Groups["column"].Value);
}

result.Errors.Add(ce);
}
}
catch (Exception ex)
{
CompilerError error = new CompilerError
{
FileName = info.FullName,
Message = string.IsNullOrEmpty(_error) ? ex.Message : _error,
LineNumber = 0,
ColumnNumber = 0,
};

result.Errors.Add(error);
}

return result;
}

private void RunCompilerProcess(Config config, FileInfo info)
{
string arguments = ConstructArguments(config);

ProcessStartInfo start = new ProcessStartInfo
{
WorkingDirectory = info.Directory.FullName,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = $"/c \"\"{Path.Combine(_path, "node_modules\\.bin\\tailwind.cmd")}\" {arguments} \"{info.FullName}\"\"",
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
RedirectStandardOutput = true,
RedirectStandardError = true,
};

start.EnvironmentVariables["PATH"] = _path + ";" + start.EnvironmentVariables["PATH"];

using (Process p = Process.Start(start))
{
var stdout = p.StandardOutput.ReadToEndAsync();
var stderr = p.StandardError.ReadToEndAsync();
p.WaitForExit();

_output = stdout.Result;
_error = stderr.Result;
}
}

private static string ConstructArguments(Config config)
{
string arguments = "build";

var options = TailwindOptions.FromConfig(config);

if (options.SourceMap || config.SourceMap)
arguments += " --source-maps";

return arguments;
}
}
}
1 change: 1 addition & 0 deletions src/WebCompiler/Config/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void CreateDefaultsFile(string fileName)
babel = new BabelOptions(),
coffeescript = new IcedCoffeeScriptOptions(),
handlebars = new HandlebarsOptions(),
tailwindcss = new TailwindOptions(),
},
minifiers = new
{
Expand Down