Skip to content

Commit

Permalink
Move shouldPrintComments to babili-preset
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Jan 20, 2017
1 parent 86a5d6a commit 643c870
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
18 changes: 17 additions & 1 deletion packages/babel-preset-babili/src/index.js
Expand Up @@ -105,12 +105,28 @@ function preset(context, _opts = {}) {

const plugins = generate(optionsTree, opts);

// comments
let comments = /@preserve|@licen(s|c)e/;
if (Object.prototype.hasOwnProperty.call(opts, "comments")) {
comments = opts.comments;
}

return {
minified: true,
comments: false,
shouldPrintComment(contents) {
return _shouldPrintComment(contents, comments);
},
presets: [
{ plugins }
],
passPerPreset: true,
};
}

function _shouldPrintComment(contents, predicate) {
switch (typeof predicate) {
case "function": return predicate(contents);
case "object": return predicate.test(contents);
default: return !!predicate;
}
}
4 changes: 2 additions & 2 deletions packages/gulp-babili/__tests__/gulp-babili-test.js
Expand Up @@ -136,7 +136,7 @@ describe("gulp-babili", () => {

it("should remove all comments when false", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili({}, {
const stream = gulpBabili({
comments: false
});
stream.on("data", () => {
Expand All @@ -150,7 +150,7 @@ describe("gulp-babili", () => {

it("should take a custom function", () => {
return new Promise((resolve, reject) => {
const stream = gulpBabili({}, {
const stream = gulpBabili({
comments(contents) {
return contents.indexOf("YAC") !== -1;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gulp-babili/package.json
Expand Up @@ -13,8 +13,8 @@
"babili"
],
"dependencies": {
"babel-core": "^6.18.2",
"babel-preset-babili": "^0.0.9",
"babel-core": "^6.22.1",
"babel-preset-babili": "^0.0.10",
"vinyl-sourcemaps-apply": "^0.2.1"
},
"devDependencies": {}
Expand Down
17 changes: 2 additions & 15 deletions packages/gulp-babili/src/index.js
Expand Up @@ -13,7 +13,6 @@ module.exports = gulpBabili;
function gulpBabili(babiliOpts = {}, {
babel = babelCore,
babili = babiliPreset,
comments = /@preserve|@license/
} = {}) {
return through2.obj(function (file, enc, callback) {
if (file.isNull()) {
Expand All @@ -31,21 +30,17 @@ function gulpBabili(babiliOpts = {}, {

const babelOpts = {
minified: true,
compact: true,
babelrc: false,
ast: false,

/* preset */
presets: [[babili, babiliOpts]],
presets: [ [ babili, babiliOpts ] ],

/* sourcemaps */
sourceMaps: !!file.sourceMap,
inputSourceMap,

/* remove comments */
shouldPrintComment(contents) {
return shouldPrintComment(contents, comments);
},

/* file */
filename: file.path,
filenameRelative: file.relative,
Expand Down Expand Up @@ -82,11 +77,3 @@ function transform({ babel, input, babelOpts }) {
};
}
}

function shouldPrintComment(contents, predicate) {
switch (typeof predicate) {
case "function": return predicate(contents);
case "object": return predicate.test(contents);
default: return !!predicate;
}
}

0 comments on commit 643c870

Please sign in to comment.