4

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.

KevinUK
  • 5,053
  • 5
  • 33
  • 49

3 Answers3

3

Within MSBild solution you can use at the least three approaches.

  1. Execute copy command (simply to understand and support, may affects build performance in case of much calls perfomed);
  2. Use ReadLinesFromFile/WriteLinesToFile msbuild tasks (simply enough, but resulting file will lose formatting)
  3. 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.

Vitaly Filatenko
  • 415
  • 3
  • 12
2

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?

KevinUK
  • 5,053
  • 5
  • 33
  • 49
1
C:\Progra~1\Microsoft\Micros~1\AjaxMin.exe js1.js js2.js -o jscombine.js -clobber
GDP
  • 8,109
  • 6
  • 45
  • 82
SamZ
  • 11
  • 1