Add "preamble" output option

Close #335
This commit is contained in:
Mihai Bazon
2013-10-29 10:43:43 +02:00
parent 0358e376f0
commit f1b7094a57
3 changed files with 19 additions and 1 deletions

View File

@@ -87,6 +87,10 @@ The available options are:
Note that currently not *all* comments can be kept when Note that currently not *all* comments can be kept when
compression is on, because of dead code removal or compression is on, because of dead code removal or
cascading statements into sequences. [string] 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] --stats Display operations run time on STDERR. [boolean]
--acorn Use Acorn for parsing. [boolean] --acorn Use Acorn for parsing. [boolean]
--spidermonkey Assume input files are SpiderMonkey AST format (as JSON). --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 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 semicolon, leading to more readable output of uglified code (size before
gzip could be smaller; size after gzip insignificantly larger). 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 ### Keeping copyright notices or other comments

View File

@@ -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, \ Note that currently not *all* comments can be kept when compression is on, \
because of dead code removal or cascading statements into sequences.") 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("stats", "Display operations run time on STDERR.")
.describe("acorn", "Use Acorn for parsing.") .describe("acorn", "Use Acorn for parsing.")
.describe("spidermonkey", "Assume input files are SpiderMonkey AST format (as JSON).") .describe("spidermonkey", "Assume input files are SpiderMonkey AST format (as JSON).")
@@ -134,7 +138,8 @@ if (ARGS.r) {
} }
var OUTPUT_OPTIONS = { var OUTPUT_OPTIONS = {
beautify: BEAUTIFY ? true : false beautify: BEAUTIFY ? true : false,
preamble: ARGS.preamble || null,
}; };
if (ARGS.screw_ie8) { if (ARGS.screw_ie8) {

View File

@@ -61,6 +61,7 @@ function OutputStream(options) {
comments : false, comments : false,
preserve_line : false, preserve_line : false,
screw_ie8 : false, screw_ie8 : false,
preamble : null,
}, true); }, true);
var indentation = 0; var indentation = 0;
@@ -299,6 +300,10 @@ function OutputStream(options) {
return OUTPUT; return OUTPUT;
}; };
if (options.preamble) {
print(options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
}
var stack = []; var stack = [];
return { return {
get : get, get : get,