Is ES6 `export class A` equivalent to `module.exports = A`?

前端 未结 3 1190
终归单人心
终归单人心 2020-12-28 16:42

When I see the compiled code by Babel, they do not seem to be equivalent. Actually, the former transforms to exports.A = A, which is not equivalent to mod

3条回答
  •  渐次进展
    2020-12-28 17:28

    You can use

    export default class A {
    
    }
    

    Or

    class A {
    
    }
    
    export default A;
    

    Which will export as

    exports["default"] = A;
    module.exports = exports["default"];
    

    There's an explanation why in the interop section here.

    In order to encourage the use of CommonJS and ES6 modules, when exporting a default export with no other exports module.exports will be set in addition to exports["default"].

提交回复
热议问题