Skip to content

Commit

Permalink
Separate babili opts from gulp-plugin opts
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Dec 4, 2016
1 parent dca0fa8 commit ba931a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
6 changes: 3 additions & 3 deletions packages/gulp-babili/__tests__/gulp-babili-test.js
Expand Up @@ -81,7 +81,7 @@ describe("gulp-babili", () => {
return babiliPreset(...args);
};

const stream = gulpBabili({
const stream = gulpBabili({}, {
babel,
babili,
});
Expand Down 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
35 changes: 11 additions & 24 deletions packages/gulp-babili/src/index.js
Expand Up @@ -10,9 +10,11 @@ const NAME = "gulp-babili";

module.exports = gulpBabili;

function gulpBabili(_opts = {}) {
const opts = Object.assign({}, _opts);

function gulpBabili(babiliOpts = {}, {
babel = babelCore,
babili = babiliPreset,
comments = /@preserve|@license/
} = {}) {
return through2.obj(function (file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
Expand All @@ -22,22 +24,6 @@ function gulpBabili(_opts = {}) {
return callback(new PluginError(NAME, "Streaming not supported"));
}

let babel = babelCore;
let babili = babiliPreset;

if (opts.babel) {
babel = opts.babel;
delete opts.babel;
}
if (opts.babili) {
babili = opts.babili;
delete opts.babili;
}

const commentsRegex = typeof opts.comments === "undefined"
? /@preserve|@license/
: opts.comments;

let inputSourceMap;
if (file.sourceMap && file.sourceMap.mappings) {
inputSourceMap = file.sourceMap;
Expand All @@ -49,15 +35,15 @@ function gulpBabili(_opts = {}) {
ast: false,

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

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

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

/* file */
Expand All @@ -73,7 +59,9 @@ function gulpBabili(_opts = {}) {

if (success) {
file.contents = new Buffer(result.code);
applySourceMap(file, result.map);
if (file.sourceMap) {
applySourceMap(file, result.map);
}
return callback(null, file);
}

Expand Down Expand Up @@ -102,4 +90,3 @@ function shouldPrintComment(contents, predicate) {
default: return !!predicate;
}
}

0 comments on commit ba931a8

Please sign in to comment.