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]"
|
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: {
|
sourceMap: {
|
||||||
alias: "sourceMaps",
|
alias: "sourceMaps",
|
||||||
hidden: true
|
hidden: true
|
||||||
|
|||||||
@ -101,7 +101,7 @@ export default class Buffer {
|
|||||||
|
|
||||||
rightBrace() {
|
rightBrace() {
|
||||||
this.newline(true);
|
this.newline(true);
|
||||||
if (this.format.compact && !this._lastPrintedIsEmptyStatement) {
|
if (this.format.minified && !this._lastPrintedIsEmptyStatement) {
|
||||||
this._removeLast(";");
|
this._removeLast(";");
|
||||||
}
|
}
|
||||||
this.push("}");
|
this.push("}");
|
||||||
|
|||||||
@ -62,7 +62,7 @@ export function ConditionalExpression(node: Object) {
|
|||||||
export function NewExpression(node: Object, parent: Object) {
|
export function NewExpression(node: Object, parent: Object) {
|
||||||
this.push("new ");
|
this.push("new ");
|
||||||
this.print(node.callee, node);
|
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.isCallExpression(parent, { callee: node }) &&
|
||||||
!t.isMemberExpression(parent) &&
|
!t.isMemberExpression(parent) &&
|
||||||
!t.isNewExpression(parent)) return;
|
!t.isNewExpression(parent)) return;
|
||||||
@ -222,7 +222,7 @@ export function MemberExpression(node: Object) {
|
|||||||
} else {
|
} else {
|
||||||
if (t.isLiteral(node.object) && !t.isTemplateLiteral(node.object)) {
|
if (t.isLiteral(node.object) && !t.isTemplateLiteral(node.object)) {
|
||||||
let val;
|
let val;
|
||||||
if (this.format.compact) {
|
if (this.format.minified) {
|
||||||
val = this._stringLiteral(node.object);
|
val = this._stringLiteral(node.object);
|
||||||
} else {
|
} else {
|
||||||
val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
|
val = this.getPossibleRaw(node.object) || this._stringLiteral(node.object);
|
||||||
|
|||||||
@ -40,6 +40,7 @@ export class CodeGenerator extends Printer {
|
|||||||
auxiliaryCommentBefore: string;
|
auxiliaryCommentBefore: string;
|
||||||
auxiliaryCommentAfter: string;
|
auxiliaryCommentAfter: string;
|
||||||
compact: boolean | "auto";
|
compact: boolean | "auto";
|
||||||
|
minified: boolean;
|
||||||
quotes: "single" | "double";
|
quotes: "single" | "double";
|
||||||
concise: boolean;
|
concise: boolean;
|
||||||
indent: {
|
indent: {
|
||||||
@ -81,6 +82,7 @@ export class CodeGenerator extends Printer {
|
|||||||
retainLines: opts.retainLines,
|
retainLines: opts.retainLines,
|
||||||
comments: opts.comments == null || opts.comments,
|
comments: opts.comments == null || opts.comments,
|
||||||
compact: opts.compact,
|
compact: opts.compact,
|
||||||
|
minified: opts.minified,
|
||||||
concise: opts.concise,
|
concise: opts.concise,
|
||||||
quotes: CodeGenerator.findCommonStringDelimiter(code, tokens),
|
quotes: CodeGenerator.findCommonStringDelimiter(code, tokens),
|
||||||
indent: {
|
indent: {
|
||||||
@ -90,6 +92,10 @@ export class CodeGenerator extends Printer {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (format.minified) {
|
||||||
|
format.compact = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (format.compact === "auto") {
|
if (format.compact === "auto") {
|
||||||
format.compact = code.length > 100000; // 100KB
|
format.compact = code.length > 100000; // 100KB
|
||||||
|
|
||||||
|
|||||||
@ -97,11 +97,11 @@ export default class Printer extends Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_print(node, parent) {
|
_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.
|
// and need to make sure that string quoting is consistent.
|
||||||
// That means we have to always reprint as opposed to getting
|
// That means we have to always reprint as opposed to getting
|
||||||
// the raw value.
|
// the raw value.
|
||||||
if (!this.format.compact) {
|
if (!this.format.minified) {
|
||||||
let extra = this.getPossibleRaw(node);
|
let extra = this.getPossibleRaw(node);
|
||||||
if (extra) {
|
if (extra) {
|
||||||
this.push("");
|
this.push("");
|
||||||
@ -238,7 +238,7 @@ export default class Printer extends Buffer {
|
|||||||
if (this.format.shouldPrintComment) {
|
if (this.format.shouldPrintComment) {
|
||||||
return this.format.shouldPrintComment(comment.value);
|
return this.format.shouldPrintComment(comment.value);
|
||||||
} else {
|
} else {
|
||||||
if (!this.format.compact &&
|
if (!this.format.minified &&
|
||||||
(comment.value.indexOf("@license") >= 0 || comment.value.indexOf("@preserve") >= 0)) {
|
(comment.value.indexOf("@license") >= 0 || comment.value.indexOf("@preserve") >= 0)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
function foo(l){
|
function foo(l){
|
||||||
return (
|
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