babel/6to5 - 导出模块时重复的代码
babel/6to5 - duplicated code when exporting modules
我正在使用 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"); } };
由于我的应用程序将有越来越多的模块定义 类,我无法想象仅仅因为这些重复的代码就浪费了这么多 space。
有没有办法告诉 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'));
});
非常感谢您的帮助!
是的,如 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
我正在使用 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"); } };
由于我的应用程序将有越来越多的模块定义 类,我无法想象仅仅因为这些重复的代码就浪费了这么多 space。
有没有办法告诉 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'));
});
非常感谢您的帮助!
是的,如 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