我正在使用 Babel 将 ES6 转换为 ES5,效果很好。唯一的问题是,一旦编译,每个模块都会重复这些行:
var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _inherits = function (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) subClass.__proto__ = superClass; };
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
由于我的应用程序将有越来越多的模块定义类,我无法想象仅仅因为这些重复的代码而占用如此多的空间。
有没有办法告诉 Babel 不要将它们包含在每个模块中,以便我们以后只能添加一次?
我正在使用 gulp,我的配置是:
var gulp = require('gulp');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
gulp.task('default', function () {
return gulp.src('src/**/*.es6')
.pipe(babel({ playground: true, experimental: true, modules: 'amd', moduleIds: true }))
.pipe(concat('tmp/src.js'))
.pipe(gulp.dest('dist'));
});
非常感谢你的帮助!