2018-10-13
Setting up a React Environment for ASP.NET MVC
react, aspnet, webpack, babel
react, aspnet, webpack, babel
Photo by Zoltan Tasi on Unsplash
I had a chance to update a legacy ASP.NET MVC website using AngularJS (yes, the first version) to use Webpack & Babel 7 (which used to import AngularJS files using script tags).
Previous post Setting up an ES6 Environment for ASP.NET MVC 5 was a bit outdated as it was using older version of babel and webpack, so I decided write more concise post to get started with the newest libraries.
As I have moved onto React, I will show you how to set up React environment for ASP.NET MVC 5.
I will assume that you are familiar with NPM & Webpack, so I won't go into too much details on what each option in NPM & Webpack.
Create a new ASP.NET MVC project (choose a choice of your .NET framework).
And select a template.
Add a new item in the project root.
Create NPM configuration file, package.json.
And add a script section. And package.json would initially look like the following.
https://gist.github.com/dance2die/b41670c22bef4553e197e3efcb43c664
Add a new file named .babelrc in the same directory as package.json file created in the previous step.
And add following babel options.
https://gist.github.com/dance2die/a33a0590d930276754ca9b814707fba7
Create a file named webpack.config.js in the project root (same location as package.json & .babelrc) & configure it as shown below.
https://gist.github.com/dance2die/ce43a01f962ef07c7ad89a19d92428e5
Webpack outputs a bundle as ./Scripts/dist/Home/react/bundle.js so let's add the script in View\Home\Index.cshtml razor file.
https://gist.github.com/dance2die/ef395768773057d1c45d6fe54a51f344
Now let's install NPM packages to enable latest JavaScript and React syntax.
https://gist.github.com/dance2die/502546db05519ce16c768da263c90bfd
This is an optional step but to make our lives easier, let's install a Visual Studio extension, NPM Task Runner for running NPM scripts from Visual Studio.
Now we are ready to write a React script using the latest JavaScript syntax (ES6+).
Let's add an entry point for React in Views\Home\Index.cshtml file by deleting everything except ViewBag.Title section and add <div id="app"></div>.
Now we have an entry point, let's write a simple React file index.js under Scripts\Home\react directory.
https://gist.github.com/dance2die/f368f1116bbc34886334d3294817cb7c
You could run the dev script within package.json file but let's use the NPM task runner to make the life easier.
Open the "Task Runner Explorer" by right clicking on package.json file in the project root.
Start dev script (double click), which monitors the changes in index.js.
To enable browser-sync, you need copy a script generated by browser-sync message in _Layout.cshtml under Shared folder near end of </body> tag.
And lastly, let's run ASP.NET from Visual Studio to see the result.
You've installed browser-sync* packages so as you change your code, the browser will reload automatically upon saving.
browser-sync at work
In this post I've assumed that you know the basics of NPM & Webpack so skipped much of details so that you can easily get up and running.
Please refer to documentations linked in-line in the post if you want to understand how each step works and to troubleshoot should you run into an issue.
Source code is available on GitHub.