Babel 6改变了如何导出默认值 [英] Babel 6 changes how it exports default

查看:89
本文介绍了Babel 6改变了如何导出默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,babel会添加 module.exports = exports [default] 。它不再这样做了。这是什么意思,我可以做:

Before, babel would add the line module.exports = exports["default"]. It no longer does this. What this means is before I could do:

var foo = require('./foo');
// use foo

现在我必须这样做:

var foo = require('./foo').default;
// use foo

不是一个巨大的交易(我猜这是什么一直以来)。
问题是我有很多代码依赖于以前工作的方式(我可以将其大部分转换为ES6导入,但不是全部)。任何人都可以给我提示如何使旧的方式工作,而无需通过我的项目,并解决这个问题(甚至一些关于如何编写代码来做这样做的指导将是非常光滑的)。

Not a huge deal (and I'm guessing this is what it should have been all along). The issue is that I have a lot of code that depended on the way that things used to work (I can convert most of it to ES6 imports, but not all of it). Can anyone give me tips on how to make the old way work without having to go through my project and fix this (or even some instruction on how to write a codemod to do this would be pretty slick).

谢谢!

示例:

输入:

const foo = {}
export default foo

输出与Babel 5

Output with Babel 5

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var foo = {};
exports["default"] = foo;
module.exports = exports["default"];

输出与Babel 6(和es2015插件):

Output with Babel 6 (and es2015 plugin):

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var foo = {};
exports["default"] = foo;

请注意,输出的唯一区别是 module.exports = exports [default]

Notice that the only difference in the output is the module.exports = exports["default"].

您可能对我在解决我的具体问题后写的这篇博文感兴趣:误解ES6模块,升级宝贝,眼泪和解决方案

You may be interested in this blogpost I wrote after solving my specific issue: Misunderstanding ES6 Modules, Upgrading Babel, Tears, and a Solution

推荐答案

还可以使用此插件获取旧的导出行为回来。

You can also use this plugin to get the old export behavior back.

这篇关于Babel 6改变了如何导出默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