Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve comments handling in presets + api + gulp #855

Merged
merged 3 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function(t) {

function convert(nodes) {
const exprs = [];
const comments = [];

for (let i = 0; i < nodes.length; i++) {
const bail = () => {
Expand All @@ -47,6 +48,9 @@ module.exports = function(t) {
if (t.isExpression(node)) {
exprs.push(node);
} else if (t.isExpressionStatement(node)) {
if (node.leadingComments) {
comments.push(...node.leadingComments);
}
if (node.expression) exprs.push(node.expression);
} else if (t.isIfStatement(node)) {
let consequent;
Expand Down Expand Up @@ -97,6 +101,14 @@ module.exports = function(t) {
seq = t.sequenceExpression(exprs);
}

/**
* collect all the comment ast nodes that are before expression
* statments and add it to the new generated node
*/
if (seq) {
seq.leadingComments = comments;
}

/* eslint-disable no-self-assign */
seq = seq;
return { seq };
Expand Down
1 change: 1 addition & 0 deletions packages/babel-minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Refer [babel-preset-minify options](https://github.com/babel/minify/tree/master/
+ `minifyPreset`: Custom minify preset
+ `inputSourceMap`: Input Sourcemap
+ `sourceMaps`: [Boolean]
+ `comments`: [Function | RegExp | Boolean]

## CLI Options

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ exports[`babel-minify Node API override default minify options 1`] = `"function

exports[`babel-minify Node API override nested minify options 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`;

exports[`babel-minify Node API preserve default comments 1`] = `"/* @license MIT */(function(){/*! mylib.js */(function(){})()})();"`;

exports[`babel-minify Node API remove comments 1`] = `"var a=10;!function(){}();"`;

exports[`babel-minify Node API simple usage 1`] = `"function foo(){const a=x(1),b=y(2);return z(a,b)}"`;
24 changes: 24 additions & 0 deletions packages/babel-minify/__tests__/node-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,28 @@ describe("babel-minify Node API", () => {
const minifyOpts = { mangle: { keepFnName: false } };
expect(minify(sampleInput, minifyOpts).code).toMatchSnapshot();
});

it("preserve default comments", () => {
const code = `
/* @license MIT */
(function() {
/*! mylib.js */
function a() {}
a();
})();
`;

expect(minify(code, {}).code).toMatchSnapshot();
});

it("remove comments ", () => {
const code = `
/* foo */
var a = 10;

!function(){}() // blah
`;

expect(minify(code, {}).code).toMatchSnapshot();
});
});
30 changes: 25 additions & 5 deletions packages/babel-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,49 @@ module.exports = function babelMinify(
input,
// Minify options passed to minifyPreset
// defaults are handled in preset
options = {},
minifyOptions = {},
// overrides and other options
{
minified = true,
inputSourceMap,
sourceMaps = false,
sourceType = "script",
comments = /^\**!|@preserve|@licen[sc]e|@cc_on/,

// to override the default babelCore used
babel = babelCore,

// to override the default minify preset used
minifyPreset = babelPresetMinify
minifyPreset = babelPresetMinify,

// passthrough to babel
filename,
filenameRelative
} = {}
) {
return babel.transformSync(input, {
babelrc: false,
configFile: false,
presets: [[minifyPreset, options]],
comments: false,
presets: [[minifyPreset, minifyOptions]],
shouldPrintComment(contents) {
return shouldPrintComment(contents, comments);
},
inputSourceMap,
sourceMaps,
minified,
sourceType
sourceType,
filename,
filenameRelative
});
};

function shouldPrintComment(contents, predicate) {
switch (typeof predicate) {
case "function":
return predicate(contents);
case "object":
return predicate.test(contents);
default:
return !!predicate;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// FIXME: for some reason, the inner `if` statement gets indented 4 spaces.
`
function foo() {
if (a) {
if (b()) return false;
} else if (c()) return true;
}
`
function foo() {
if (a) {
if (b()) return false;
} else if (c()) return true;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
`
function foo() {
if (a) {
if (b()) return false;
} else if (c()) return true;
}
`;
function foo() {
if (a) {
if (b()) return false;
} else if (c()) return true;
}
22 changes: 0 additions & 22 deletions packages/babel-preset-minify/__tests__/preset-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,6 @@ describe("preset", () => {
`
);

thePlugin(
"should fix remove comments",
`
var asdf = 1; // test
`,
`
var asdf = 1;
`
);

thePlugin(
"should keep license/preserve annotated comments",
`
/* @license */
var asdf = 1;
`,
`
/* @license */
var asdf = 1;
`
);

thePlugin(
"should fix issue#385 - impure if statements with Sequence and DCE",
`
Expand Down
1 change: 0 additions & 1 deletion packages/babel-preset-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ function preset(context, _opts = {}) {

return {
minified: true,
comments: false,
presets: [{ plugins }],
passPerPreset: true
};
Expand Down
1 change: 1 addition & 0 deletions packages/gulp-babel-minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Default: `{}`

+ `babel`: Use a custom `@babel/core`
+ `minifyPreset`: Use a custom `babel-preset-minify`
+ `comments`: [Function | RegExp | Boolean]
2 changes: 1 addition & 1 deletion packages/gulp-babel-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function gulpBabelMinify(
{
babel = babelCore,
minifyPreset = babelPresetMinify,
comments = /preserve|licen(s|c)e/,
comments = /^\**!|@preserve|@licen[sc]e|@cc_on/,
sourceType = "script"
} = {}
) {
Expand Down