<blockquote>
<p dir="auto">Issue originally made by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yangchenyun/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yangchenyun">@yangchenyun</a></p>
</blockquote>
<h3 dir="auto">Bug information</h3>
<ul dir="auto">
<li><strong>Babel version:</strong> 6.3.17 (babel-core 6.3.21)</li>
<li><strong>Node version:</strong> 5.3.0</li>
<li><strong>npm version:</strong> 3.3.12</li>
</ul>
<h3 dir="auto">Options</h3>
<p dir="auto">$ cat .babelrc</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;presets&quot;: [&quot;es2015&quot;]
}"><pre class="notranslate"><code class="notranslate">{
  "presets": ["es2015"]
}
</code></pre></div>
<p dir="auto">$ cat &gt; script.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="function f2(x, ...y) {
  // y is an Array
  return x * y.length;
}"><pre class="notranslate"><code class="notranslate">function f2(x, ...y) {
  // y is an Array
  return x * y.length;
}
</code></pre></div>
<p dir="auto">$ babel script.js --out-file script-compiled.js &amp;&amp; cat script-compiled.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&quot;use strict&quot;;

function f2(x) {
  // y is an Array
  return x * arguments.length + 1;
};"><pre class="notranslate"><code class="notranslate">"use strict";

function f2(x) {
  // y is an Array
  return x * arguments.length + 1;
};
</code></pre></div>
<h3 dir="auto">Input code</h3>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  &quot;presets&quot;: [&quot;es2015&quot;]
}"><pre class="notranslate"><span class="pl-kos">{</span>
  <span class="pl-s">"presets"</span>: <span class="pl-kos">[</span><span class="pl-s">"es2015"</span><span class="pl-kos">]</span>
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">The compiled code seems to be incorrect optimization when the value of y is not needed; but the correct optimization should be:</p>
<p dir="auto">return x * (arguments.length - 1)</p>
<p dir="auto">Let's say we have the following piece of code:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="export async function beforeBluebirdRequire() {
    await getMeSomePromise();
}

export default async function afterBluebirdRequire() {
    await getMeSomePromise();
}"><pre class="notranslate"><span class="pl-k">export</span> <span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">await</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">afterBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">await</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">To deal with the async/wait we transpile this with a <code class="notranslate">.babelrc</code> as follows:</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;plugins&quot;: [
        [&quot;transform-async-to-module-method&quot;, {
            &quot;module&quot;: &quot;bluebird&quot;,
            &quot;method&quot;: &quot;coroutine&quot;
        }]
    ]
}"><pre class="notranslate">{
    <span class="pl-ent">"plugins"</span>: [
        [<span class="pl-s"><span class="pl-pds">"</span>transform-async-to-module-method<span class="pl-pds">"</span></span>, {
            <span class="pl-ent">"module"</span>: <span class="pl-s"><span class="pl-pds">"</span>bluebird<span class="pl-pds">"</span></span>,
            <span class="pl-ent">"method"</span>: <span class="pl-s"><span class="pl-pds">"</span>coroutine<span class="pl-pds">"</span></span>
        }]
    ]
}</pre></div>
<p dir="auto">This results in the following code:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import { coroutine as _coroutine } from &quot;bluebird&quot;;
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);
    };
})();"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">coroutine</span> <span class="pl-k">as</span> <span class="pl-s1">_coroutine</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"bluebird"</span><span class="pl-kos">;</span>
<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-en">_coroutine</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">afterBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">let</span> <span class="pl-s1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">var</span> <span class="pl-s1">ref</span> <span class="pl-c1">=</span> <span class="pl-en">_coroutine</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-k">return</span> <span class="pl-k">function</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-s1">ref</span><span class="pl-kos">.</span><span class="pl-en">apply</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">,</span> <span class="pl-smi">arguments</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Which will not run on Node because it assumes ES6 modules, so we change <code class="notranslate">.babelrc</code> to:</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;plugins&quot;: [
        [&quot;transform-async-to-module-method&quot;, {
            &quot;module&quot;: &quot;bluebird&quot;,
            &quot;method&quot;: &quot;coroutine&quot;
        }],
        &quot;transform-es2015-modules-commonjs&quot;
    ]
}"><pre class="notranslate">{
    <span class="pl-ent">"plugins"</span>: [
        [<span class="pl-s"><span class="pl-pds">"</span>transform-async-to-module-method<span class="pl-pds">"</span></span>, {
            <span class="pl-ent">"module"</span>: <span class="pl-s"><span class="pl-pds">"</span>bluebird<span class="pl-pds">"</span></span>,
            <span class="pl-ent">"method"</span>: <span class="pl-s"><span class="pl-pds">"</span>coroutine<span class="pl-pds">"</span></span>
        }],
        <span class="pl-s"><span class="pl-pds">"</span>transform-es2015-modules-commonjs<span class="pl-pds">"</span></span>
    ]
}</pre></div>
<p dir="auto">But now the transpiled code ends up as:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&quot;use strict&quot;;

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, &quot;__esModule&quot;, {
    value: true
});
exports.beforeBluebirdRequire = undefined;

