title: How do you author a module that is compatible with CommonJS, AMD and Browser in ES6 with Babel?
body: <p>In normal ES5 if I want to author a package that could be use on either server side (via CJS, AMD, RequireJS) or browser, I would do something like this:</p>
  
  <pre><code>(function(name, definition)){
     if (typeof exports !== 'undefined' &amp;&amp; typeof module !== 'undefined') {
         module.exports = definition();
     } else if (typeof define === 'function' &amp;&amp; typeof define.amd === 'object') {
         define(definition);
     } else {
         this[name] = definition();
     }
  }('foo', function foo(){
         'use strict';
  
          return 'foo';
  })
  </code></pre>
  
  <p>How'd I achieve this functionality while writing a package in ES6? </p>
  
  <p>What I tried:</p>
  
  <pre><code>if (typeof exports !== 'undefined' &amp;&amp; typeof module !== 'undefined') export Foo;
  else if (typeof define === 'function' &amp;&amp; typeof define.amd === 'object') define(Foo);
  else this['Foo'] = Foo;
  </code></pre>
  
  <p>While transpiling this, Babel throws me an error:</p>
  
  <pre><code>SyntaxError: src/index.js: 'import' and 'export' may only appear at the top level (10:69)
     9 |
  &gt; 10 | if (typeof exports !== 'undefined' &amp;&amp; typeof module !== 'undefined') export Foo;
       |                                                                      ^
    11 | else if (typeof define === 'function' &amp;&amp; typeof define.amd === 'object') define(Foo);
    12 | else this['Foo'] = Foo;
    13 |
  </code></pre>
  
  <p>It did work when I used <code>module.exports</code> instead of <code>export</code> but is it possible to do this strictly using ES6?</p>