Yui.Compressor

 

在开发中编写的js、css发布的时候,往往需要进行压缩,以减少文件大小,减轻服务器的负担。这就得每次发版本的时候,对js、js进行压缩,然后再发布。有没有什么办法,让代码到了服务器上边,它自己进行压缩呢?

有两种办法:

第一种,在css、js请求到来的时候读取一下相对应的文件,进行压缩后返回。此方法可以通过在Global.asax的Application_BeginRequest的事件中,进行处理,也可以在web.config中注册一个httpHandler进行处理。

第二种是在程序启动的时候,对全部css以及js进行压缩,压缩之后,每次访问都使用压缩后的文件即可。这种办法可以将js全部压缩到一个文件夹里边,不过需要注意一下文件的顺序。

压缩使用的是雅虎的压缩组件: Yahoo.Yui.Compressor.dll

由于第一种办法没能实现js压缩到一个文件,所以这里采用的是第二种方案。

压缩方法比较简单,首先引用Yahoo.Yui.Compressor.dll和EcmaScript.NET.modified.dll
压缩js

复制代码

//文件内容
string strContent = File.ReadAllText(jsPath, Encoding.UTF8);
//初始化
var js = new JavaScriptCompressor(strContent, false, Encoding.UTF8, System.Globalization.CultureInfo.CurrentCulture);
//压缩该js
strContent = js.Compress();
File.WriteAllText(jsPath, strContent, Encoding.UTF8);

复制代码

压缩css

复制代码

string strContent = File.ReadAllText(cssPath, Encoding.UTF8)
//进行压缩
strContent = CssCompressor.Compress
(strContent);
File.WriteAllText(cssPath, strContent, Encoding.UTF8);

复制代码

还有另外一一种办法不用自己写代码,每次网站发布的时候自动压缩指定的js和css文件:
1)在项目中新建“MSBuild” 文件夹,把yahoo压缩组件的两个dll放进去
2)在“文件夹”内新建”MSBuildCompressor.xml”文件:

复制代码

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Project xmlns=”http://schemas.microsoft.com/developer/MsBuild/2003″>
<UsingTask
TaskName=”CompressorTask”
  AssemblyFile=”Yahoo.Yui.Compressor.dll” />
<!– The .NET 2.0 version of the task .. and yes .. that’s Model.Net20 folder listed twice .. i know i know…
<UsingTask
TaskName=”CompressorTask”
        AssemblyFile=”..\..\Projects\Yahoo.Yui.Compressor\Model.Net20\Model.Net20\bin\Debug\Yahoo.Yui.Compressor.NET20.dll” />
–>
<!– Define the output locations. These can be set via the msbuild command line using
         /p:CssOutputFile=$(TargetDir)../whatever…
         /p:JavaScriptOutputFile=$(TargetDir)../whatever…
         If they are not supplied or are empty, then we the value whatever is supplied, below.
–>
<PropertyGroup>
<CssOutputFile Condition=” ‘$(CssOutputFile)’==” “>
      SylesSheetFinal.css
</CssOutputFile>
<JavaScriptOutputFile Condition=” ‘$(JavaScriptOutputFile)’==” “>
      JavaScriptFinal.css
</JavaScriptOutputFile>
</PropertyGroup>
<Target Name=”MyTaskTarget”>
<!–
ItemGroup\CssFiles or ItemGroup\JavaScriptFiles: add zero to many files you wish to include in this compression task.
                                                             Don’t forget, you can use the wildcard (eg. *.css, *.js) if you feel up to it.
                                                             Finally, at least one item is required – either a css file or a js file.
CssFiles/JavaScriptFiles data format: Please do not touch this.
DeleteCssFiles: [Optional] True | Yes | Yeah | Yep | True | FoSho | FoSho. Default is False. Anything else is False. (eg. blah = false, xxxx111 = false, etc)
CssCompressionType: YuiStockCompression | MichaelAshsRegexEnhancements | HaveMyCakeAndEatIt or BestOfBothWorlds or Hybrid; Default is YuiStockCompression.
ObfuscateJavaScript: [Optional] refer to DeleteCssFiles, above.
PreserveAllSemicolons: [Optional] refer to DeleteCssFiles, above.
DisableOptimizations: [Optional] refer to DeleteCssFiles, above.
EncodingType: [Optional] ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8, Default. Default is ‘Default’.
DeleteJavaScriptFiles: [Optional] refer to DeleteCssFiles, above.
LineBreakPosition: [Optional] the position where a line feed is appened when the next semicolon is reached. Default is -1 (never add a line break).
                               0 (zero) means add a line break after every semicolon. (This might help with debugging troublesome files).        
