Browse files

Extending preprocessor hooks to handle coffeescript as well

  • Loading branch information...
1 parent 6734dc8 commit 9bb650fd068fbd2bde40c5430f6aafc51a3591e1 @AlexCuse committed Nov 17, 2011
View
2 SquishIt (2010).sln
@@ -11,7 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquishItAspNetTest", "Squis
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquishIt.Mvc", "SquishIt.Mvc\SquishIt.Mvc.csproj", "{A9F9331C-C3AA-4844-AB33-7714761F725D}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquishIt.Less", "SquishIt.Less\SquishIt.Less.csproj", "{A8E60378-569B-4BAA-B344-75A065796E20}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquishIt.Preprocessors", "SquishIt.Preprocessors\SquishIt.Preprocessors.csproj", "{A8E60378-569B-4BAA-B344-75A065796E20}"
@rdumont
rdumont added a line comment Nov 17, 2011

Don't you think we should keep separate projects for each preprocessor? I know it would be too little code in each, but I worry about the dependencies and NuGet. It would be much easier to package each preprocessor if we kept them in separate projects. That way we could call them SquishIt.Less, SquishIt.CoffeeScript, SquishIt.Sass etc.

@AlexCuse
Owner
AlexCuse added a line comment Nov 17, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
View
4 SquishIt.Framework/Base/BundleBase.cs
@@ -125,6 +125,10 @@ private Input GetEmbeddedResourcePath(string resourcePath)
return new Input(resourcePath, ResolverFactory.Get<EmbeddedResourceResolver>());
}
+ protected static IPreprocessor FindPreprocessor(string file) {
+ return Bundle.Preprocessors.FirstOrDefault(p => p.ValidFor(file));
+ }
+
private string ExpandAppRelativePath(string file)
{
if (file.StartsWith("~/"))
View
12 SquishIt.Framework/Bundle.cs
@@ -7,17 +7,11 @@ namespace SquishIt.Framework
{
public class Bundle
{
- internal static readonly List<IPreprocessor> JsPreprocessors = new List<IPreprocessor>();
- internal static readonly List<IPreprocessor> CssPreprocessors = new List<IPreprocessor>();
+ internal static readonly List<IPreprocessor> Preprocessors = new List<IPreprocessor>();
- public static void RegisterCssPreprocessor<T>() where T : IPreprocessor
+ public static void RegisterPreprocessor<T>() where T : IPreprocessor
{
- CssPreprocessors.Add(Activator.CreateInstance<T>());
- }
-
- public static void RegisterJsPreprocessor<T>() where T : IPreprocessor
- {
- JsPreprocessors.Add(Activator.CreateInstance<T>());
+ Preprocessors.Add(Activator.CreateInstance<T>());
}
public static JavaScriptBundle JavaScript()
View
9 SquishIt.Framework/Css/CssBundle.cs
@@ -117,7 +117,7 @@ private string PreProcessCssFile(string file, IPreprocessor preProcessor)
string ProcessCssFile(string file, string outputFile) {
string css = null;
- var preProcessor = FindPreProcessor(file);
+ var preProcessor = FindPreprocessor(file);
if(preProcessor != null)
{
@@ -144,11 +144,6 @@ private string PreProcessCssFile(string file, IPreprocessor preProcessor)
return CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher);
}
- private static IPreprocessor FindPreProcessor(string file)
- {
- return Bundle.CssPreprocessors.FirstOrDefault(p => p.ValidFor(file));
- }
-
internal override Dictionary<string, GroupBundle> BeforeRenderDebug()
{
var modifiedGroupBundles = new Dictionary<string, GroupBundle>(GroupBundles);
@@ -161,7 +156,7 @@ private static IPreprocessor FindPreProcessor(string file)
foreach (var asset in groupBundle.Assets)
{
var localPath = asset.LocalPath;
- var preProcessor = FindPreProcessor(localPath);
+ var preProcessor = FindPreprocessor(localPath);
if(preProcessor != null)
{
string outputFile = FileSystem.ResolveAppRelativePathToFileSystem(localPath);
View
33 SquishIt.Framework/JavaScript/JavaScriptBundle.cs
@@ -69,10 +69,11 @@ protected override string CachePrefix
foreach (var asset in groupBundle.Assets)
{
var localPath = asset.LocalPath;
- if (localPath.ToLower().EndsWith(".coffee"))
+ var preprocessor = FindPreprocessor(localPath);
+ if (preprocessor != null)
{
string outputFile = FileSystem.ResolveAppRelativePathToFileSystem(localPath);
- string javascript = ProcessCoffee(outputFile);
+ string javascript = PreprocessJavascriptFile(outputFile, preprocessor);
outputFile += ".debug.js";
using (var fileWriter = fileWriterFactory.GetFileWriter(outputFile))
{
@@ -91,26 +92,32 @@ protected override string BeforeMinify(string outputFile, List<string> files, IE
{
var sb = new StringBuilder();
- files.Select(file => file.EndsWith(".coffee") ? ProcessCoffee(file) : ReadFile(file))
+ files.Select(ProcessJavascriptFile)
.Concat(arbitraryContent)
.Aggregate(sb, (builder, val) => builder.Append(val + "\n"));
return sb.ToString();
}
- private string ProcessCoffee(string file)
- {
- lock (typeof(JavaScriptBundle))
- {
- try
- {
+ private string ProcessJavascriptFile(string file) {
+ var preprocessor = FindPreprocessor(file);
+ if (preprocessor != null) {
+ return PreprocessJavascriptFile(file, preprocessor);
+ }
+ return ReadFile(file);
+ }
+
+ private string PreprocessJavascriptFile(string file, IPreprocessor preprocessor) {
+ lock (typeof(JavaScriptBundle)) {
+ try {
currentDirectoryWrapper.SetCurrentDirectory(Path.GetDirectoryName(file));
var content = ReadFile(file);
- var compiler = new Coffee.CoffeescriptCompiler();
- return compiler.Compile(content);
+ if (preprocessor == null) {
+ return content;
+ }
+ return preprocessor.Process(file, content);
}
- finally
- {
+ finally {
currentDirectoryWrapper.Revert();
}
}
View
2 SquishIt.Framework/SquishIt.Framework.csproj
@@ -83,7 +83,6 @@
<Compile Include="Base\BundleBase.cs" />
<Compile Include="Base\GroupBundle.cs" />
<Compile Include="Base\IBundle.cs" />
- <Compile Include="Coffee\CoffeescriptCompiler.cs" />
<Compile Include="IBundleCache.cs" />
<Compile Include="IPreprocessor.cs" />
<Compile Include="Minifiers\IMinifier.cs" />
@@ -143,7 +142,6 @@
<Content Include="closure-compiler\compiler.jar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <EmbeddedResource Include="Coffee\coffee-script.js" />
</ItemGroup>
<ItemGroup>
<None Include="closure-compiler\COPYING">
View
141 ....Framework/Coffee/CoffeescriptCompiler.cs → ...processors/Coffee/CoffeescriptCompiler.cs
@@ -1,70 +1,71 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using Jurassic;
-
-namespace SquishIt.Framework.Coffee
-{
- public class CoffeescriptCompiler
- {
- private static string _coffeescript;
- private static ScriptEngine _engine;
-
- public string Compile(string input)
- {
- if(FileSystem.Unix)
- {
- throw new NotSupportedException ("Coffeescript not yet supported for mono.");
- }
-
- CoffeeScriptEngine.SetGlobalValue("Source", input);
-
- // Errors go from here straight on to the rendered page;
- // we don't want to hide them because they provide valuable feedback
- // on the location of the error
- string result = CoffeeScriptEngine.Evaluate<string>("CoffeeScript.compile(Source, {bare: false})");
-
- return result;
- }
-
- private static ScriptEngine CoffeeScriptEngine
- {
- get
- {
- if(_engine == null)
- {
- var engine = new ScriptEngine();
- engine.ForceStrictMode = true;
- engine.Execute(Compiler);
- _engine = engine;
- }
- return _engine;
- }
- }
-
- static string Compiler
- {
- get
- {
- if (_coffeescript == null)
- _coffeescript = LoadCoffeescript();
-
- return _coffeescript;
- }
- }
-
- private static string LoadCoffeescript()
- {
- using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SquishIt.Framework.Coffee.coffee-script.js"))
- {
- using(var reader = new StreamReader(stream))
- {
- return reader.ReadToEnd();
- }
- }
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using Jurassic;
+using SquishIt.Framework;
+
+namespace SquishIt.Preprocessors.Coffee
+{
+ public class CoffeescriptCompiler
+ {
+ private static string _coffeescript;
+ private static ScriptEngine _engine;
+
+ public string Compile(string input)
+ {
+ if(FileSystem.Unix)
+ {
+ throw new NotSupportedException ("Coffeescript not yet supported for mono.");
+ }
+
+ CoffeeScriptEngine.SetGlobalValue("Source", input);
+
+ // Errors go from here straight on to the rendered page;
+ // we don't want to hide them because they provide valuable feedback
+ // on the location of the error
+ string result = CoffeeScriptEngine.Evaluate<string>("CoffeeScript.compile(Source, {bare: false})");
+
+ return result;
+ }
+
+ private static ScriptEngine CoffeeScriptEngine
+ {
+ get
+ {
+ if(_engine == null)
+ {
+ var engine = new ScriptEngine();
+ engine.ForceStrictMode = true;
+ engine.Execute(Compiler);
+ _engine = engine;
+ }
+ return _engine;
+ }
+ }
+
+ static string Compiler
+ {
+ get
+ {
+ if (_coffeescript == null)
+ _coffeescript = LoadCoffeescript();
+
+ return _coffeescript;
+ }
+ }
+
+ private static string LoadCoffeescript()
+ {
+ using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SquishIt.Preprocessors.Coffee.coffee-script.js"))
+ {
+ using(var reader = new StreamReader(stream))
+ {
+ return reader.ReadToEnd();
+ }
+ }
+ }
+ }
+}
View
0 SquishIt.Framework/Coffee/coffee-script.js → ...hIt.Preprocessors/Coffee/coffee-script.js
File renamed without changes.
View
21 SquishIt.Preprocessors/CoffeeScriptPreprocessor.cs
@@ -0,0 +1,21 @@
+using System.Text.RegularExpressions;
+using SquishIt.Framework;
+
+namespace SquishIt.Preprocessors
+{
+ public class CoffeeScriptPreprocessor : IPreprocessor
+ {
+ static Regex lessFiles = new Regex(@"(\.coffee)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
+ public bool ValidFor(string filePath)
+ {
+ return lessFiles.IsMatch(filePath);
+ }
+
+ public string Process(string filePath, string content)
+ {
+ var compiler = new Coffee.CoffeescriptCompiler();
+ return compiler.Compile(content);
+ }
+ }
+}
View
3 SquishIt.Less/LessPreprocessor.cs → SquishIt.Preprocessors/LessPreprocessor.cs
@@ -2,13 +2,12 @@
using SquishIt.Framework;
using dotless.Core;
-namespace SquishIt.Less
+namespace SquishIt.Preprocessors
{
public class LessPreprocessor : IPreprocessor
{
static Regex lessFiles = new Regex(@"(\.less)|(\.less.css)$", RegexOptions.Compiled);
-
public bool ValidFor(string filePath)
{
return lessFiles.IsMatch(filePath);
View
0 SquishIt.Less/Properties/AssemblyInfo.cs → ....Preprocessors/Properties/AssemblyInfo.cs
File renamed without changes.
View
13 SquishIt.Less/SquishIt.Less.csproj → ...eprocessors/SquishIt.Preprocessors.csproj
@@ -8,8 +8,8 @@
<ProjectGuid>{A8E60378-569B-4BAA-B344-75A065796E20}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>SquishIt.Less</RootNamespace>
- <AssemblyName>SquishIt.Less</AssemblyName>
+ <RootNamespace>SquishIt.Preprocessors</RootNamespace>
+ <AssemblyName>SquishIt.Preprocessors</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
@@ -35,15 +35,19 @@
<Reference Include="dotless.Core">
<HintPath>..\packages\dotless.1.2.1.0\lib\dotless.Core.dll</HintPath>
</Reference>
+ <Reference Include="Jurassic">
+ <HintPath>..\packages\Jurassic.2.1.1\lib\Jurassic.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="CoffeeScriptPreprocessor.cs" />
+ <Compile Include="Coffee\CoffeescriptCompiler.cs" />
<Compile Include="LessPreprocessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -56,6 +60,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Coffee\coffee-script.js" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
0 SquishIt.Less/packages.config → SquishIt.Preprocessors/packages.config
File renamed without changes.
View
2 SquishIt.Tests/Coffee/CoffeescriptCompilerTests.cs
@@ -1,6 +1,6 @@
using System;
using NUnit.Framework;
-using SquishIt.Framework.Coffee;
+using SquishIt.Preprocessors.Coffee;
namespace SquishIt.Tests.Coffee
{
View
4 SquishIt.Tests/SquishIt.Tests.csproj
@@ -95,6 +95,10 @@
<Project>{50C3BC24-E534-4B78-8E8E-CB2C4053321D}</Project>
<Name>SquishIt.Framework</Name>
</ProjectReference>
+ <ProjectReference Include="..\SquishIt.Preprocessors\SquishIt.Preprocessors.csproj">
+ <Project>{A8E60378-569B-4BAA-B344-75A065796E20}</Project>
+ <Name>SquishIt.Preprocessors</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
View
2 SquishItAspNetTest/Global.asax.cs
@@ -11,7 +11,7 @@ public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
- SquishIt.Framework.Bundle.RegisterCssPreprocessor<SquishIt.Less.LessPreprocessor>();
+ SquishIt.Framework.Bundle.RegisterPreprocessor<SquishIt.Preprocessors.LessPreprocessor>();
}
}
}
View
4 SquishItAspNetTest/SquishItAspNetTest.csproj
@@ -121,9 +121,9 @@
<Project>{50C3BC24-E534-4B78-8E8E-CB2C4053321D}</Project>
<Name>SquishIt.Framework</Name>
</ProjectReference>
- <ProjectReference Include="..\SquishIt.Less\SquishIt.Less.csproj">
+ <ProjectReference Include="..\SquishIt.Preprocessors\SquishIt.Preprocessors.csproj">
<Project>{A8E60378-569B-4BAA-B344-75A065796E20}</Project>
- <Name>SquishIt.Less</Name>
+ <Name>SquishIt.Preprocessors</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>

0 comments on commit 9bb650f

Please sign in to comment.