<p dir="auto">I have a function that takes a complicated options object as its only argument.<br>
Also, I am using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Default_values_2" rel="nofollow">object destructuring with default values</a> to set default values, which are actually callbacks with similar signatures.</p>
<p dir="auto">The parameters of the callbacks should be independent of each other. However, babel 6.26.0 is still giving a "Duplicate declaration" error.</p>
<h3 dir="auto">Input Code</h3>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="export function createConnectionType({
  resolveTotalCount = ({ first, ...searchContext }) =&gt; {},
  resolveEdges = ({ first, ...searchContext }) =&gt; {},
}) {}"><pre class="notranslate"><span class="pl-k">export</span> <span class="pl-k">function</span> <span class="pl-en">createConnectionType</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  resolveTotalCount <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">{</span> first<span class="pl-kos">,</span> ...<span class="pl-s1">searchContext</span> <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span>
  resolveEdges <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">{</span> first<span class="pl-kos">,</span> ...<span class="pl-s1">searchContext</span> <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</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">REPL: <a href="http://bit.ly/2CVCWuB" rel="nofollow">http://bit.ly/2CVCWuB</a></p>
<h3 dir="auto">Babel/Babylon Configuration (.babelrc, package.json, cli command)</h3>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# .babelrc
{
  &quot;presets&quot;: [
    [&quot;env&quot;, {
      &quot;targets&quot;: {
        &quot;node&quot;: &quot;current&quot;
      }
    }],
    &quot;stage-3&quot;
  ]
}"><pre class="notranslate"># <span class="pl-kos">.</span><span class="pl-s1">babelrc</span>
<span class="pl-kos">{</span>
  <span class="pl-s">"presets"</span>: <span class="pl-kos">[</span>
    <span class="pl-kos">[</span><span class="pl-s">"env"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
      <span class="pl-s">"targets"</span>: <span class="pl-kos">{</span>
        <span class="pl-s">"node"</span>: <span class="pl-s">"current"</span>
      <span class="pl-kos">}</span>
    <span class="pl-kos">}</span><span class="pl-kos">]</span><span class="pl-kos">,</span>
    <span class="pl-s">"stage-3"</span>
  <span class="pl-kos">]</span>
<span class="pl-kos">}</span></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Versions
$ node_modules/.bin/babel --version
6.26.0 (babel-core 6.26.0)"><pre class="notranslate"><code class="notranslate"># Versions
$ node_modules/.bin/babel --version
6.26.0 (babel-core 6.26.0)
</code></pre></div>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">It should compile to something like:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="export function createConnectionType({
  resolveTotalCount,
  resolveEdges,
}) {
  if(!resolveTotalCount) resolveTotalCount = (_ref) =&gt; {const {first} = _ref; const searchContext = _objectWithoutProperties(_ref)};
  if(!resolveEdges) resolveEdges = (_ref) =&gt; {const {first} = _ref; const searchContext = _objectWithoutProperties(_ref)};
}"><pre class="notranslate"><span class="pl-k">export</span> <span class="pl-k">function</span> <span class="pl-en">createConnectionType</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  resolveTotalCount<span class="pl-kos">,</span>
  resolveEdges<span class="pl-kos">,</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">if</span><span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">resolveTotalCount</span><span class="pl-kos">)</span> <span class="pl-en">resolveTotalCount</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">_ref</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-k">const</span> <span class="pl-kos">{</span>first<span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-s1">_ref</span><span class="pl-kos">;</span> <span class="pl-k">const</span> <span class="pl-s1">searchContext</span> <span class="pl-c1">=</span> <span class="pl-en">_objectWithoutProperties</span><span class="pl-kos">(</span><span class="pl-s1">_ref</span><span class="pl-kos">)</span><span class="pl-kos">}</span><span class="pl-kos">;</span>
  <span class="pl-k">if</span><span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">resolveEdges</span><span class="pl-kos">)</span> <span class="pl-en">resolveEdges</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">_ref</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span><span class="pl-k">const</span> <span class="pl-kos">{</span>first<span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-s1">_ref</span><span class="pl-kos">;</span> <span class="pl-k">const</span> <span class="pl-s1">searchContext</span> <span class="pl-c1">=</span> <span class="pl-en">_objectWithoutProperties</span><span class="pl-kos">(</span><span class="pl-s1">_ref</span><span class="pl-kos">)</span><span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto">Current Behavior</h3>
<p dir="auto">When babel-preset-env is enabled, it gives compile error</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="repl: Duplicate declaration &quot;searchContext&quot;
  1 | export function createConnectionType({
  2 |   resolveTotalCount = ({ first, ...searchContext }) =&gt; {},
&gt; 3 |   resolveEdges = ({ first, ...searchContext }) =&gt; {},
    |                               ^
  4 | }) {}"><pre class="notranslate"><code class="notranslate">repl: Duplicate declaration "searchContext"
  1 | export function createConnectionType({
  2 |   resolveTotalCount = ({ first, ...searchContext }) =&gt; {},
&gt; 3 |   resolveEdges = ({ first, ...searchContext }) =&gt; {},
    |                               ^
  4 | }) {}
</code></pre></div>
<p dir="auto">When babel-preset-env is disabled, it gives:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="/* (_objectWithoutProperties omitted) */
export function createConnectionType(_ref) {
  let {
    resolveTotalCount = ({ first }) =&gt; {},
    resolveEdges = ({ first }) =&gt; {}
  } = _ref,
      searchContext = _objectWithoutProperties(_ref.resolveTotalCount, [&quot;first&quot;]),
      searchContext = _objectWithoutProperties(_ref.resolveEdges, [&quot;first&quot;]);
}"><pre class="notranslate"><code class="notranslate">/* (_objectWithoutProperties omitted) */
export function createConnectionType(_ref) {
  let {
    resolveTotalCount = ({ first }) =&gt; {},
    resolveEdges = ({ first }) =&gt; {}
  } = _ref,
      searchContext = _objectWithoutProperties(_ref.resolveTotalCount, ["first"]),
      searchContext = _objectWithoutProperties(_ref.resolveEdges, ["first"]);
}
</code></pre></div>
<p dir="auto">Note that searchContext in the two default values are totally messed up.</p>
<p dir="auto">Seems that there are something wrong with object rest destructuring on function arguments.</p>
<h3 dir="auto">Context</h3>
<p dir="auto">This is the full code:<br>
<a href="https://github.com/cofacts/rumors-api/tree/914b16ea0bc9274f7b16c6a09669f3a2627fe030/src/graphql/util.js#L146-L181">https://github.com/cofacts/rumors-api/tree/914b16ea0bc9274f7b16c6a09669f3a2627fe030/src/graphql/util.js#L146-L181</a></p>
<p dir="auto">The repository was using Babel-core 6.18 and it successfully compiles.</p>
<h3 dir="auto">Your Environment</h3>
<table role="table">
<thead>
<tr>
<th>software</th>
<th>version(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Babel</td>
<td>6.26.0</td>
</tr>
<tr>
<td>node</td>
<td>8.9.1</td>
</tr>
<tr>
<td>npm</td>
<td>5.5.1</td>
</tr>
<tr>
<td>Operating System</td>
<td>Mac OSX 10.11</td>
</tr>
</tbody>
</table>
<blockquote>
<p dir="auto">Issue originally made by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/branneman/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/branneman">@branneman</a></p>
</blockquote>
<h3 dir="auto">Bug information</h3>
<ul dir="auto">
<li><strong>Babel version:</strong> 6</li>
<li><strong>Node version:</strong> any</li>
<li><strong>npm version:</strong> any</li>
</ul>
<h3 dir="auto">Input code</h3>
<p dir="auto">My <code class="notranslate">.babelrc</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;presets&quot;: [&quot;es2015&quot;, &quot;stage-3&quot;],
    &quot;plugins&quot;: [
      &quot;external-helpers-2&quot;,
      &quot;transform-es2015-modules-amd&quot;,
      &quot;transform-class-properties&quot;
    ]
}"><pre class="notranslate"><code class="notranslate">{
    "presets": ["es2015", "stage-3"],
    "plugins": [
      "external-helpers-2",
      "transform-es2015-modules-amd",
      "transform-class-properties"
    ]
}
</code></pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">During my search for generating and serving as few bytes as possible in the name of performance, I've struck a wall. I am using the stage-3 and es2015 presets, and thus I need the Polyfill and Helpers.</p>
<p dir="auto">My question is basically this:</p>
<blockquote>
<p dir="auto"><strong>Is it possible to generate the polyfill and external-helpers custom for only the features I actually use?</strong></p>
</blockquote>
<p dir="auto"><strong>1. Polyfill</strong>:<br>
The polyfill is almost 90kb, and seems to include Promises, Iterators, Generators, Reflect, Map/Set and the entire ES2015 standard library. Most of it, I don't actually need. What if I'm only using Iterators and Generators? I'd like to shave off every possible byte from that 90kb. It seems the polyfill is an all or nothing situation.<br>
I could achieve this manually of course, by just bundling only the parts of core-js I need, but then I'd need to maintain a list of which exact ES features I use, I'd like to avoid that if possible.</p>
<p dir="auto"><strong>2. External Helpers</strong>:<br>
Since I don't want my external-helpers to be duplicate, I went looking for a way to generate them. The documentation seems sparse, but I found this:<br>
<code class="notranslate">var code = require('./node_modules/babel-core/lib/tools/build-external-helpers')();</code><br>
This gives me //all// the helpers that exist in babel, instead of just the ones I need, the same situation as with the polyfill. Internally, Babel probably knows which features I'm using, because it generates code like <code class="notranslate">babelHelpers.createClass</code>.</p>