Babel 6 更改了默认导出方式 [英] Babel 6 changes how it exports default

查看:12
本文介绍了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 模块、升级 Babel、Tears 和 a解决方案

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

推荐答案

你也可以使用 这个插件恢复旧的export行为.

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

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

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