Javascript 将Babel外部助手与ES6模块一起使用时出现问题-Babel重新排序导入/导出会中断执行顺序
我在尝试将Babel的外部助手用于ES6模块时遇到了一个问题,我想知道我的方法是否正确。到目前为止,我无法运行传输的代码,如下所述,但是tl;dr版本是Babel将我的Javascript 将Babel外部助手与ES6模块一起使用时出现问题-Babel重新排序导入/导出会中断执行顺序,javascript,babeljs,es6-modules,Javascript,Babeljs,Es6 Modules,我在尝试将Babel的外部助手用于ES6模块时遇到了一个问题,我想知道我的方法是否正确。到目前为止,我无法运行传输的代码,如下所述,但是tl;dr版本是Babel将我的imports和exports重新排序为一个破坏代码的顺序,我不知道它为什么这样做 下面是一个非常简单的示例,演示了这个问题: source/index.js // import babel helpers first so they are added to the global scope // and available
import
s和export
s重新排序为一个破坏代码的顺序,我不知道它为什么这样做
下面是一个非常简单的示例,演示了这个问题:
source/index.js
// import babel helpers first so they are added to the global scope
// and available to anything that needs them
import './babel-helpers';
export * from './dependency';
const constant = 'myProp';
const a = { [constant]: 1 };
const b = { [constant]: 2 };
export { a, b };
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dependency = require('./dependency');
Object.keys(_dependency).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _dependency[key];
}
});
});
require('./babel-helpers');
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var constant = 'myProp';
var a = babelHelpers.defineProperty({}, constant, 1);
var b = babelHelpers.defineProperty({}, constant, 2);
exports.a = a;
exports.b = b;
source/dependency.js
// import babel helpers first so they are added to the global scope
// and available to anything that needs them
import './babel-helpers';
export * from './dependency';
const constant = 'myProp';
const a = { [constant]: 1 };
const b = { [constant]: 2 };
export { a, b };
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dependency = require('./dependency');
Object.keys(_dependency).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _dependency[key];
}
});
});
require('./babel-helpers');
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var constant = 'myProp';
var a = babelHelpers.defineProperty({}, constant, 1);
var b = babelHelpers.defineProperty({}, constant, 2);
exports.a = a;
exports.b = b;
source/babel helpers.js(使用babel外部助手-l defineProperty自动生成)
.babelrc
{
"plugins": ["external-helpers"],
"presets": ["env"]
}
使用babel source--output dir builded
传输这些数据后,输出为:
build/index.js
// import babel helpers first so they are added to the global scope
// and available to anything that needs them
import './babel-helpers';
export * from './dependency';
const constant = 'myProp';
const a = { [constant]: 1 };
const b = { [constant]: 2 };
export { a, b };
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dependency = require('./dependency');
Object.keys(_dependency).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _dependency[key];
}
});
});
require('./babel-helpers');
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var constant = 'myProp';
var a = babelHelpers.defineProperty({}, constant, 1);
var b = babelHelpers.defineProperty({}, constant, 2);
exports.a = a;
exports.b = b;
builded/dependency.js
// import babel helpers first so they are added to the global scope
// and available to anything that needs them
import './babel-helpers';
export * from './dependency';
const constant = 'myProp';
const a = { [constant]: 1 };
const b = { [constant]: 2 };
export { a, b };
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dependency = require('./dependency');
Object.keys(_dependency).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _dependency[key];
}
});
});
require('./babel-helpers');
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var constant = 'myProp';
var a = babelHelpers.defineProperty({}, constant, 1);
var b = babelHelpers.defineProperty({}, constant, 2);
exports.a = a;
exports.b = b;
builded/babel helpers.js与原始版本相比没有任何意义的更改
我遇到的问题是,出于某种原因,传输的import./babel helpers'
被移动到index.js的末尾,因此当我尝试运行代码时,它会失败并出现错误,这是可以理解的,因为使用helpers的代码在添加到全局作用域之前运行:
var a = babelHelpers.defineProperty({}, constant, 1);
^
ReferenceError: babelHelpers is not defined
如果我手动编辑transpiled index.js以移动require('./babel helpers')在文件的顶部,代码运行正常
所以我有两个问题:
为什么巴贝尔要对import
s和export
s进行这种奇怪的重新排序?这似乎完全是无稽之谈。有办法控制它吗
我走错方向了吗?是否有更好的方式使用外部助手?该文档没有提供有关如何实际使用生成的帮助程序的指导。将它们生成到一个模块中,并显式地将该模块导入到最终需要它们的每个文件中,这可能会起作用,但这似乎是一个非常棘手的问题。当然,我的单个模块最好不必在预传输的源代码中显式引用它们
我已经为这个场景创建了一个Github回购协议,以防有人想自己运行它:事实证明,导入和导出的重新排序是一个错误,在Babel的7.x测试版中得到了修复:
我结束了我自己关于这个问题的存档,一位巴别塔团队成员回答说这是一个已知(已修复)的问题。他还提供了一些关于使用外部助手的建议,因为在编写库时,在全球范围内导入它们并不是一个好主意:
我认为它不会将babel helpers
移到最后,而是将export*从移到顶部。@Bergi在我的例子中有点像“一个六个,另一个六个”,因为我的实际文件只有导入
和导出
,但你是对的-看起来问题是导出。。。从
s向上移动到顶部。