<h3 dir="auto">Choose one: is this a bug report or feature request?</h3>
<p dir="auto">Bug report.</p>
<h3 dir="auto">Summary</h3>
<p dir="auto">When attempting to import and re-export a TypeScript <code class="notranslate">enum</code> with <code class="notranslate">@babel/preset-env</code> in loose mode, Babel generates code with an undefined export that throws a <code class="notranslate">TypeError</code> at runtime. This issue appeared after upgrading <code class="notranslate">@babel/preset-env</code> from <code class="notranslate">v7.0.0-beta.31</code> to <code class="notranslate">v7.0.0-beta.32</code>.</p>
<p dir="auto">Some sample code is below, but see <a href="https://github.com/phyllisstein/babel-env-loose-mwe">https://github.com/phyllisstein/babel-env-loose-mwe</a> for a complete MWE.</p>
<h3 dir="auto">Input Code</h3>


<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// src/some-enum/index.ts

export enum SomeEnum {
  MY_NAME = 'MY_NAME',
  YOUR_NAME = 'YOUR_NAME',
}"><pre class="notranslate"><span class="pl-c">// src/some-enum/index.ts</span>

<span class="pl-k">export</span> <span class="pl-k">enum</span> <span class="pl-smi">SomeEnum</span> <span class="pl-kos">{</span>
  <span class="pl-c1">MY_NAME</span> <span class="pl-c1">=</span> <span class="pl-s">'MY_NAME'</span><span class="pl-kos">,</span>
  <span class="pl-c1">YOUR_NAME</span> <span class="pl-c1">=</span> <span class="pl-s">'YOUR_NAME'</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span></pre></div>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// src/enums.ts

import { SomeEnum } from './some-enum'

export { SomeEnum }"><pre class="notranslate"><span class="pl-c">// src/enums.ts</span>

<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-smi">SomeEnum</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'./some-enum'</span>

<span class="pl-k">export</span> <span class="pl-kos">{</span> <span class="pl-smi">SomeEnum</span> <span class="pl-kos">}</span></pre></div>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// src/index.ts

import { SomeEnum } from './enums'

const payload = { name: 'phyllisstein', type: SomeEnum.MY_NAME }

console.log(`You did the thing, ${ payload.name }!`)"><pre class="notranslate"><span class="pl-c">// src/index.ts</span>

<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-smi">SomeEnum</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'./enums'</span>

<span class="pl-k">const</span> <span class="pl-s1">payload</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">name</span>: <span class="pl-s">'phyllisstein'</span><span class="pl-kos">,</span> <span class="pl-c1">type</span>: <span class="pl-smi">SomeEnum</span><span class="pl-kos">.</span><span class="pl-c1">MY_NAME</span> <span class="pl-kos">}</span>

<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">`You did the thing, <span class="pl-s1"><span class="pl-kos">${</span> <span class="pl-s1">payload</span><span class="pl-kos">.</span><span class="pl-c1">name</span> <span class="pl-kos">}</span></span>!`</span><span class="pl-kos">)</span></pre></div>
<details>
<summary>v7.0.0-beta.31 Emitted JS</summary>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// lib/enums.js

&quot;use strict&quot;;

exports.__esModule = true;
Object.defineProperty(exports, &quot;SomeEnum&quot;, {
  enumerable: true,
  get: function () {
    return _someEnum.SomeEnum;
  }
});

var _someEnum = require(&quot;./some-enum&quot;);"><pre class="notranslate"><span class="pl-c">// lib/enums.js</span>

<span class="pl-s">"use strict"</span><span class="pl-kos">;</span>

<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">__esModule</span> <span class="pl-c1">=</span> <span class="pl-c1">true</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">"SomeEnum"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
  <span class="pl-c1">enumerable</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
  <span class="pl-en">get</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">return</span> <span class="pl-s1">_someEnum</span><span class="pl-kos">.</span><span class="pl-c1">SomeEnum</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">var</span> <span class="pl-s1">_someEnum</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"./some-enum"</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
</details>
<details>
<summary>v7.0.0-beta.32 Emitted JS</summary>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// lib/enums.js

&quot;use strict&quot;;

exports.__esModule = true;
exports.SomeEnum = _someEnum.SomeEnum;

var _someEnum = require(&quot;./some-enum&quot;);"><pre class="notranslate"><span class="pl-c">// lib/enums.js</span>

<span class="pl-s">"use strict"</span><span class="pl-kos">;</span>

<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">__esModule</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>
<span class="pl-s1">exports</span><span class="pl-kos">.</span><span class="pl-c1">SomeEnum</span> <span class="pl-c1">=</span> <span class="pl-s1">_someEnum</span><span class="pl-kos">.</span><span class="pl-c1">SomeEnum</span><span class="pl-kos">;</span>

<span class="pl-k">var</span> <span class="pl-s1">_someEnum</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"./some-enum"</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
</details>
<h3 dir="auto">Babel/Babylon Configuration (.babelrc, package.json, cli command)</h3>

<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  &quot;presets&quot;: [
    [&quot;@babel/env&quot;, {
      &quot;targets&quot;: {
        &quot;node&quot;: &quot;current&quot;
      },
      &quot;loose&quot;: true
    }],
    &quot;@babel/typescript&quot;
  ]
}"><pre class="notranslate">{
  <span class="pl-ent">"presets"</span>: [
    [<span class="pl-s"><span class="pl-pds">"</span>@babel/env<span class="pl-pds">"</span></span>, {
      <span class="pl-ent">"targets"</span>: {
        <span class="pl-ent">"node"</span>: <span class="pl-s"><span class="pl-pds">"</span>current<span class="pl-pds">"</span></span>
      },
      <span class="pl-ent">"loose"</span>: <span class="pl-c1">true</span>
    }],
    <span class="pl-s"><span class="pl-pds">"</span>@babel/typescript<span class="pl-pds">"</span></span>
  ]
}</pre></div>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">I should be able to import the enum and run the code transpiled from <code class="notranslate">src/index.ts</code> successfully.</p>
<h3 dir="auto">Current Behavior</h3>
<p dir="auto">Babel emits code that throws a <code class="notranslate">TypeError</code> at runtime:</p>
<details>
<summary>💥</summary>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TypeError: Cannot read property 'SomeEnum' of undefined
    at Object.&lt;anonymous&gt; (/Users/daniel/Repos/Personal/babel-env-loose-mwe/lib/enums.js:4:30)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.&lt;anonymous&gt; (/Users/daniel/Repos/Personal/babel-env-loose-mwe/lib/index.js:3:14)
    at Module._compile (module.js:635:30)"><pre class="notranslate"><code class="notranslate">TypeError: Cannot read property 'SomeEnum' of undefined
    at Object.&lt;anonymous&gt; (/Users/daniel/Repos/Personal/babel-env-loose-mwe/lib/enums.js:4:30)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.&lt;anonymous&gt; (/Users/daniel/Repos/Personal/babel-env-loose-mwe/lib/index.js:3:14)
    at Module._compile (module.js:635:30)