LoggingType: None | ALittleBit | HardcoreBringItOn;  Hardcore also lists javascript verbose warnings, if there are any (and there usually is 😛 ).
ThreadCulture: [Optional] the culture you want the thread to run under. Default is ‘en-gb’.
IsEvalIgnored: [Optional] compress any functions that contain ‘eval’. Default is False, which means a function that contains
                           ‘eval’ will NOT be compressed. It’s deemed risky to compress a function containing ‘eval’. That said,
if the usages are deemed safe this check can be disabled by setting this value to True.
–>
<ItemGroup>
<!– Single files, listed in order of dependency
      <CssFiles Include=”../StylesheetSample1.css”/>
      <CssFiles Include=”../StylesheetSample2.css”/>
      <CssFiles Include=”../StylesheetSample3.css”/>
      <CssFiles Include=”../StylesheetSample4.css”/>–>
<JavaScriptFiles Include=”../myjs/sample_1.js”/>
<!– All the files. They will be handled (I assume) in alphabetically. –>
<!–<CssFiles Include=”*.css” />
<JavaScriptFiles Include=”*.js” />
–>
</ItemGroup>
<CompressorTask
CssFiles=”@(CssFiles)”
    DeleteCssFiles=”false”
    CssOutputFile=”$(CssOutputFile)”
    CssCompressionType=”YuiStockCompression”
    JavaScriptFiles=”@(JavaScriptFiles)”
    ObfuscateJavaScript=”False”
    PreserveAllSemicolons=”False”
    DisableOptimizations=”Nope”
    EncodingType=”utf-8″
    DeleteJavaScriptFiles=”false”
    LineBreakPosition=”-1″
    JavaScriptOutputFile=”$(JavaScriptOutputFile)”
    LoggingType=”ALittleBit”
    ThreadCulture=”en-au”
    IsEvalIgnored=”false”
/>
<!–
    CssFiles=”@(CssFiles)”
    DeleteCssFiles=”false”
    CssOutputFile=”$(CssOutputFile)”
    CssCompressionType=”YuiStockCompression”
    JavaScriptFiles=”@(JavaScriptFiles)”
    ObfuscateJavaScript=”False”                   //是否模糊处理js文件,True:会将js中的变量为“a,b,c”单个字符的变量
    PreserveAllSemicolons=”False”
    DisableOptimizations=”Nope”
    EncodingType=”utf-8″                          //如果js或者css包含有中文,则必须使用utf-8编码,否则会乱码
    DeleteJavaScriptFiles=”false”
    LineBreakPosition=”-1″
    JavaScriptOutputFile=”$(JavaScriptOutputFile)”
    LoggingType=”ALittleBit”
    ThreadCulture=”en-au”
    IsEvalIgnored=”false”
–>
</Target>
</Project>

复制代码

3)选择网站属性,切换到“生成事件”标签,,在“”输入以下命令:
$(MSBuildBinPath)\msbuild.exe “$(ProjectDir)MSBuild\MSBuildCompressor.xml” /p:CssOutputFile=”../miniFile/Sieena.min.css” /p:JavaScriptOutputFile=”../miniFile/Sieena.min.js”

这样,每次编译网站的时候就会自动压缩指定的js和css文件了

实例下载

官方地址:

http://yuicompressor.codeplex.com/wikipage?title=Sample%20MSBuild.xml%20File&ProjectName=yuicompressor

 

转自:http://www.cnblogs.com/timy/archive/2011/10/30/2229640.html

NuGet系列-製作安裝Package時自動設定Config

