From f1b7094a57cc3fbbe37a2c653f255190b9359c0d Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Tue, 29 Oct 2013 10:43:43 +0200 Subject: [PATCH] Add "preamble" output option Close #335 --- README.md | 8 ++++++++ bin/uglifyjs | 7 ++++++- lib/output.js | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a4d27a5..72348d10 100644 --- 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 diff --git a/bin/uglifyjs b/bin/uglifyjs index b103cc3e..9bdb8ea7 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -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) { diff --git a/lib/output.js b/lib/output.js index 7eb685a0..0114783a 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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, -- 2.34.1