diff --git a/packages/babel-generator/src/index.js b/packages/babel-generator/src/index.js index 711a335675..124789ef28 100644 --- a/packages/babel-generator/src/index.js +++ b/packages/babel-generator/src/index.js @@ -70,6 +70,11 @@ function normalizeOptions(code, opts, tokens): Format { if (format.minified) { format.compact = true; + + format.shouldPrintComment = format.shouldPrintComment || (() => format.comments); + } else { + format.shouldPrintComment = format.shouldPrintComment || ((value) => format.comments || + (value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0)); } if (format.compact === "auto") { diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 323342e54d..a03c4f0f31 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -490,21 +490,8 @@ export default class Printer { return (node && (leading ? node.leadingComments : node.trailingComments)) || []; } - _shouldPrintComment(comment) { - if (this.format.shouldPrintComment) { - return this.format.shouldPrintComment(comment.value); - } else { - if (!this.format.minified && - (comment.value.indexOf("@license") >= 0 || comment.value.indexOf("@preserve") >= 0)) { - return true; - } else { - return this.format.comments; - } - } - } - _printComment(comment) { - if (!this._shouldPrintComment(comment)) return; + if (!this.format.shouldPrintComment(comment.value)) return; // Some plugins use this to mark comments as removed using the AST-root 'comments' property, // where they can't manually mutate the AST node comment lists.