ES6 重新导出的值被包装到 Getter 中

Ole*_*man 2 javascript module ecmascript-6 babeljs

有人可以向我解释一下吗?假设我有这两个模块:

// moduleTwo.js

export const module2Func = () => {
  return 2
}
Run Code Online (Sandbox Code Playgroud)

// moduleOne.js

export * from './moduleTwo'

export const module1Func = () => {
  return 1
}
Run Code Online (Sandbox Code Playgroud)

然后我在某处导入moduleOne

import * as final from './moduleOne'

console.log(final) 
Run Code Online (Sandbox Code Playgroud)

然后最终结果是这样的:

{ module2Func: [Getter], module1Func: [Function: module1Func] }
Run Code Online (Sandbox Code Playgroud)

为什么module2Func包裹成 a Getter?这是export from那个语法吗?所有功能均按预期工作。我正在使用 Babel 6 来转译代码。

感谢您的任何解释!

Fel*_*ing 5

Babel 就是这样做来正确实现模块导入的引用绑定的。您可以查看编译后的输出

输入:

export * from 'foo';
Run Code Online (Sandbox Code Playgroud)

输出:

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _foo = require('foo');

Object.keys(_foo).forEach(function (key) {
  if (key === "default") return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _foo[key];
    }
  });
});
Run Code Online (Sandbox Code Playgroud)