Permalink
Browse files

Add a new --globals option to pre-declare global variables; e.g., "YUI"

Signed-off-by: Reid Burke <me@reidburke.com>
  • Loading branch information...
1 parent 281808b commit d27f3adbfecdeb8097851406370bb17d62ab461d Robert Tsai committed with reid Jul 24, 2010
@@ -526,11 +526,17 @@ private static void optimizeObjLitMemberDecl(ArrayList tokens) {
private ScriptOrFnScope globalScope = new ScriptOrFnScope(-1, null);
private Hashtable indexedScopes = new Hashtable();
- public JavaScriptCompressor(Reader in, ErrorReporter reporter)
+ public JavaScriptCompressor(String[] globals, Reader in, ErrorReporter reporter)
throws IOException, EvaluatorException {
this.logger = reporter;
this.tokens = parse(in, reporter);
+ for (int ii = 0; ii < globals.length; ii++) {
+ String symbol = globals[ii];
+ if (symbol.length() <= 3) {
+ globalScope.declareIdentifier(symbol);
+ }
+ }
}
public void compress(Writer out, int linebreak, boolean munge, boolean verbose,
@@ -24,6 +24,7 @@ public static void main(String args[]) {
CmdLineParser.Option verboseOpt = parser.addBooleanOption('v', "verbose");
CmdLineParser.Option nomungeOpt = parser.addBooleanOption("nomunge");
CmdLineParser.Option linebreakOpt = parser.addStringOption("line-break");
+ CmdLineParser.Option globalsOpt = parser.addStringOption("globals");
CmdLineParser.Option preserveSemiOpt = parser.addBooleanOption("preserve-semi");
CmdLineParser.Option disableOptimizationsOpt = parser.addBooleanOption("disable-optimizations");
CmdLineParser.Option helpOpt = parser.addBooleanOption('h', "help");
@@ -72,6 +73,10 @@ public static void main(String args[]) {
}
}
+ String globalsStr = (String) parser.getOptionValue(globalsOpt);
+ String[] globals = globalsStr != null ?
+ globalsStr.split(",") : new String[0];
+
String type = (String) parser.getOptionValue(typeOpt);
if (type != null && !type.equalsIgnoreCase("js") && !type.equalsIgnoreCase("css")) {
usage();
@@ -124,7 +129,7 @@ public static void main(String args[]) {
try {
- JavaScriptCompressor compressor = new JavaScriptCompressor(in, new ErrorReporter() {
+ JavaScriptCompressor compressor = new JavaScriptCompressor(globals, in, new ErrorReporter() {
public void warning(String message, String sourceName,
int line, String lineSource, int lineOffset) {

0 comments on commit d27f3ad

Please sign in to comment.