UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2
3var babelCore = require("babel-core");
4var through2 = require("through2");
5var gutil = require("gulp-util");
6var applySourceMap = require("vinyl-sourcemaps-apply");
7var babiliPreset = require("babel-preset-babili");
8
9var PluginError = gutil.PluginError;
10
11
12var NAME = "gulp-babili";
13
14module.exports = gulpBabili;
15
16function gulpBabili() {
17 var babiliOpts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
18
19 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
20 _ref$babel = _ref.babel,
21 babel = _ref$babel === undefined ? babelCore : _ref$babel,
22 _ref$babili = _ref.babili,
23 babili = _ref$babili === undefined ? babiliPreset : _ref$babili,
24 _ref$comments = _ref.comments,
25 comments = _ref$comments === undefined ? /preserve|licen(s|c)e/ : _ref$comments;
26
27 return through2.obj(function (file, enc, callback) {
28 if (file.isNull()) {
29 return callback(null, file);
30 }
31
32 if (file.isStream()) {
33 return callback(new PluginError(NAME, "Streaming not supported"));
34 }
35
36 var inputSourceMap = void 0;
37 if (file.sourceMap && file.sourceMap.mappings) {
38 inputSourceMap = file.sourceMap;
39 }
40
41 var babelOpts = {
42 minified: true,
43 babelrc: false,
44 ast: false,
45
46 /* preset */
47 presets: [[babili, babiliOpts]],
48
49 /* sourcemaps */
50 sourceMaps: !!file.sourceMap,
51 inputSourceMap,
52
53 shouldPrintComment(contents) {
54 return shouldPrintComment(contents, comments);
55 },
56
57 /* file */
58 filename: file.path,
59 filenameRelative: file.relative
60 };
61
62 var _transform = transform({
63 babel,
64 input: file.contents.toString(),
65 babelOpts
66 }),
67 result = _transform.result,
68 success = _transform.success,
69 error = _transform.error;
70
71 if (success) {
72 file.contents = new Buffer(result.code);
73 if (file.sourceMap) {
74 applySourceMap(file, result.map);
75 }
76 return callback(null, file);
77 }
78
79 callback(error);
80 });
81}
82
83function transform(_ref2) {
84 var babel = _ref2.babel,
85 input = _ref2.input,
86 babelOpts = _ref2.babelOpts;
87
88 try {
89 return {
90 success: true,
91 result: babel.transform(input, babelOpts)
92 };
93 } catch (e) {
94 return {
95 success: false,
96 error: e
97 };
98 }
99}
100
101function shouldPrintComment(contents, predicate) {
102 switch (typeof predicate) {
103 case "function":
104 return predicate(contents);
105 case "object":
106 return predicate.test(contents);
107 default:
108 return !!predicate;
109 }
110}
\No newline at end of file