Feb 23 2007
Note: This is a direct translation from C (procedural) to C#. I didn’t refactor the object too much, so the design is far from perfect, but it works well.
Note 2: I just found out that Douglas translated JsMin from C to C# too, his version is available here, so pick the one you prefer ;)
JsMin has only a default constructor, but it also has a few public properties used to set options:
- string BakFileExtension: The extension used for the backup file. Note: The default file will be kept only if the method Go( string, bool ) is used, and the second parameter set to true. Default: “bak”.
- string FirstLines: An array of strings which will be added on top of the minimized file. Each entry in the array is one line in the target file. This can be used to specify copyright information, author name, etc…
- int KeepFirstLines: A number specifying how many lines in the original file will be copied as is in the target file. For example, this is useful to preserve copyright information, author name… present in the original file.
- Go( string filePath ): Minimizes a script file and saves it under the same name as the original file. No backup is kept.
- Go( string filePath, bool keepBakFile ): Minimizes a script file and saves it under the same name as the original file. Depending on the value of the parameter keepBakFile, a backup file will be kept or not.
- Go( StreamReader reader, StreamWriter writer ): Minimizes the content of the “reader” stream and saves it to the “writer” stream. This method is useful when no file is available. For example, in ASP.NET, this method can be used to minimize script files contained in resources dynamically. Another use is described here under: The user of this webpage selects a local script file and sends it to the web server. The file content is minimized, and returned to the user as an attachment, so that the user can choose to open or save the file.
Code example: ASP.NET
Receiving, minimizing and sending a script file in ASP.NET