Skip to content

Commit 64fc7e4

Browse files
Support comma separated imports
Fixed #191
1 parent 187a65b commit 64fc7e4

File tree

6 files changed

+64
-39
lines changed

6 files changed

+64
-39
lines changed
Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text.RegularExpressions;
45

56
namespace WebCompiler
67
{
@@ -29,7 +30,7 @@ public override void UpdateFileDependencies(string path)
2930
if (this.Dependencies != null)
3031
{
3132
path = path.ToLowerInvariant();
32-
33+
3334
if (!Dependencies.ContainsKey(path))
3435
Dependencies[path] = new Dependencies();
3536

@@ -49,60 +50,72 @@ public override void UpdateFileDependencies(string path)
4950
string content = File.ReadAllText(info.FullName);
5051

5152
//match both <@import "myFile.scss";> and <@import url("myFile.scss");> syntax
52-
var matches = System.Text.RegularExpressions.Regex.Matches(content, "@import([\\s]+)(\\([\\S]+\\)([\\s]+))?(url\\()?('|\"|)(?<url>[^'\"\\):?:]+)('|\"|\\))");
53-
foreach (System.Text.RegularExpressions.Match match in matches)
53+
var matches = Regex.Matches(content, @"(?<=^@import(?:[\s]+))(?:(?:\(\w+\)))?\s*(?:url)?(?<url>.+?)$", RegexOptions.Multiline);
54+
foreach (Match match in matches)
5455
{
55-
FileInfo importedfile = GetFileInfo(info, match);
56-
57-
if (importedfile == null)
58-
continue;
56+
var importedfiles = GetFileInfos(info, match);
5957

60-
//if the file doesn't end with the correct extension, an import statement without extension is probably used, to re-add the extension (#175)
61-
if (string.Compare(importedfile.Extension, FileExtension, StringComparison.OrdinalIgnoreCase) != 0)
58+
foreach (FileInfo importedfile in importedfiles)
6259
{
63-
importedfile = new FileInfo(importedfile.FullName + this.FileExtension);
64-
}
60+
if (importedfile == null)
61+
continue;
6562

66-
var dependencyFilePath = importedfile.FullName.ToLowerInvariant();
63+
var theFile = importedfile;
6764

68-
if (!File.Exists(dependencyFilePath))
69-
{
70-
// Trim leading underscore to support Sass partials
71-
var dir = Path.GetDirectoryName(dependencyFilePath);
72-
var fileName = Path.GetFileName(dependencyFilePath);
73-
var cleanPath = Path.Combine(dir, "_" + fileName);
65+
//if the file doesn't end with the correct extension, an import statement without extension is probably used, to re-add the extension (#175)
66+
if (string.Compare(importedfile.Extension, FileExtension, StringComparison.OrdinalIgnoreCase) != 0)
67+
{
68+
theFile = new FileInfo(importedfile.FullName + this.FileExtension);
69+
}
7470

75-
if (!File.Exists(cleanPath))
76-
continue;
71+
var dependencyFilePath = theFile.FullName.ToLowerInvariant();
7772

78-
dependencyFilePath = cleanPath;
79-
}
73+
if (!File.Exists(dependencyFilePath))
74+
{
75+
// Trim leading underscore to support Sass partials
76+
var dir = Path.GetDirectoryName(dependencyFilePath);
77+
var fileName = Path.GetFileName(dependencyFilePath);
78+
var cleanPath = Path.Combine(dir, "_" + fileName);
79+
80+
if (!File.Exists(cleanPath))
81+
continue;
82+
83+
dependencyFilePath = cleanPath;
84+
}
8085

81-
if (!Dependencies[path].DependentOn.Contains(dependencyFilePath))
82-
Dependencies[path].DependentOn.Add(dependencyFilePath);
86+
if (!Dependencies[path].DependentOn.Contains(dependencyFilePath))
87+
Dependencies[path].DependentOn.Add(dependencyFilePath);
8388

84-
if (!Dependencies.ContainsKey(dependencyFilePath))
85-
Dependencies[dependencyFilePath] = new Dependencies();
89+
if (!Dependencies.ContainsKey(dependencyFilePath))
90+
Dependencies[dependencyFilePath] = new Dependencies();
8691

87-
if (!Dependencies[dependencyFilePath].DependentFiles.Contains(path))
88-
Dependencies[dependencyFilePath].DependentFiles.Add(path);
92+
if (!Dependencies[dependencyFilePath].DependentFiles.Contains(path))
93+
Dependencies[dependencyFilePath].DependentFiles.Add(path);
94+
}
8995
}
9096
}
9197
}
9298

93-
private static FileInfo GetFileInfo(FileInfo info, System.Text.RegularExpressions.Match match)
99+
private static IEnumerable<FileInfo> GetFileInfos(FileInfo info, System.Text.RegularExpressions.Match match)
94100
{
95-
string url = match.Groups["url"].Value;
101+
string url = match.Groups["url"].Value.Replace("'", "\"").Replace("(", "").Replace(")", "").Replace(";", "").Trim();
102+
var list = new List<FileInfo>();
96103

97-
try
98-
{
99-
return new FileInfo(Path.Combine(info.DirectoryName, match.Groups["url"].Value));
100-
}
101-
catch (Exception)
104+
foreach (string name in url.Split(new[] { "\"," }, StringSplitOptions.RemoveEmptyEntries))
102105
{
103-
// Not a valid file name
104-
return null;
106+
try
107+
{
108+
string value = name.Replace("\"", "").Replace("/", "\\").Trim();
109+
list.Add(new FileInfo(Path.Combine(info.DirectoryName, value)));
110+
}
111+
catch (Exception ex)
112+
{
113+
// Not a valid file name
114+
System.Diagnostics.Debug.Write(ex);
115+
}
105116
}
117+
118+
return list;
106119
}
107120
}
108121
}

src/WebCompilerTest/Compile/ScssTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public void AssociateExtensionSourceFileChangedTest()
5757
Assert.IsTrue(File.Exists("../../artifacts/scss/test.css"));
5858
}
5959

60+
[TestMethod, TestCategory("SCSS")]
61+
public void CommaListOfImportsSourcefileChanged()
62+
{
63+
var result = _processor.SourceFileChanged(new FileInfo("../../artifacts/scssconfig.json").FullName, new FileInfo("../../artifacts/scss/sub/foo.scss").FullName, new DirectoryInfo("../../artifacts/").FullName);
64+
Assert.AreEqual(1, result.Count<CompilerResult>());
65+
Assert.IsTrue(File.Exists("../../artifacts/scss/test.css"));
66+
}
67+
6068
[TestMethod, TestCategory("SCSS")]
6169
public void OtherExtensionTypeSourceFileChangedTest()
6270
{

src/WebCompilerTest/WebCompilerTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<ItemGroup>
7474
<Content Include="artifacts\less\sub\logo.png" />
7575
<None Include="artifacts\coffeeconfig.json.defaults" />
76+
<None Include="artifacts\scss\sub\foo.scss" />
7677
<None Include="artifacts\stylusconfig.json" />
7778
<None Include="artifacts\less\sub\relative.less" />
7879
<None Include="artifacts\babelconfigError.json" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
background: url(../foo.png);
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
body {
1+
div {
22
background: url(../foo.png);
33
}

src/WebCompilerTest/artifacts/scss/test.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import "variables";
1+
@import "variables", "sub/foo";
22

33
body {
44
background-color: $background;

0 commit comments

Comments
 (0)