title: Webpack libraryTarget: "module" generates code with `'return' outside of function`
body: <p>I'm trying to output an ESM module with WebPack 5.  My understanding is that this feature has been added in version 5, and is used by specifying <code>{output: libraryTarget: 'module'}}</code> and <code>{experiments: {outputModule: true}}</code>.</p>
  <p>I believe that what I'm seeing is just a WebPack bug - perhaps related to <code>outputModule</code> still being experimental, but I'm not very well versed in WebPack so could easily have missed something.</p>
  <p>I get an error <code>'return' outside of function</code>.  With the configuration below, this happens when I run <code>npx webpack</code>.  If I change <code>mode</code> to <code>'development'</code>, then it generates a bundle which indeed has a <code>return</code> statement at the end such that attempting to use it generates a similar error at runtime.</p>
  <p><strong>webpack.config.js:</strong></p>
  <pre><code>const path = require('path');
  
  module.exports = {
      entry: &quot;./index.js&quot;,
      resolve: { extensions: ['.js'] },
      //using 'production' rather than 'development' catches error earlier.
      mode: 'production',
      experiments: {
          outputModule: true
      },
      output: {
          filename: 'hello.js',
          libraryTarget: &quot;module&quot;,
          path: path.resolve(__dirname, 'dist')
      }
  }
  </code></pre>
  <p><strong>index.js</strong></p>
  <pre><code>export default function() { return 42; }
  </code></pre>
  <p><strong>package.json</strong></p>
  <pre><code>{
      &quot;name&quot;: &quot;webpackTest&quot;,
      &quot;version&quot;: &quot;1.0.0&quot;,
      &quot;description&quot;: &quot;&quot;,
      &quot;main&quot;: &quot;index.js&quot;,
      &quot;scripts&quot;: {
        &quot;webpack&quot;: &quot;webpack&quot;,
        &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
      },
      &quot;keywords&quot;: [],
      &quot;author&quot;: &quot;&quot;,
      &quot;license&quot;: &quot;ISC&quot;,
      &quot;devDependencies&quot;: {
        &quot;webpack&quot;: &quot;^5.5.1&quot;,
        &quot;webpack-cli&quot;: &quot;^4.2.0&quot;
      }
  }
  </code></pre>