Repository navigation
Expand file tree
/
Copy pathJavascriptMinifier.cs
More file actions
182 lines (150 loc) · 6.1 KB
/
Copy pathJavascriptMinifier.cs
File metadata and controls
182 lines (150 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ajax.Utilities;
namespace BlogEngine.Core
{
/// <summary>
/// Helper class for performing minification of Javascript and CSS.
/// </summary>
/// <remarks>
///
/// This class is basically a wrapper for the AjaxMin library(lib/AjaxMin.dll).
/// http://ajaxmin.codeplex.com/
///
/// There are no symbols that come with the AjaxMin dll, so this class gives a bit of intellisense
/// help for basic control. AjaxMin is a pretty dense library with lots of different settings, so
/// everyone's encouraged to use it directly if they want to.
///
/// </remarks>
public sealed class JavascriptMinifier
{
private Microsoft.Ajax.Utilities.Minifier ajaxMinifier = new Microsoft.Ajax.Utilities.Minifier();
/// <summary>
/// Creates a new Minifier instance.
/// </summary>
public JavascriptMinifier()
{
this.RemoveWhitespace = true;
this.PreserveFunctionNames = true;
this.VariableMinification = Core.VariableMinification.None;
}
#region "Methods"
/// <summary>
/// Builds the required CodeSettings class needed for the Ajax Minifier.
/// </summary>
/// <returns></returns>
private CodeSettings CreateCodeSettings()
{
var codeSettings = new CodeSettings();
codeSettings.MinifyCode = false;
codeSettings.OutputMode = (this.RemoveWhitespace ? OutputMode.SingleLine : OutputMode.MultipleLines);
// MinifyCode needs to be set to true in order for anything besides whitespace removal
// to be done on a script.
codeSettings.MinifyCode = this.ShouldMinifyCode;
if (this.ShouldMinifyCode)
{
switch (this.VariableMinification)
{
case Core.VariableMinification.None:
codeSettings.LocalRenaming = LocalRenaming.KeepAll;
break;
case Core.VariableMinification.LocalVariablesOnly:
codeSettings.LocalRenaming = LocalRenaming.KeepLocalizationVars;
break;
case Core.VariableMinification.LocalVariablesAndFunctionArguments:
codeSettings.LocalRenaming = LocalRenaming.CrunchAll;
break;
}
// This is being set by default. A lot of scripts use eval to parse out various functions
// and objects. These names need to be kept consistant with the actual arguments.
codeSettings.EvalTreatment = EvalTreatment.MakeAllSafe;
// This makes sure that function names on objects are kept exactly as they are. This is
// so functions that other non-minified scripts rely on do not get renamed.
codeSettings.PreserveFunctionNames = this.PreserveFunctionNames;
}
return codeSettings;
}
/// <summary>
/// Gets the minified version of the passed in script.
/// </summary>
/// <param name="script"></param>
/// <returns></returns>
public string Minify(string script)
{
if (this.ShouldMinify)
{
if (String.IsNullOrEmpty(script))
{
return string.Empty;
}
else
{
return this.ajaxMinifier.MinifyJavaScript(script, this.CreateCodeSettings());
}
}
return script;
}
#endregion
#region "Properties"
/// <summary>
/// Gets or sets whether this Minifier instance should minify local-scoped variables.
/// </summary>
/// <remarks>
///
/// Setting this value to LocalVariablesAndFunctionArguments can have a negative impact on some scripts.
/// Ex: A pre-minified jQuery will fail if passed through this.
///
/// </remarks>
public VariableMinification VariableMinification { get; set; }
/// <summary>
/// Gets or sets whether this Minifier instance should preserve function names when minifying a script.
/// </summary>
/// <remarks>
///
/// Scripts that have external scripts relying on their functions should leave this set to true.
///
/// </remarks>
public bool PreserveFunctionNames { get; set; }
/// <summary>
/// Gets or sets whether the <see cref="BlogEngine.Core.JavascriptMinifier"/> instance should remove
/// whitespace from a script.
/// </summary>
public bool RemoveWhitespace { get; set; }
private bool ShouldMinifyCode
{
get
{
// return true;
return ((!PreserveFunctionNames) || (this.VariableMinification != Core.VariableMinification.None));
}
}
private bool ShouldMinify
{
get
{
return ((this.RemoveWhitespace) || (this.ShouldMinifyCode));
}
}
#endregion
}
/// <summary>
/// Represents the way variables should be minified by a Minifier instance.
/// </summary>
public enum VariableMinification
{
/// <summary>
/// No minification will take place.
/// </summary>
None = 0,
/// <summary>
/// Only variables that are local in scope to a function will be minified.
/// </summary>
LocalVariablesOnly = 1,
/// <summary>
/// Local scope variables will be minified, as will function parameter names. This can have a negative impact on some scripts, so test if you use it!
/// </summary>
LocalVariablesAndFunctionArguments = 2
}
}