public static void Mangle(InputContext context) { var mangling = new ManglingContext(); // Mangle the content in each file (generated code will // need to be inserted into one of these to be mangled) foreach (var input in context.inputs) { input.tree.AcceptVisitor(mangling); } }
public static bool Lower(InputContext context) { var lowering = new LoweringContext(context); // Cache information about important types foreach (var code in knownTypeCodes) { lowering.knownTypes[context.compilation.FindType(code)] = code; } // Lower the content in each file (generated code will // need to be inserted into one of these to be lowered) foreach (var input in context.inputs) { lowering.resolver = input.resolver; lowering.input = input; input.tree.AcceptVisitor(lowering); } return lowering.wasSuccessful; }
public static int Main(string[] args) { var boolFlags = new Dictionary<string, bool> { { "--minify", false }, { "--mangle", false }, { "--timing", false }, { "--server", false }, { "--source-map", false }, }; string outputPath = null; // Parse command-line arguments var inputs = new List<Input>(); for (var i = 0; i < args.Length; i++) { var arg = args[i]; // Help if (arg == "-h" || arg == "-help" || arg == "--help" || arg == "-?" || arg == "/?") { WriteUsage(); return 0; } // Boolean flags if (boolFlags.ContainsKey(arg)) { if (boolFlags[arg]) { Console.WriteLine("Duplicate flag \"" + arg + "\""); return 1; } boolFlags[arg] = true; continue; } // Output file if (arg == "--output" || arg == "-o") { if (outputPath != null) { Console.WriteLine("Duplicate flag \"" + arg + "\""); return 1; } if (i + 1 == args.Length) { Console.WriteLine("Missing path for flag \"" + arg + "\""); return 1; } outputPath = args[++i]; continue; } // Invalid flags if (arg.StartsWith("-")) { Console.WriteLine("Invalid flag \"" + arg + "\""); return 1; } // Input files inputs.Add(new Input(arg, File.ReadAllText(arg))); } // The server option ignores all other options if (boolFlags["--server"]) { return RunLocalServer() ? 0 : 1; } // Show usage if there are no inputs if (inputs.Count == 0) { WriteUsage(); return 1; } // Parse inputs var context = new InputContext(); if (!context.Compile(inputs)) { Console.Write(context.GenerateLog()); return 1; } // Generate output var output = new OutputContext(context); output.ShouldMinify = boolFlags["--minify"]; output.ShouldMangle = boolFlags["--mangle"]; output.SourceMap = boolFlags["--source-map"] ? outputPath != null ? SourceMap.External : SourceMap.Inline : SourceMap.None; if (outputPath != null) { File.WriteAllText(outputPath, output.Code); if (output.SourceMap == SourceMap.External) { File.WriteAllText(outputPath + ".map", output.SourceMapCode); } } else { Console.Write(output.Code); } // Write out timing info for debugging if (boolFlags["--timing"]) { foreach (var pair in context.timingInMilliseconds) { Console.Error.WriteLine(pair.Key + ": " + pair.Value + "ms"); } } return 0; }
public static bool RunLocalServer() { if (!HttpListener.IsSupported) { return false; } var listener = new HttpListener(); var address = "http://localhost:8008/"; listener.Prefixes.Add(address); listener.Start(); Console.WriteLine("Serving on " + address); while (true) { var context = listener.GetContext(); var request = context.Request; var response = context.Response; var content = new StreamReader(request.InputStream, request.ContentEncoding).ReadToEnd(); var input = new InputContext(); var responseText = ""; try { if (!input.Compile(new List<Input> { new Input("<input>", content) })) { responseText = input.GenerateLog(); } else { var output = new OutputContext(input); output.ShouldMinify = request.QueryString["minify"] == "true"; output.ShouldMangle = request.QueryString["mangle"] == "true"; output.SourceMap = request.QueryString["sourceMap"] == "true" ? SourceMap.Inline : SourceMap.None; responseText = output.Code; } } catch (Exception error) { responseText = error.Message + "\n" + error.StackTrace; } var buffer = Encoding.UTF8.GetBytes(responseText); response.Headers["Access-Control-Allow-Origin"] = "*"; response.ContentLength64 = buffer.Length; try { response.OutputStream.Write(buffer, 0, buffer.Length); } catch (IOException) { } response.OutputStream.Close(); } }
public Visitor(InputContext context) { this.context = context; }
private LoweringContext(InputContext context) { this.context = context; }