Roslyn version for .NET Standard 🚧#46
Conversation
# Conflicts: # CSharpMinifier/IdentifierGenerator.cs # CSharpMinifier/Rewriters/TokensMinifier.cs
|
Sorry, I'm too busy now. I hope to look at it at the beginning of April. @codeFather2 could you please make a review and point something about tests you wrote to me? |
|
Meanwhile, I published my own minifier, which isn't as ambitious. It doesn't have any external dependencies (not even Roslyn) and filters comments and unnecessary whitespace (so no minification of identifiers) with the main use case being to generate stable hashes/digests (avoid wasteful work if there's no useful change to content). |
|
It's quite interesting, but it sounds like your minifier is not able to shorten identifiers? |
|
I made my own completely from scratch also based on Roselyn. It can shortnen identifers, although it dos'nt take into account all the different scopes. Så the good identifer replacement like "a" will only be used as a replacement for one original identifer. But its okay because the size of programs i'm using it for will only generate identifiers up to 3 long. |
|
Is your project available on GitHub? |
Yes and I already mentioned that:
It's strictly a minifier, not an obfuscator or an uglifier, which I see as two separate things. I wanted to minify without resorting to Roslyn because that requires dragging in a lot (not to mention a specific version that you have to keep updating) and isn't necessarily fast or cheap on memory. My goal was to cheaply & quickly generate more stable hashes/digests of sources such that changes in whitespace (tabs/spaces, line-endings, formatting, etc.), comments and regions do not change the hash. For example, most watchers will trigger a re-compilation on any physical change to a file. With a minified version you avoid doing work unless the code changes. Here's an example scenario cited in the README:
It could benefit from minification of private program details such that changing a local variable name that isn't publicly visible anywhere doesn't change the hash, but then that requires semantic analysis and adds complexity. The level of minification I chose seemed Good Enough™ and way better than any physical change. Since it's cheap and fast, there's no cost to using it on the desktop, server, cloud or in a library. Also, since it parses the absolute minimum C# grammar, leaving the rest as raw text, you can technically cheat and minify C# snippets like scripts and expressions provided that they are syntactically valid. For quick and ad-hoc syntactic validation using Roslyn, I have published CSharpSyntaxValidator as a separate tool. |
|
FWIW, I've also published my C# minifier for anyone to try out directly in the browser. It's a client-side Blazor application so no code is round-tripped over the wire (can also be cloned and tested locally via |
|
Interesting! Will take a look. |
This PR builds on top of PR #45 from @codeFather2 and updates the library to target .NET Standard 2.0 and tests to target .NET Core 2.1. While I realize that #45 is work-in-progress, I wanted to share my results and findings in case we'd like to get this merged.
Why do this?
Because there is no reason to limit the library to .NET Framework.
Summary of changes
The GUI was upgraded from .NET Framework 4.6 to 4.6.1 to neatly align with .NET Standard 2.0.
The biggest change needed was in the
CompileUtilsclass that relied onSystem.CodeDomfor compilation, but which is unsupported for now (see dotnet/corefx#12180). So what I did, instead, was to emulate some of the types to have the least impact on the rest of the code and instead spawncsc.exefor the real work. ItsCanCompileandCompilemethods were then changed to take an additional parameter that's the path to the C# compiler executable. I've also added a small batch to the root of the project calledinstall.cmdthat will download the compiler via the Microsoft.Net.Compilers package. If someone clones the repo, they have to runinstall.cmd. This can be improved to happen automagically if need be. However, I feel thatCompileUtilsshould be moved out of the library project and into common code shared by the GUI and the test project that really use it.