Skip to content
This repository
Browse code

adding preprocessor support for arbitrary content (continue #170)

  • Loading branch information...
commit 4046ff4174839a073676bcdfab7c874d3c6065cc 1 parent eaef909
Alex Ullrich authored
2  SquishIt.Framework/Base/BundleBase.cs
@@ -155,7 +155,7 @@ protected string PreprocessFile(string file, IEnumerable<IPreprocessor> preproce
155 155 }
156 156 }
157 157
158   - string PreprocessContent(string file, IEnumerable<IPreprocessor> preprocessors, string content)
  158 + protected string PreprocessContent(string file, IEnumerable<IPreprocessor> preprocessors, string content)
159 159 {
160 160 if(preprocessors == null)
161 161 {
10 SquishIt.Framework/Css/CssBundle.cs
... ... @@ -1,4 +1,3 @@
1   -using System;
2 1 using System.Collections.Generic;
3 2 using System.IO;
4 3 using System.Linq;
@@ -6,7 +5,6 @@
6 5 using System.Text.RegularExpressions;
7 6 using SquishIt.Framework.Base;
8 7 using SquishIt.Framework.Minifiers;
9   -using SquishIt.Framework.Minifiers.CSS;
10 8 using SquishIt.Framework.Resolvers;
11 9 using SquishIt.Framework.Files;
12 10 using SquishIt.Framework.Utilities;
@@ -109,11 +107,15 @@ public CSSBundle AppendHashForAssets()
109 107
110 108 protected override string BeforeMinify(string outputFile, List<string> filePaths, IEnumerable<ArbitraryContent> arbitraryContent)
111 109 {
  110 + //TODO: refactor so that this is common code and ProcessCSSFile/ProcessJavascriptFile are a single abstract method called here
112 111 var outputCss = new StringBuilder();
113 112
114   - //TODO: deal w/ arbitrary extensions
115 113 filePaths.Select(file => ProcessCssFile(file, outputFile))
116   - .Concat(arbitraryContent.Select(ac => ac.Content))
  114 + .Concat(arbitraryContent.Select(ac => {
  115 + var filename = "dummy." + ac.Extension;
  116 + var preprocessors = FindPreprocessors(filename);
  117 + return PreprocessContent(filename, preprocessors, ac.Content);
  118 + }))
117 119 .Aggregate(outputCss, (builder, val) => builder.Append(val + "\n"));
118 120
119 121 return outputCss.ToString();
29 SquishIt.Framework/JavaScript/JavaScriptBundle.cs
@@ -28,7 +28,7 @@ protected override IEnumerable<string> allowedExtensions
28 28 get { return instanceAllowedExtensions.Union(Bundle.AllowedGlobalExtensions.Union(Bundle.AllowedScriptExtensions)); }
29 29 }
30 30
31   - protected override IEnumerable<string> disallowedExtensions
  31 + protected override IEnumerable<string> disallowedExtensions
32 32 {
33 33 get { return Bundle.AllowedStyleExtensions; }
34 34 }
@@ -64,16 +64,16 @@ protected override string CachePrefix
64 64
65 65 internal override void BeforeRenderDebug()
66 66 {
67   - foreach (var asset in bundleState.Assets)
  67 + foreach(var asset in bundleState.Assets)
68 68 {
69 69 var localPath = asset.LocalPath;
70 70 var preprocessors = FindPreprocessors(localPath);
71   - if (preprocessors != null && preprocessors.Count() > 0)
  71 + if(preprocessors != null && preprocessors.Count() > 0)
72 72 {
73 73 string outputFile = FileSystem.ResolveAppRelativePathToFileSystem(localPath);
74 74 string javascript = PreprocessJavascriptFile(outputFile, preprocessors);
75 75 outputFile += ".debug.js";
76   - using (var fileWriter = fileWriterFactory.GetFileWriter(outputFile))
  76 + using(var fileWriter = fileWriterFactory.GetFileWriter(outputFile))
77 77 {
78 78 fileWriter.Write(javascript);
79 79 }
@@ -85,25 +85,34 @@ internal override void BeforeRenderDebug()
85 85
86 86 protected override string BeforeMinify(string outputFile, List<string> files, IEnumerable<ArbitraryContent> arbitraryContent)
87 87 {
  88 + //TODO: refactor so that this is common code and ProcessCSSFile/ProcessJavascriptFile are a single abstract method called here
88 89 var sb = new StringBuilder();
89   - //TODO: deal w/ arbitrary extensions
  90 +
90 91 files.Select(ProcessJavascriptFile)
91   - .Concat(arbitraryContent.Select(ac => ac.Content))
  92 + .Concat(arbitraryContent.Select(ac => {
  93 + var filename = "dummy." + ac.Extension;
  94 + var preprocessors = FindPreprocessors(filename);
  95 + return PreprocessContent(filename, preprocessors, ac.Content);
  96 + }))
92 97 .Aggregate(sb, (builder, val) => builder.Append(val + "\n"));
93 98
94 99 return sb.ToString();
95 100 }
96 101
97   - private string ProcessJavascriptFile(string file) {
  102 + private string ProcessJavascriptFile(string file)
  103 + {
98 104 var preprocessors = FindPreprocessors(file);
99   - if (preprocessors != null) {
  105 + if(preprocessors != null)
  106 + {
100 107 return PreprocessJavascriptFile(file, preprocessors);
101 108 }
102 109 return ReadFile(file);
103 110 }
104 111
105   - private string PreprocessJavascriptFile(string file, IEnumerable<IPreprocessor> preprocessors) {
106   - lock (typeof(JavaScriptBundle)) {
  112 + private string PreprocessJavascriptFile(string file, IEnumerable<IPreprocessor> preprocessors)
  113 + {
  114 + lock(typeof(JavaScriptBundle))
  115 + {
107 116 return PreprocessFile(file, preprocessors);
108 117 }
109 118 }
62 SquishIt.Tests/Coffee/CoffeescriptCompilerTests.cs
... ... @@ -1,62 +0,0 @@
1   -using System;
2   -using NUnit.Framework;
3   -using SquishIt.CoffeeScript.Coffee;
4   -
5   -namespace SquishIt.Tests.Coffee
6   -{
7   - [TestFixture]
8   - public class CoffeescriptCompilerTests
9   - {
10   - [Test, Platform (Exclude = "Unix, Linux, Mono")]
11   - public void CompileWithSimpleAlertSucceeds ()
12   - {
13   - var compiler = new CoffeeScriptCompiler ();
14   -
15   - string result = compiler.Compile ("alert 'test' ");
16   -
17   - Assert.AreEqual ("(function() {\n alert('test');\n}).call(this);\n", result);
18   - }
19   -
20   - [Test, Platform (Exclude = "Unix, Linux, Mono")]
21   - public void CompileWithComplexScriptSucceeds ()
22   - {
23   - string source = @"# Assignment:
24   -number = 42
25   -opposite = true
26   -
27   -# Conditions:
28   -number = -42 if opposite
29   -
30   -# Functions:
31   -square = (x) -> x * x
32   -
33   -# Arrays:
34   -list = [1, 2, 3, 4, 5]
35   -
36   -# Objects:
37   -math =
38   - root: Math.sqrt
39   - square: square
40   - cube: (x) -> x * square x
41   -
42   -# Splats:
43   -race = (winner, runners...) ->
44   - print winner, runners
45   -
46   -# Existence:
47   -alert 'I knew it!' if elvis?";
48   -
49   - var compiler = new CoffeeScriptCompiler ();
50   - string result = compiler.Compile (source);
51   - }
52   -
53   - [Test, Platform (Include = "Unix, Linux, Mono")]
54   - public void CompileFailsGracefullyOnMono ()
55   - {
56   - var compiler = new CoffeeScriptCompiler();
57   - var exception = Assert.Throws(typeof (NotSupportedException), () => compiler.Compile(""));
58   - Assert.AreEqual("Coffeescript not yet supported for mono.", exception.Message);
59   - }
60   -
61   - }
62   -}
101 SquishIt.Tests/CoffeeScriptTests.cs
... ... @@ -0,0 +1,101 @@
  1 +using System;
  2 +using NUnit.Framework;
  3 +using SquishIt.CoffeeScript;
  4 +using SquishIt.CoffeeScript.Coffee;
  5 +using SquishIt.Framework;
  6 +using SquishIt.Framework.Files;
  7 +using SquishIt.Framework.Utilities;
  8 +using SquishIt.Tests.Helpers;
  9 +
  10 +namespace SquishIt.Tests
  11 +{
  12 + [TestFixture]
  13 + public class CoffeeScriptTests
  14 + {
  15 + //TODO: should probably have more tests here
  16 + private JavaScriptBundleFactory javaScriptBundleFactory;
  17 + private IHasher hasher;
  18 +
  19 + [SetUp]
  20 + public void Setup()
  21 + {
  22 + javaScriptBundleFactory = new JavaScriptBundleFactory();
  23 + var retryableFileOpener = new RetryableFileOpener();
  24 + hasher = new Hasher(retryableFileOpener);
  25 + }
  26 +
  27 + [Test]
  28 + public void CanBundleJavascriptWithArbitraryCoffeeScript()
  29 + {
  30 + var coffee = "alert 'test' ";
  31 +
  32 + var tag = javaScriptBundleFactory
  33 + .WithDebuggingEnabled(false)
  34 + .Create()
  35 + .WithPreprocessor(new CoffeeScriptPreprocessor())
  36 + .AddString(coffee, ".coffee")
  37 + .Render("~/brewed.js");
  38 +
  39 + var compiled = javaScriptBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"brewed.js")];
  40 +
  41 + Assert.AreEqual(@"(function(){alert(""test"")}).call(this)", compiled);
  42 + Assert.AreEqual(@"<script type=""text/javascript"" src=""brewed.js?r=hash""></script>", tag);
  43 + }
  44 + }
  45 +
  46 + [TestFixture]
  47 + public class CoffeescriptCompilerTests
  48 + {
  49 + [Test, Platform(Exclude = "Unix, Linux, Mono")]
  50 + public void CompileWithSimpleAlertSucceeds()
  51 + {
  52 + var compiler = new CoffeeScriptCompiler();
  53 +
  54 + string result = compiler.Compile("alert 'test' ");
  55 +
  56 + Assert.AreEqual("(function() {\n alert('test');\n}).call(this);\n", result);
  57 + }
  58 +
  59 + [Test, Platform(Exclude = "Unix, Linux, Mono")]
  60 + public void CompileWithComplexScriptSucceeds()
  61 + {
  62 + string source = @"# Assignment:
  63 +number = 42
  64 +opposite = true
  65 +
  66 +# Conditions:
  67 +number = -42 if opposite
  68 +
  69 +# Functions:
  70 +square = (x) -> x * x
  71 +
  72 +# Arrays:
  73 +list = [1, 2, 3, 4, 5]
  74 +
  75 +# Objects:
  76 +math =
  77 + root: Math.sqrt
  78 + square: square
  79 + cube: (x) -> x * square x
  80 +
  81 +# Splats:
  82 +race = (winner, runners...) ->
  83 + print winner, runners
  84 +
  85 +# Existence:
  86 +alert 'I knew it!' if elvis?";
  87 +
  88 + var compiler = new CoffeeScriptCompiler();
  89 + string result = compiler.Compile(source);
  90 + }
  91 +
  92 + [Test, Platform(Include = "Unix, Linux, Mono")]
  93 + public void CompileFailsGracefullyOnMono()
  94 + {
  95 + var compiler = new CoffeeScriptCompiler();
  96 + var exception = Assert.Throws(typeof(NotSupportedException), () => compiler.Compile(""));
  97 + Assert.AreEqual("Coffeescript not yet supported for mono.", exception.Message);
  98 + }
  99 +
  100 + }
  101 +}
20 SquishIt.Tests/LessTests.cs
@@ -55,6 +55,26 @@ public class LessTests
55 55 }
56 56
57 57 [Test]
  58 + public void CanBundleArbitraryLessContent ()
  59 + {
  60 + var tag = cssBundleFactory
  61 + .WithHasher(hasher)
  62 + .WithDebuggingEnabled(false)
  63 + .Create()
  64 + .WithPreprocessor(new LessPreprocessor())
  65 + .AddString(cssLess, ".less")
  66 + .Render("~/css/output.css");
  67 +
  68 + var contents =
  69 + cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
  70 +
  71 +
  72 + Assert.AreEqual("#header{color:#4d926f}h2{color:#4d926f}", contents);
  73 +
  74 + Assert.AreEqual ("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/output.css?r=15D3D9555DEFACE69D6AB9E7FD972638\" />", tag);
  75 + }
  76 +
  77 + [Test]
58 78 public void CanBundleCssWithLessAndPathRewrites () {
59 79 using (new StylePreprocessorScope<LessPreprocessor> ()) {
60 80 string css =
79 SquishIt.Tests/SassTests.cs
@@ -69,6 +69,47 @@ public void CanBundleCssWithScss()
69 69 }
70 70 }
71 71
  72 +
  73 + [Test]
  74 + public void CanBundleCssWithArbitraryScss()
  75 + {
  76 + using(new StylePreprocessorScope<SassPreprocessor>())
  77 + {
  78 + var original =
  79 + @"$blue: #3bbfce;
  80 + $margin: 16px;
  81 +
  82 + .content-navigation {
  83 + border-color: $blue;
  84 + color:
  85 + darken($blue, 9%);
  86 + }
  87 +
  88 + .border {
  89 + padding: $margin / 2;
  90 + margin: $margin / 2;
  91 + border-color: $blue;
  92 + }";
  93 +
  94 + var expected =
  95 + @".content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}";
  96 +
  97 + var cssBundle = cssBundleFactory
  98 + .WithHasher(hasher)
  99 + .WithDebuggingEnabled(false)
  100 + .Create();
  101 +
  102 + string tag = cssBundle
  103 + .AddString(original, ".scss")
  104 + .Render("~/css/output.css");
  105 +
  106 + string contents = cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
  107 +
  108 + Assert.AreEqual(@"<link rel=""stylesheet"" type=""text/css"" href=""css/output.css?r=5C851B7837C923C399A44B1F5BF9F14A"" />", tag);
  109 + Assert.AreEqual(expected, contents);
  110 + }
  111 + }
  112 +
72 113 [Test]
73 114 public void CanBundleCssWithSass()
74 115 {
@@ -111,6 +152,44 @@ public void CanBundleCssWithSass()
111 152 }
112 153
113 154 [Test]
  155 + public void CanBundleCssWithArbitrarySass()
  156 + {
  157 + using(new StylePreprocessorScope<SassPreprocessor>())
  158 + {
  159 + var original =
  160 +@"$blue: #3bbfce
  161 +$margin: 16px
  162 +
  163 +.content-navigation
  164 + border-color: $blue
  165 + color: darken($blue, 9%)
  166 +
  167 +.border
  168 + padding: $margin / 2
  169 + margin: $margin / 2
  170 + border-color: $blue";
  171 +
  172 + var expected =
  173 + @".content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}";
  174 +
  175 + var cssBundle = cssBundleFactory
  176 + .WithHasher(hasher)
  177 + .WithDebuggingEnabled(false)
  178 + .Create();
  179 +
  180 + string tag = cssBundle
  181 + .AddString(original, ".sass")
  182 + .Render("~/css/output.css");
  183 +
  184 + string contents =
  185 + cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
  186 +
  187 + Assert.AreEqual(@"<link rel=""stylesheet"" type=""text/css"" href=""css/output.css?r=5C851B7837C923C399A44B1F5BF9F14A"" />", tag);
  188 + Assert.AreEqual(expected, contents);
  189 + }
  190 + }
  191 +
  192 + [Test]
114 193 public void CanUseNesting()
115 194 {
116 195 using (new StylePreprocessorScope<SassPreprocessor>())
2  SquishIt.Tests/SquishIt.Tests.csproj
@@ -87,7 +87,7 @@
87 87 </ItemGroup>
88 88 <ItemGroup>
89 89 <Compile Include="AssetsFileHasherTests.cs" />
90   - <Compile Include="Coffee\CoffeescriptCompilerTests.cs" />
  90 + <Compile Include="CoffeeScriptTests.cs" />
91 91 <Compile Include="ScriptPreprocessorPipelineTests.cs" />
92 92 <Compile Include="CssBundleTests.cs" />
93 93 <Compile Include="CssCompressorsTests.cs" />

0 comments on commit 4046ff4

Please sign in to comment.
Something went wrong with that request. Please try again.