How to correctly use ES6 “export default” with CommonJS “require”?

て烟熏妆下的殇ゞ 提交于 2019-11-27 13:56:09

To use export default with Babel, you can do 1 of the following:

  1. require("myStuff").default
  2. npm install babel-plugin-add-module-exports --save-dev

Or 3:

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

If someone using the gulp + browserify + babelify to bundle js using in client-end.

Try following code [gulpfile.js]:

browserify({
  entries: "./ui/qiyun-ui/javascripts/qiyun-ui.src.js",
  standalone: "qyUI" // To UMD
})
.transform(babelify, {
  presets: ["env"],
  plugins: ["add-module-exports"] // export default {} => module.exports = exports['default'];
})
.bundle()

Don't forget to install this package: https://www.npmjs.com/package/babel-plugin-add-module-exports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!