Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active September 7, 2016 17:24
Show Gist options
  • Save ldong/300f8422d3f4843435fa to your computer and use it in GitHub Desktop.
Save ldong/300f8422d3f4843435fa to your computer and use it in GitHub Desktop.
Babel no longer transform `export default` to `module.exports`

Babel 6 no longer transform export default like babel 5

Note

From var a = require('./a'); to var a = require('./a').default;

See [Repo](for demo)

Babel 5

// ES6
export default class MyClass extends Component {

}
// ES5
"use strict";

exports.__esModule = true;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var MyClass = (function (_Component) {
  _inherits(MyClass, _Component);

  function MyClass() {
    _classCallCheck(this, MyClass);

    _Component.apply(this, arguments);
  }

  return MyClass;
})(Component);

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

Babel 6

Babel 6 with presets": ["es2015"] enabled, see repo to reproduce this

// ES6
export default class MyClass extends Component {

}
// ES5
"use strict";

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var MyClass = function (_Component) {
  _inherits(MyClass, _Component);

  function MyClass() {
    _classCallCheck(this, MyClass);

    return _possibleConstructorReturn(this, Object.getPrototypeOf(MyClass).apply(this, arguments));
  }

  return MyClass;
}(Component);

exports.default = MyClass;

Reference

  1. http://www.2ality.com/2014/09/es6-modules-final.html
  2. http://www.2ality.com/2015/12/babel-commonjs.html
  3. http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default
  4. http://www.jbrantly.com/es6-modules-with-typescript-and-webpack/
  5. http://www.sitepoint.com/transpiling-es6-modules-to-amd-commonjs-using-babel-gulp/
  6. https://medium.com/@kentcdodds/misunderstanding-es6-modules-upgrading-babel-tears-and-a-solution-ad2d5ab93ce0#.2q4yeggc9
  7. microsoft/TypeScript#5458
  8. facebook/react-native#4784
@ldong
Copy link
Author

ldong commented Jan 22, 2016

Babel 6 with presets": ["es2015"] enabled, see repo to reproduce this

// ES6
export default class MyClass extends Component {

}
// ES5
"use strict";

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var MyClass = function (_Component) {
  _inherits(MyClass, _Component);

  function MyClass() {
    _classCallCheck(this, MyClass);

    return _possibleConstructorReturn(this, Object.getPrototypeOf(MyClass).apply(this, arguments));
  }

  return MyClass;
}(Component);

exports.default = MyClass;

@ldong
Copy link
Author

ldong commented Jan 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment