Add "preamble" output option
authorMihai Bazon <mihai@bazon.net>
Tue, 29 Oct 2013 08:43:43 +0000 (10:43 +0200)
committerMihai Bazon <mihai@bazon.net>
Tue, 29 Oct 2013 09:09:18 +0000 (11:09 +0200)
Close #335

README.md
bin/uglifyjs
lib/output.js

index 3a4d27a..72348d1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -87,6 +87,10 @@ The available options are:
                      Note that currently not *all* comments can be kept when
                      compression is on, because of dead code removal or
                      cascading statements into sequences.               [string]
+  --preamble         Preamble to prepend to the output.  You can use this to
+                     insert a comment, for example for licensing information.
+                     This will not be parsed, but the source map will adjust
+                     for its presence.
   --stats            Display operations run time on STDERR.            [boolean]
   --acorn            Use Acorn for parsing.                            [boolean]
   --spidermonkey     Assume input files are SpiderMonkey AST format (as JSON).
@@ -328,6 +332,10 @@ can pass additional arguments that control the code output:
   you pass `false` then whenever possible we will use a newline instead of a
   semicolon, leading to more readable output of uglified code (size before
   gzip could be smaller; size after gzip insignificantly larger).
+- `preamble` (default `null`) -- when passed it must be a string and
+  it will be prepended to the output literally.  The source map will
+  adjust for this text.  Can be used to insert a comment containing
+  licensing information, for example.
 
 ### Keeping copyright notices or other comments
 
index b103cc3..9bdb8ea 100755 (executable)
@@ -48,6 +48,10 @@ You can optionally pass one of the following arguments to this flag:\n\
 Note that currently not *all* comments can be kept when compression is on, \
 because of dead code removal or cascading statements into sequences.")
 
+    .describe("preamble", "Preamble to prepend to the output.  You can use this to insert a \
+comment, for example for licensing information.  This will not be \
+parsed, but the source map will adjust for its presence.")
+
     .describe("stats", "Display operations run time on STDERR.")
     .describe("acorn", "Use Acorn for parsing.")
     .describe("spidermonkey", "Assume input files are SpiderMonkey AST format (as JSON).")
@@ -134,7 +138,8 @@ if (ARGS.r) {
 }
 
 var OUTPUT_OPTIONS = {
-    beautify: BEAUTIFY ? true : false
+    beautify: BEAUTIFY ? true : false,
+    preamble: ARGS.preamble || null,
 };
 
 if (ARGS.screw_ie8) {
index 7eb685a..0114783 100644 (file)
@@ -61,6 +61,7 @@ function OutputStream(options) {
         comments      : false,
         preserve_line : false,
         screw_ie8     : false,
+        preamble      : null,
     }, true);
 
     var indentation = 0;
@@ -299,6 +300,10 @@ function OutputStream(options) {
         return OUTPUT;
     };
 
+    if (options.preamble) {
+        print(options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
+    }
+
     var stack = [];
     return {
         get             : get,