title: .NET Core with Webpack 4 - how to access javascript module from razor view?
body: <p>I have been looking everywhere and can't for the life of me figure this out. Hoping someone here has the right answer...</p>
  
  <p>I created a new .NET Core 2.1 web app with c# and Razor Views. I'm seasoned in MVC but new to .NET Core and especially the notion of webpack. I used to use requireJS for bundling and handling modules, now I'm using webpack to create my bundles. </p>
  
  <p>Every webpack example I've seen just includes a reference to the bundle from within a razor view - i.e..<code>&lt;script src="~/dist/app.bundle.js"&gt;&lt;/script&gt;</code> and everything is done within the script. </p>
  
  <p>My question is - how do I access the object within that bundle, or more importantly, pass it some parameters? What I want to do is something like this:</p>
  
  <p>This would be the app.js that compiles into app.bundle.js with webpack:</p>
  
  <pre><code>export default class ES6Lib {
      constructor(userName) {
          this.text = "&lt;h1&gt;Hello there, " + userName + "!&lt;/h1&gt;"; 
      }
      getData() {        
          return this.text;
      }
  }
  </code></pre>
  
  <p>Basically I want in my specific Razor view (not just _Layout.cshtml) to include this bundle and code, and instantiate it so I can pass in a value from the server-side, in this case, the userName would come from server.</p>
  
  <p>Something like:</p>
  
  <pre><code>&lt;script src="app.bundle.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var userName = '@Model.UserName'; // from server = 'Jesse'
      var myClass = new ES6Lib(userName); // pass in a parameter
      var greeting = myClass.getData(); // run a function
  
      console.log(greeting); // prints 'Hello there, Jesse!'
  &lt;/script&gt;
  </code></pre>
  
  <p>Obviously, this doesn't work - but how the heck can I instantiate an object from a module and pass a value into it? Or how do I build a correct module that can be used in this way? </p>
  
  <p>Some examples would be very, very helpful. Thanks! </p>