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
Gulp babili #318
Merged
Merged
Gulp babili #318
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
365c7de
Add gulp-babili
boopathi 3340ce2
0.0.1 first version
boopathi 5ded7a2
Separate babili opts from gulp-plugin opts
boopathi eb88bbf
Lint fixes
boopathi f3b94b2
Move shouldPrintComments to babili-preset
boopathi a05da73
Disable the comments tests
boopathi 1c334df
Add README
boopathi 01683c5
Add shouldPrintComment
boopathi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
src | ||
__tests__ | ||
node_modules | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# gulp-babili | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install gulp-babili --save-dev | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const gulp = require("gulp"); | ||
const babili = require("gulp-babili"); | ||
|
||
gulp.task("minify", () => | ||
gulp.src("./build/app.js") | ||
.pipe(babili({ | ||
mangle: { | ||
keepClassNames: true | ||
} | ||
})) | ||
.pipe(gulp.dest("./dist")); | ||
); | ||
``` | ||
|
||
## API | ||
|
||
```js | ||
gulpBabili(babiliOptions, overrides); | ||
``` | ||
|
||
### babiliOptions | ||
|
||
These are passed on to the babili preset. Refer https://github.com/babel/babili/tree/master/packages/babel-preset-babili#options. Default `{}` | ||
|
||
### Overrides | ||
|
||
Default: `{}` | ||
|
||
+ `babel`: Use a custom `babel-core` | ||
+ `babili`: Use a custom `babel-preset-babili` |
11 changes: 11 additions & 0 deletions
11
packages/gulp-babili/__tests__/__snapshots__/gulp-babili-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`gulp-babili comments should remove all comments when false 1`] = `"foo(),bar(),baz();"`; | ||
|
||
exports[`gulp-babili comments should remove comments by default except license and preserve 1`] = ` | ||
/** | ||
* @license | ||
* This is a test | ||
*/"foo(),bar(),baz();"`; | ||
|
||
exports[`gulp-babili comments should take a custom function 1`] = `"/* YAC - yet another comment */foo(),bar(),baz();"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
jest.autoMockOff(); | ||
|
||
const gutil = require("gulp-util"); | ||
const babelCore = require("babel-core"); | ||
const babiliPreset = require("babel-preset-babili"); | ||
|
||
const unpad = require("../../../utils/unpad"); | ||
const gulpBabili = require("../src/index"); | ||
|
||
describe("gulp-babili", () => { | ||
it("should work with a good default", () => { | ||
return new Promise((resolve, reject) => { | ||
const stream = gulpBabili(); | ||
|
||
const source = unpad(` | ||
function foo() { | ||
const a = 10; | ||
return a; | ||
} | ||
`); | ||
const expected = "function foo(){return 10}"; | ||
|
||
stream.on("data", function (file) { | ||
expect(file.contents.toString()).toBe(expected); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
|
||
stream.write(new gutil.File({ | ||
path: "defaults.js", | ||
contents: new Buffer(source), | ||
})); | ||
}); | ||
}); | ||
|
||
it("should take options and pass them to babili", () => { | ||
return new Promise((resolve, reject) => { | ||
const stream = gulpBabili({ | ||
mangle: { | ||
blacklist: { | ||
bar: true | ||
} | ||
} | ||
}); | ||
|
||
const source = unpad(` | ||
function foo(bar, baz) { | ||
return bar + baz; | ||
} | ||
`); | ||
const expected = "function foo(bar,a){return bar+a}"; | ||
|
||
stream.on("data", function (file) { | ||
expect(file.contents.toString()).toBe(expected); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
|
||
stream.write(new gutil.File({ | ||
path: "options.js", | ||
contents: new Buffer(source) | ||
})); | ||
}); | ||
}); | ||
|
||
it("should take custom babel and babili", () => { | ||
return new Promise((resolve, reject) => { | ||
const babel = Object.assign({}, babelCore); | ||
|
||
let usedTransform = false; | ||
Object.defineProperty(babel, "transform", { | ||
get() { | ||
usedTransform = true; | ||
return babelCore.transform; | ||
} | ||
}); | ||
|
||
let usedPreset = false; | ||
const babili = function (...args) { | ||
usedPreset = true; | ||
return babiliPreset(...args); | ||
}; | ||
|
||
const stream = gulpBabili({}, { | ||
babel, | ||
babili, | ||
}); | ||
|
||
stream.on("data", function () { | ||
expect(usedTransform).toBe(true); | ||
expect(usedPreset).toBe(true); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
|
||
stream.write(new gutil.File({ | ||
path: "custom-transformers.js", | ||
contents: new Buffer("foo()") | ||
})); | ||
}); | ||
}); | ||
|
||
describe("comments", () => { | ||
const source = unpad(` | ||
/** | ||
* @license | ||
* This is a test | ||
*/ | ||
foo(); | ||
// this is another comment | ||
bar(); | ||
/* YAC - yet another comment */ | ||
baz(); | ||
`); | ||
|
||
let file; | ||
|
||
beforeEach(() => { | ||
file = new gutil.File({ | ||
path: "comments.js", | ||
contents: new Buffer(source) | ||
}); | ||
}); | ||
|
||
xit("should remove comments by default except license and preserve", () => { | ||
return new Promise((resolve, reject) => { | ||
const stream = gulpBabili(); | ||
stream.on("data", function (file) { | ||
expect(file.contents.toString()).toMatchSnapshot(); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
stream.write(file); | ||
}); | ||
}); | ||
|
||
it("should remove all comments when false", () => { | ||
return new Promise((resolve, reject) => { | ||
const stream = gulpBabili({ | ||
comments: false | ||
}); | ||
stream.on("data", () => { | ||
expect(file.contents.toString()).toMatchSnapshot(); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
stream.write(file); | ||
}); | ||
}); | ||
|
||
xit("should take a custom function", () => { | ||
return new Promise((resolve, reject) => { | ||
const stream = gulpBabili({ | ||
comments(contents) { | ||
return contents.indexOf("YAC") !== -1; | ||
} | ||
}); | ||
stream.on("data", () => { | ||
expect(file.contents.toString()).toMatchSnapshot(); | ||
resolve(); | ||
}); | ||
stream.on("error", reject); | ||
stream.write(file); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "gulp-babili", | ||
"version": "0.0.1", | ||
"description": "Gulp Babili", | ||
"homepage": "https://github.com/babel/babili#readme", | ||
"repository": "https://github.com/babel/babili/tree/master/packages/gulp-babili", | ||
"bugs": "https://github.com/babel/babili/issues", | ||
"author": "boopathi", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-preset", | ||
"babili" | ||
], | ||
"dependencies": { | ||
"babel-core": "^6.22.1", | ||
"babel-preset-babili": "^0.0.10", | ||
"vinyl-sourcemaps-apply": "^0.2.1" | ||
}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const babelCore = require("babel-core"); | ||
const through2 = require("through2"); | ||
const gutil = require("gulp-util"); | ||
const applySourceMap = require("vinyl-sourcemaps-apply"); | ||
const babiliPreset = require("babel-preset-babili"); | ||
|
||
const { PluginError } = gutil; | ||
|
||
const NAME = "gulp-babili"; | ||
|
||
module.exports = gulpBabili; | ||
|
||
function gulpBabili(babiliOpts = {}, { | ||
babel = babelCore, | ||
babili = babiliPreset, | ||
comments = /preserve|licen(s|c)e/ | ||
} = {}) { | ||
return through2.obj(function (file, enc, callback) { | ||
if (file.isNull()) { | ||
return callback(null, file); | ||
} | ||
|
||
if (file.isStream()) { | ||
return callback(new PluginError(NAME, "Streaming not supported")); | ||
} | ||
|
||
let inputSourceMap; | ||
if (file.sourceMap && file.sourceMap.mappings) { | ||
inputSourceMap = file.sourceMap; | ||
} | ||
|
||
const babelOpts = { | ||
minified: true, | ||
babelrc: false, | ||
ast: false, | ||
|
||
/* preset */ | ||
presets: [ [ babili, babiliOpts ] ], | ||
|
||
/* sourcemaps */ | ||
sourceMaps: !!file.sourceMap, | ||
inputSourceMap, | ||
|
||
shouldPrintComment(contents) { | ||
return shouldPrintComment(contents, comments); | ||
}, | ||
|
||
/* file */ | ||
filename: file.path, | ||
filenameRelative: file.relative, | ||
}; | ||
|
||
const { result, success, error } = transform({ | ||
babel, | ||
input: file.contents.toString(), | ||
babelOpts | ||
}); | ||
|
||
if (success) { | ||
file.contents = new Buffer(result.code); | ||
if (file.sourceMap) { | ||
applySourceMap(file, result.map); | ||
} | ||
return callback(null, file); | ||
} | ||
|
||
callback(error); | ||
}); | ||
} | ||
|
||
function transform({ babel, input, babelOpts }) { | ||
try { | ||
return { | ||
success: true, | ||
result: babel.transform(input, babelOpts) | ||
}; | ||
} catch (e) { | ||
return { | ||
success: false, | ||
error: e | ||
}; | ||
} | ||
} | ||
|
||
function shouldPrintComment(contents, predicate) { | ||
switch (typeof predicate) { | ||
case "function": return predicate(contents); | ||
case "object": return predicate.test(contents); | ||
default: return !!predicate; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test is disabled as it removes all comments by default.