var _bluebird = require(&quot;bluebird&quot;);

exports.default = (0, _bluebird.coroutine)(function* afterBluebirdRequire() {
    yield getMeSomePromise();
});"><pre class="notranslate"><span class="pl-s">"use strict"</span><span class="pl-kos">;</span>

<span class="pl-k">let</span> <span class="pl-s1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">var</span> <span class="pl-s1">ref</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-s1">_bluebird</span><span class="pl-kos">.</span><span class="pl-c1">coroutine</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">return</span> <span class="pl-k">function</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-s1">ref</span><span class="pl-kos">.</span><span class="pl-en">apply</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">,</span> <span class="pl-smi">arguments</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">defineProperty</span><span class="pl-kos">(</span><span class="pl-s1">exports</span><span class="pl-kos">,</span> <span class="pl-s">"__esModule"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">value</span>: <span class="pl-c1">true</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-c1">undefined</span><span class="pl-kos">;</span>

<span class="pl-k">var</span> <span class="pl-s1">_bluebird</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"bluebird"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">default</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-s1">_bluebird</span><span class="pl-kos">.</span><span class="pl-c1">coroutine</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">afterBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">This won't run because <code class="notranslate">_bluebird.coroutine</code> is used before it is required.</p>
<hr>
<p dir="auto">The silly thing is that when you separate the two transpile operations into two steps, it will all work fine and as expected.</p>
<p dir="auto">So first we substitute the async/await using <code class="notranslate">transform-async-to-module-method</code>, and then we take the result as input to another transpilation call that substitutes the ES6 modules for CommonJS modules using <code class="notranslate">transform-es2015-modules-commonjs</code>.</p>
<p dir="auto">The result is exactly what we need:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&quot;use strict&quot;;

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

var _bluebird = require(&quot;bluebird&quot;);

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);
    };
})();"><pre class="notranslate"><span class="pl-s">"use strict"</span><span class="pl-kos">;</span>

<span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">defineProperty</span><span class="pl-kos">(</span><span class="pl-s1">exports</span><span class="pl-kos">,</span> <span class="pl-s">"__esModule"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">value</span>: <span class="pl-c1">true</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-c1">undefined</span><span class="pl-kos">;</span>

<span class="pl-k">var</span> <span class="pl-s1">_bluebird</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"bluebird"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">default</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-s1">_bluebird</span><span class="pl-kos">.</span><span class="pl-c1">coroutine</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">afterBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">let</span> <span class="pl-s1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">beforeBluebirdRequire</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">var</span> <span class="pl-s1">ref</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-s1">_bluebird</span><span class="pl-kos">.</span><span class="pl-c1">coroutine</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">yield</span> <span class="pl-en">getMeSomePromise</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-k">return</span> <span class="pl-k">function</span> <span class="pl-s1">beforeBluebirdRequire</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-s1">ref</span><span class="pl-kos">.</span><span class="pl-en">apply</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">,</span> <span class="pl-smi">arguments</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Why won't we get this result when using the two plugins together, in the same transpile operation?</strong></p>
<p dir="auto">P.S. I'm using Babel 6</p>