Merge pull request #3152 from babel/minified-format
Add minified format option
This commit is contained in:
commit
d2e7e6a54d
@ -106,6 +106,12 @@ module.exports = {
|
||||
description: "do not include superfluous whitespace characters and line terminators [true|false|auto]"
|
||||
},
|
||||
|
||||
minified: {
|
||||
type: "boolean",
|
||||
default: false,
|
||||
description: "save as much bytes when printing [true|false]"
|
||||
},
|
||||
|
||||
sourceMap: {
|
||||
alias: "sourceMaps",
|
||||
hidden: true
|
||||
|
||||
@ -101,7 +101,7 @@ export default class Buffer {
|
||||
|
||||
rightBrace() {
|
||||
this.newline(true);
|
||||
if (this.format.compact && !this._lastPrintedIsEmptyStatement) {
|
||||
if (this.format.minified && !this._lastPrintedIsEmptyStatement) {
|
||||
this._removeLast(";");
|
||||
}
|
||||
this.push("}");
|
||||
|
||||
@ -62,7 +62,7 @@ export function ConditionalExpression(node: Object) {
|
||||
export function NewExpression(node: Object, parent: Object) {
|
||||
this.push("new ");
|
||||
this.print(node.callee, node);
|
||||
if (node.arguments.length === 0 && this.format.compact &&
|
||||
if (node.arguments.length === 0 && this.format.minified &&
|
||||
!t.isCallExpression(parent, { callee: node }) &&
|
||||
!t.isMemberExpression(parent) &&
|
||||
!t.isNewExpression(parent)) return;
|
||||
@ -222,7 +222,7 @@ export function MemberExpression(node: Object) {
|
||||
} else {
|
||||
if (t.isLiteral(node.object) && !t.isTemplateLiteral(node.object)) {
|
||||
let val;
|
||||
if (this.format.compact) {
|
||||
if (this.format.minified) {
|
||||
val = this._stringLiteral(node.object);
|
||||
} else {
|
||||
val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
|
||||
|
||||
@ -40,6 +40,7 @@ export class CodeGenerator extends Printer {
|
||||
auxiliaryCommentBefore: string;
|
||||
auxiliaryCommentAfter: string;
|
||||
compact: boolean | "auto";
|
||||
minified: boolean;
|
||||
quotes: "single" | "double";
|
||||
concise: boolean;
|
||||
indent: {
|
||||
@ -81,6 +82,7 @@ export class CodeGenerator extends Printer {
|
||||
retainLines: opts.retainLines,
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: opts.compact,
|
||||
minified: opts.minified,
|
||||
concise: opts.concise,
|
||||
quotes: CodeGenerator.findCommonStringDelimiter(code, tokens),
|
||||
indent: {
|
||||
@ -90,6 +92,10 @@ export class CodeGenerator extends Printer {
|
||||
}
|
||||
};
|
||||
|
||||
if (format.minified) {
|
||||
format.compact = true;
|
||||
}
|
||||
|
||||
if (format.compact === "auto") {
|
||||
format.compact = code.length > 100000; // 100KB
|
||||
|
||||
|
||||
@ -97,11 +97,11 @@ export default class Printer extends Buffer {
|
||||
}
|
||||
|
||||
_print(node, parent) {
|
||||
// In compact mode we need to produce as little bytes as needed
|
||||
// In minified mode we need to produce as little bytes as needed
|
||||
// and need to make sure that string quoting is consistent.
|
||||
// That means we have to always reprint as opposed to getting
|
||||
// the raw value.
|
||||
if (!this.format.compact) {
|
||||
if (!this.format.minified) {
|
||||
let extra = this.getPossibleRaw(node);
|
||||
if (extra) {
|
||||
this.push("");
|
||||
@ -238,7 +238,7 @@ export default class Printer extends Buffer {
|
||||
if (this.format.shouldPrintComment) {
|
||||
return this.format.shouldPrintComment(comment.value);
|
||||
} else {
|
||||
if (!this.format.compact &&
|
||||
if (!this.format.minified &&
|
||||
(comment.value.indexOf("@license") >= 0 || comment.value.indexOf("@preserve") >= 0)) {
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
function foo(l){
|
||||
return (
|
||||
|
||||
l)}
|
||||
l);}
|
||||
|
||||
3
packages/babel-generator/test/fixtures/minified/options.json
vendored
Normal file
3
packages/babel-generator/test/fixtures/minified/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"minified": true
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user