在NuGet還沒有出來前要在專案中加入組件的相關Config是挺麻煩的,要看著文件或範例來一段一段複制貼上Config,說真的不是很方便,但是NuGet有提供Config轉換,會把需要設定的Config合併到專案的Config中,可以省下不少設定時間,這篇就來跟大家說在建立Package時如何加入Config轉換檔。

建立Config轉換檔

準備一個僅安裝時需要的Config檔,如下範列是我在製作Common.Logging.Elmah時的設定。

01
<configuration>

02
<configSections>  

03
<sectionGroup name="common">

04
<section name="logging"t ype="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>

05
</sectionGroup>

06
</configSections>  

07
<common>

08
<logging>

09
<factoryAdapter type="Common.Logging.Elmah.ElmahLoggerFactoryAdapter, Common.Logging.Elmah">

10
<arg key="Level" value="all"/>

11
</factoryAdapter>

12
</logging>

13
</common>

14
</configuration>

NuGet的Config檔的轉化檔在設計上很簡單,就跟平常在設定Config一樣一樣,相對的它沒有辦法做太多事,只能以XML的DOM,一個一個比對element的階層與內容,沒有完全相同的element就新增,如果element相同,但有一個attribute相同會合併設定,完全相同就略過,以下面的例子為例:

App.Config內容

1
<configuration>

2
<appSettings>

3
<add key="SetA" />

4
<add key="SetB" value="App.Coinfg" />

5
<add key="SetC" value="Same" />  

6
</appSettings>

7
</configuration>

App.config.transform(transform為轉換檔的副檔名)內容

1
<configuration>

2
<appSettings>

3
<add key="SetA" vaule="Transformation" />

4
<add key="SetB" value="Transformation" />

5
<add key="SetC" value="Same" />

6
<add key="SetD" value="Transformation" />

7
</appSettings>

8
</configuration>

合併後的結果

1
<configuration>                               //完全一樣,不處理

2
<appsettings>                               //完全一樣,不處理

3
<add key="SetA" value="Transformation" /> //一個Attribute相同,合併

4
<add key="SetB" value="App.Coinfg"/>     //雖然有一個Attribute相同,但另一個Attribute不同,不處理

5
<add key="SetB" value="Transformation" /> //雖然有一個Attribute相同,但另一個Attribute不同,新增

6
<add key="SetC" value="Same" />           //完全一樣,不處理

7
<add key="SetD" value="Transformation" /> //沒有,新增

8
</appsettings>                            

9
</configuration>

如果要寫某種情境下才新增設定,那就不能用Config轉化檔,可能要在NuGet的PowerShell安裝檔中寫Code來處理。

加入Package中

轉換檔的副檔名是.transform,只要將準備好的config的檔名後方加上.transform即可,如果是要轉換web.config,就建立web.config.transform,如果是要轉換app.config,就建立app.config.transform,並加入Package中的Content下,加入的方法有:

如果組件可以用在Web或Windows Form,可以同時準備web.config.transform、app.config.transform,這二個檔案,當安裝Package時,會比對目錄下(放在Content下相對於專案根目錄)有沒有扣除.transform檔名的檔案,如果有才做轉換,如果沒有不做轉換也不會在專案中增加檔案。

image

安裝與移除Package

有Config專案的Package在安裝上沒有不一樣,使用平常的安裝方法及可,但在移除上可能要注意,因為NuGet Config轉換除了安裝時會合供,移除時會刪除合併,不過只有跟轉換檔相同的element才會刪除,因為安裝後通常會改設定值,應該有些比對不成功,會刪不乾清。

參考資料

Configuration File and Source Code Transformations

转自: http://www.dotblogs.com.tw/wadehuang36/archive/2011/10/16/nuget-config-transform.aspx

The user identity cannot change during an active SignalR connection

The user identity cannot change during an active SignalR connection

This error may be seen if authentication is being used, and the client is logged out before the connection is stopped. The solution is to stop the SignalR connection before logging the client out.

无法识别的用户标识。在 SignalR 连接处于活动状态期间无法更改用户标识

官方有详细的文档:

http://www.asp.net/signalr/overview/signalr-20/troubleshooting-and-debugging/troubleshooting