1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _buble = require('buble');
|
10 |
|
11 | var _objectAssign = require('object-assign');
|
12 |
|
13 | var _objectAssign2 = _interopRequireDefault(_objectAssign);
|
14 |
|
15 | var _stream = require('stream');
|
16 |
|
17 | var _os = require('os');
|
18 |
|
19 | var _path = require('path');
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
24 |
|
25 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
26 |
|
27 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
28 |
|
29 | var Bubleify = function (_Transform) {
|
30 | _inherits(Bubleify, _Transform);
|
31 |
|
32 | function Bubleify(filename, options) {
|
33 | _classCallCheck(this, Bubleify);
|
34 |
|
35 | var _this = _possibleConstructorReturn(this, (Bubleify.__proto__ || Object.getPrototypeOf(Bubleify)).call(this));
|
36 |
|
37 | _this._data = '';
|
38 | _this._filename = filename;
|
39 | _this._options = options;
|
40 | return _this;
|
41 | }
|
42 |
|
43 | _createClass(Bubleify, [{
|
44 | key: '_transform',
|
45 | value: function _transform(buf, enc, cb) {
|
46 | this._data += buf;
|
47 | cb();
|
48 | }
|
49 | }, {
|
50 | key: '_flush',
|
51 | value: function _flush(cb) {
|
52 | try {
|
53 | var result = (0, _buble.transform)(this._data, this._bubleOptions);
|
54 | var code = result.code;
|
55 |
|
56 |
|
57 | if (this._options.sourceMap) {
|
58 |
|
59 | code += _os.EOL + '//# sourceMappingURL=' + result.map.toUrl();
|
60 | }
|
61 |
|
62 | this.emit('bubleify', result, this._filename);
|
63 | this.push(code);
|
64 | } catch (err) {
|
65 |
|
66 | if (this._options.bubleError && err.snippet) {
|
67 | this.emit('error', '---' + _os.EOL + err.snippet + _os.EOL + _os.EOL + err.message + _os.EOL);
|
68 | } else {
|
69 | this.emit('error', err);
|
70 | }
|
71 | return;
|
72 | }
|
73 | cb();
|
74 | }
|
75 | }, {
|
76 | key: '_bubleOptions',
|
77 | get: function get() {
|
78 | var defaults = { source: this._filename };
|
79 | var options = (0, _objectAssign2.default)(defaults, this._options);
|
80 |
|
81 |
|
82 |
|
83 | options.transforms = (0, _objectAssign2.default)({ modules: false }, this._options.transforms);
|
84 | options.target = (0, _objectAssign2.default)({}, this._options.target);
|
85 |
|
86 |
|
87 | delete options._flags;
|
88 | delete options.sourceMap;
|
89 | delete options.extensions;
|
90 | delete options.bubleError;
|
91 |
|
92 | return options;
|
93 | }
|
94 | }]);
|
95 |
|
96 | return Bubleify;
|
97 | }(_stream.Transform);
|
98 |
|
99 | exports.default = function (filename, options) {
|
100 |
|
101 | var _options$extensions = options.extensions,
|
102 | extensions = _options$extensions === undefined ? ['.js', '.jsx', '.es', '.es6'] : _options$extensions;
|
103 |
|
104 |
|
105 | extensions = Array.isArray(extensions) ? extensions : [extensions];
|
106 |
|
107 | var enrishedOptions = (0, _objectAssign2.default)({
|
108 | sourceMap: true,
|
109 | bubleError: false
|
110 | }, options, { extensions: extensions });
|
111 |
|
112 | var shouldIgnoreFile = extensions.indexOf((0, _path.extname)(filename)) === -1;
|
113 |
|
114 | if (shouldIgnoreFile) {
|
115 |
|
116 | return (0, _stream.PassThrough)();
|
117 | }
|
118 |
|
119 | return new Bubleify(filename, enrishedOptions);
|
120 | };
|
121 |
|
\ | No newline at end of file |