</code></pre></div>
</details>
<h3 dir="auto">Possible Solution</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/222f864ff92847662f7aa5f227735070c333cb4cd676cae03aad3e2280d802ee/687474703a2f2f7068796c2e6c692f64756e6e6f2e676966"><img src="https://camo.githubusercontent.com/222f864ff92847662f7aa5f227735070c333cb4cd676cae03aad3e2280d802ee/687474703a2f2f7068796c2e6c692f64756e6e6f2e676966" alt="" data-animated-image="" data-canonical-src="http://phyl.li/dunno.gif" style="max-width: 100%;"></a></p>
<h3 dir="auto">Context</h3>
<p dir="auto">We use TypeScript's enums as action creator <code class="notranslate">type</code>s in our React/Redux code. I noticed the issue when attempting to import the enum into a test module and make assertions with it.</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>v7.0.0-beta.32</td>
</tr>
<tr>
<td>Babylon</td>
<td>N/A</td>
</tr>
<tr>
<td>node</td>
<td>v8.9.1</td>
</tr>
<tr>
<td>npm</td>
<td>v5.5.1</td>
</tr>
<tr>
<td>Operating System</td>
<td>macOS 10.13.2 (17C67b)</td>
</tr>
</tbody>
</table>
<h2 dir="auto">Bug Report</h2>
<p dir="auto"><strong>Current Behavior</strong><br>
code split fail with preset-env modules:commonjs  v7.5.4</p>
<p dir="auto"><strong>Expected behavior/code</strong><br>
when modules: 'common' and plugin @babel/plugin-syntax-dynamic-import''; babel will tranfrom import(...) to require(...),  then will cause code split fail</p>
<p dir="auto"><strong>Babel Configuration (.babelrc, package.json, cli command)</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                modules: 'commonjs'
            }
        ],
        '@babel/preset-react'
    ],
    plugins: [
        '@babel/plugin-syntax-dynamic-import'
    ]
};"><pre class="notranslate"><span class="pl-smi">module</span><span class="pl-kos">.</span><span class="pl-c1">exports</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
    <span class="pl-c1">presets</span>: <span class="pl-kos">[</span>
        <span class="pl-kos">[</span>
            <span class="pl-s">'@babel/preset-env'</span><span class="pl-kos">,</span>
            <span class="pl-kos">{</span>
                <span class="pl-c1">modules</span>: <span class="pl-s">'commonjs'</span>
            <span class="pl-kos">}</span>
        <span class="pl-kos">]</span><span class="pl-kos">,</span>
        <span class="pl-s">'@babel/preset-react'</span>
    <span class="pl-kos">]</span><span class="pl-kos">,</span>
    <span class="pl-c1">plugins</span>: <span class="pl-kos">[</span>
        <span class="pl-s">'@babel/plugin-syntax-dynamic-import'</span>
    <span class="pl-kos">]</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Environment</strong></p>
<ul dir="auto">
<li>Babel version(s): v7.5.4</li>
<li>Node/npm version: v11.6.0/6.5.0-next.0</li>
<li>OS: [e.g. OSX 10.13.4, Windows 10] OSX</li>
<li>Monorepo: [e.g. yes/no/Lerna]no</li>
<li>How you are using Babel: [e.g. <code class="notranslate">cli</code>, <code class="notranslate">register</code>, <code class="notranslate">loader</code>] cli</li>
</ul>