public RoslynMinifier(MinifierOptions options = null, string[] ignoredIdentifiers = null, string[] ignoredComments = null) { Options = options ?? new MinifierOptions(); IgnoredIdentifiers = ignoredIdentifiers?.ToList() ?? new List <string>(); IgnoredComments = ignoredComments?.ToList() ?? new List <string>(); _workspace = new AdhocWorkspace(); _mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); }
public static string RemoveSpacesInSource(string csharpCode) { var minifierOptions = new MinifierOptions(false); minifierOptions.SpacesRemoving = true; minifierOptions.NamespacesRemoving = true; var minifier = new Minifier(minifierOptions); return minifier.MinifyFromString(csharpCode); }
public Minifier(MinifierOptions options = null, string[] ignoredIdentifiers = null, string[] ignoredComments = null) { Options = options ?? new MinifierOptions(); _projectContent = new CSharpProjectContent(); var assemblies = new List <Assembly> { typeof(object).Assembly, // mscorlib typeof(Uri).Assembly, // System.dll typeof(Enumerable).Assembly, // System.Core.dll }; var unresolvedAssemblies = new IUnresolvedAssembly[assemblies.Count]; Parallel.For( 0, assemblies.Count, delegate(int i) { var loader = new CecilLoader(); var path = assemblies[i].Location; unresolvedAssemblies[i] = loader.LoadAssemblyFile(assemblies[i].Location); }); _projectContent = _projectContent.AddAssemblyReferences((IEnumerable <IUnresolvedAssembly>)unresolvedAssemblies); IgnoredIdentifiers = ignoredIdentifiers == null ? new List <string>() : ignoredIdentifiers.ToList(); IgnoredComments = new List <string>(); if (ignoredComments != null) { foreach (string comment in ignoredComments) { var str = comment; if (str.StartsWith("//")) { str = str.Substring("//".Length); } else if (str.StartsWith("/*") && str.EndsWith("*/")) { str = str.Substring("/*".Length, str.Length - "/*".Length - "*/".Length); } if (!IgnoredComments.Contains(str)) { IgnoredComments.Add(str); } } } }
public Minifier(MinifierOptions options = null, string[] ignoredIdentifiers = null, string[] ignoredComments = null) { Options = options ?? new MinifierOptions(); _projectContent = new CSharpProjectContent(); var assemblies = new List<Assembly> { typeof(object).Assembly, // mscorlib typeof(Uri).Assembly, // System.dll typeof(Enumerable).Assembly, // System.Core.dll }; var unresolvedAssemblies = new IUnresolvedAssembly[assemblies.Count]; Parallel.For( 0, assemblies.Count, delegate (int i) { var loader = new CecilLoader(); var path = assemblies[i].Location; unresolvedAssemblies[i] = loader.LoadAssemblyFile(assemblies[i].Location); }); _projectContent = _projectContent.AddAssemblyReferences((IEnumerable<IUnresolvedAssembly>)unresolvedAssemblies); IgnoredIdentifiers = ignoredIdentifiers == null ? new List<string>() : ignoredIdentifiers.ToList(); IgnoredComments = new List<string>(); if (ignoredComments != null) foreach (var comment in ignoredComments) { var str = comment; if (str.StartsWith("//")) str = str.Substring("//".Length); else if (str.StartsWith("/*") && str.EndsWith("*/")) str = str.Substring("/*".Length, str.Length - "/*".Length - "*/".Length); if (!IgnoredComments.Contains(str)) IgnoredComments.Add(str); } }