I'm using Babel to transpile ES6 to ES5 and it works great. The only problem is that once transpiled each module duplicates these lines:
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"); } };
As my application is going to have more and more modules defining classes, I cannot imagine waisting so much space just because of these repeated code.
Is there a way to tell Babel to not include them in each module so we can add them only once later ?
I'm using gulp and my config is:
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'));
});
Thanks a lot for your help!
Yes, as it is described in the documentation:
Babel uses very small helpers for common functions such as
_extend
. By default this will be added to every file that requires it. This duplication is sometimes unnecessary, especially when your application is spread out over multiple files.This is where the runtime optional transformer comes in. All of the helpers will reference the module
babel-runtime
to avoid duplication across your compiled output.Usage
require("babel").transform("code", { optional: ["runtime"] });
$ babel --optional runtime script.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With