Skip to content

Use amd for transpiling modules with babel@5. - #6827

Merged
homu merged 2 commits into
betafrom
forward-compat-babel-6
Mar 5, 2017
Merged

homu merged 2 commits into
betafrom
forward-compat-babel-6

Conversation

@rwjblue

@rwjblue rwjblue commented Mar 5, 2017

Copy link
Copy Markdown
Member

In order to allow gradual migration from babel@5 to babel@6 we must be able update addons on a case by case basis to ember-cli-babel@6. This means that at any given time we will have both babel@5 and babel@6 transpiled modules present in the system. Unfortunately, our prior default module transpilation target (amdStrict) is not compatible with babel@6 transpiled modules. The issue is best illustrated with an example.

Given the following input content:

import defaultThingy from './a';
import { namedThingy } from './b';

export default function() {
  defaultThingy();
}

export function namedExport() {
  namedThingy();
}

Babel 5 with amdStrict will emit:

define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  exports.namedExport = namedExport;

  exports['default'] = function () {
    (0, _a['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});

And Babel 6 with transform-es2015-modules-amd will emit:

define('some/file/here', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

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

  exports.default = function () {
    (0, _a2.default)();
  };

  exports.namedExport = namedExport;

  var _a2 = _interopRequireDefault(_a);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  function namedExport() {
    (0, _b.namedThingy)();
  }
});

The issue is not immediately obvious, but the babel@5 module does not define __esModule on the exports object which means that when the babel@6 module attempts to import the default export from the babel@5 module it will result in getting the raw exports object (not exports.default).

Switching our default transpilation while still using babel@5 from amdStrict to amd will emit interoperable modules:

define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  Object.defineProperty(exports, '__esModule', {
    value: true
  });
  exports.namedExport = namedExport;

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

  var _defaultThingy = _interopRequireDefault(_a);

  exports['default'] = function () {
    (0, _defaultThingy['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});

In order to allow gradual migration from babel@5 to babel@6 we
must be able update addons on a case by case basis to
ember-cli-babel@6. This means that at any given time we will
have both babel@5 and babel@6 transpiled modules present in the
system. Unfortunately, our prior default module transpilation
target (`amdStrict`) is not compatible with babel@6 transpiled
modules. The issue is best illustrated with an example.

Given the following input content:

```js
import defaultThingy from './a';
import { namedThingy } from './b';

export default function() {
  defaultThingy();
}

export function namedExport() {
  namedThingy();
}
```

Babel 5 with `amdStrict` will emit:

```js
define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  exports.namedExport = namedExport;

  exports['default'] = function () {
    (0, _a['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});
```

And Babel 6 with `transform-es2015-modules-amd` will emit:

```js
define('some/file/here', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

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

  exports.default = function () {
    (0, _a2.default)();
  };

  exports.namedExport = namedExport;

  var _a2 = _interopRequireDefault(_a);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  function namedExport() {
    (0, _b.namedThingy)();
  }
});

```

The issue is not immediately obvious, but the babel@5 module
does not define `__esModule` on the `exports` object which means
that when the babel@6 module attempts to import the default
export from the babel@5 module it will result in getting the raw
`exports` object (not `exports.default`).

Switching our default transpilation while still using babel@5 from
`amdStrict` to `amd` will emit interoperable modules:

```js
define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  Object.defineProperty(exports, '__esModule', {
    value: true
  });
  exports.namedExport = namedExport;

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

  var _defaultThingy = _interopRequireDefault(_a);

  exports['default'] = function () {
    (0, _defaultThingy['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});
```
@rwjblue
rwjblue requested review from Turbo87 and stefanpenner March 5, 2017 14:56
@rwjblue

rwjblue commented Mar 5, 2017

Copy link
Copy Markdown
Member Author

Note: this targets beta branch ([email protected]) in order to ease babel@6 transition work in [email protected].

@stefanpenner stefanpenner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good.

Originally amdStrict was added for compat with browsers without support for ES5's Object.defineProperty, I do not believe we support those any-longer anyways?

I suppose amdStrict also shipped less bytes, but I suspect that is ok as there are other important benefits to babel 6 that trump this. Even just in the module system.

--

I am also fine with this riding the beta train, as long as we get some users testing this before it hits stable we should be good.

@Turbo87

Turbo87 commented Mar 5, 2017

Copy link
Copy Markdown
Member

I'm a little worried about putting this on the beta branch given that we planned to release it on Monday. If we do it we should at least add another week to the beta cycle.

Is there no way to configure the Babel 6 plugin to skip the interop part? /cc @hzoo

@rwjblue

rwjblue commented Mar 5, 2017

Copy link
Copy Markdown
Member Author

I'm a little worried about putting this on the beta branch given that we planned to release it on Monday. If we do it we should at least add another week to the beta cycle.

I'm happy to push a week (I think ember will need to delay a week anyways). I'm also fine if we decide to only land on master (for 2.13) so this gets a full beta cycle.

@rwjblue

rwjblue commented Mar 5, 2017

Copy link
Copy Markdown
Member Author

FWIW, the tests are failing due to the bug fixed in ember-cli/loader.js#113.

Addresses issue with babel@6 and babel@5 interop.
@rwjblue

rwjblue commented Mar 5, 2017

Copy link
Copy Markdown
Member Author

Landing into beta with the understanding that we will follow @Turbo87's advice and ship another beta today/tomorrow and delay 2.12.0 final until next week.

@homu r+

@homu

homu commented Mar 5, 2017

Copy link
Copy Markdown
Contributor

📌 Commit 0429795 has been approved by rwjblue

@homu
homu merged commit 0429795 into beta Mar 5, 2017
homu added a commit that referenced this pull request Mar 5, 2017
Use `amd` for transpiling modules with babel@5.

In order to allow gradual migration from babel@5 to babel@6 we must be able update addons on a case by case basis to ember-cli-babel@6. This means that at any given time we will have both babel@5 and babel@6 transpiled modules present in the system. Unfortunately, our prior default module transpilation target (`amdStrict`) is not compatible with babel@6 transpiled modules. The issue is best illustrated with an example.

Given the following input content:

```js
import defaultThingy from './a';
import { namedThingy } from './b';

export default function() {
  defaultThingy();
}

export function namedExport() {
  namedThingy();
}
```

Babel 5 with `amdStrict` will emit:

```js
define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  exports.namedExport = namedExport;

  exports['default'] = function () {
    (0, _a['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});
```

And Babel 6 with `transform-es2015-modules-amd` will emit:

```js
define('some/file/here', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

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

  exports.default = function () {
    (0, _a2.default)();
  };

  exports.namedExport = namedExport;

  var _a2 = _interopRequireDefault(_a);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  function namedExport() {
    (0, _b.namedThingy)();
  }
});
```

The issue is not immediately obvious, but the babel@5 module does not define `__esModule` on the `exports` object which means that when the babel@6 module attempts to import the default export from the babel@5 module it will result in getting the raw `exports` object (not `exports.default`).

Switching our default transpilation while still using babel@5 from `amdStrict` to `amd` will emit interoperable modules:

```js
define('some/file/name', ['exports', './a', './b'], function (exports, _a, _b) {
  'use strict';

  Object.defineProperty(exports, '__esModule', {
    value: true
  });
  exports.namedExport = namedExport;

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

  var _defaultThingy = _interopRequireDefault(_a);

  exports['default'] = function () {
    (0, _defaultThingy['default'])();
  };

  function namedExport() {
    (0, _b.namedThingy)();
  }
});
```
@homu

homu commented Mar 5, 2017

Copy link
Copy Markdown
Contributor

⚡ Test exempted - status

@rwjblue
rwjblue deleted the forward-compat-babel-6 branch March 5, 2017 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants