opened
timmolendijk
username_0
Let's say we have the following piece of code:

```js
export async function beforeBluebirdRequire() {
	await getMeSomePromise();
}

export default async function afterBluebirdRequire() {
	await getMeSomePromise();
}
```

To deal with the async/wait we transpile this with a `.babelrc` as follows:

```json
{
	"plugins": [
		["transform-async-to-module-method", {
			"module": "bluebird",
			"method": "coroutine"
		}]
	]
}
```

This results in the following code:

```js
import { coroutine as _coroutine } from "bluebird";
export default _coroutine(function* afterBluebirdRequire() {
	yield getMeSomePromise();
});

export let beforeBluebirdRequire = (function () {
	var ref = _coroutine(function* beforeBluebirdRequire() {
		yield getMeSomePromise();
	});

	return function beforeBluebirdRequire() {
		return ref.apply(this, arguments);
	};
})();
```

Which will not run on Node because it assumes ES6 modules, so we change `.babelrc` to:

```json
{
	"plugins": [
		["transform-async-to-module-method", {
			"module": "bluebird",
			"method": "coroutine"
		}],
		"transform-es2015-modules-commonjs"
	]
}
```

But now the transpiled code ends up as:

```js
"use strict";

let beforeBluebirdRequire = exports.beforeBluebirdRequire = (function () {
	var ref = (0, _bluebird.coroutine)(function* beforeBluebirdRequire() {
		yield getMeSomePromise();
	});
	return function beforeBluebirdRequire() {
		return ref.apply(this, arguments);
	};
})();

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

var _bluebird = require("bluebird");

exports.default = (0, _bluebird.coroutine)(function* afterBluebirdRequire() {
	yield getMeSomePromise();
});
```

This won't run because `_bluebird.coroutine` is used before it is required.

---

The silly thing is that when you separate the two transpile operations into two steps, it will all work fine and as expected.

So first we substitute the async/await using `transform-async-to-module-method`, and then we take the result as input to another transpilation call that substitutes the ES6 modules for CommonJS modules using `transform-es2015-modules-commonjs`.

The result is exactly what we need:

```js
"use strict";

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

var _bluebird = require("bluebird");

exports.default = (0, _bluebird.coroutine)(function* afterBluebirdRequire() {
	yield getMeSomePromise();
});
let beforeBluebirdRequire = exports.beforeBluebirdRequire = (function () {
	var ref = (0, _bluebird.coroutine)(function* beforeBluebirdRequire() {
		yield getMeSomePromise();
	});

	return function beforeBluebirdRequire() {
		return ref.apply(this, arguments);
	};
})();
```

**Why won't we get this result when using the two plugins together, in the same transpile operation?**
`transform-async-to-module-method` entails needing `transform-es2015-modules-commonjs` but the two don't go well together
issue
closed
loganfsmyth
username_1
issue
created
loganfsmyth
username_1
As you mentioned, this is a dup.
comment