Merging multiple jscript files into one with msbuild task?
I am using Microsoft Ajax Minifier to convert file1.js to file1.min.js. Now I would like to take file1.min.js and file2.min.js and merge them into files-merged.min.js.
I have found several open 开发者_开发问答source msbuild projects but with no documentation on how to use them!
I am using Visual Studio 2010, is this something Ajax Minifier can do, if not do you have any tips on how to do it?
I want this to be an automated process, something done each time I build the solution.
Within MSBild solution you can use at the least three approaches.
- Execute copy command (simply to understand and support, may affects build performance in case of much calls perfomed);
- Use ReadLinesFromFile/WriteLinesToFile msbuild tasks (simply enough, but resulting file will lose formatting)
- Use .NET File.ReadAllText/WriteLinesToFile (good result, but may be complicated in some cases)
Executing copy command
To merge with copy command you should add something like this into your target:
<Exec Command="COPY /b file1.min.js + file2.min.js files-merged.min.js" />
In most cases it should be enough.
Using ReadLinesFromFile/WriteLinesToFile msbuild tasks
<ItemGroup>
<SourceFiles Include="file1.min.js;file2.min.js" />
</ItemGroup>
<ReadLinesFromFile File="%(SourceFiles.Identity)">
<Output TaskParameter="Lines" ItemName="FileLines" />
</ReadLinesFromFile>
<WriteLinesToFile File="files-merged.min.js" Lines="@(FileLines)" Overwrite="true" />
This example illustrates how to use native msbuild facilities to merge both files. Unfortunately, in that case we're loosing file formatting.
Using .NET File.ReadAllText() and msbuild WriteLinesToFile
<ItemGroup>
<SourceFiles Include="arrays.cmd;cp.cmd" />
<FileLines Include="$([System.IO.File]::ReadAllText(%(SourceFiles.Identity)))" />
</ItemGroup>
<WriteLinesToFile File="test.out" Lines="@(FileLines)" Overwrite="true" />
This approach fast and accurate, but uses some kind of .NET injection.
I did it by following this article:
http://encosia.com/2009/05/20/automatically-minify-and-combine-javascript-in-visual-studio/
I would prefer not to use the JSMin tool as I already have AJAX Minifier so I use JSMin just to merge the files. Can Minifier handle the merging of files?
C:\Progra~1\Microsoft\Micros~1\AjaxMin.exe js1.js js2.js -o jscombine.js -clobber
精彩评论