HI WELCOME TO SIRIS

Starting Angular 2 in ASP.NET Core with TypeScript using Visual Studio 2015

Leave a Comment
This tutorial aims for starting Angular 2 in ASP.NET Core using Visual Studio 2015. The release of Angular 2, ASP.NET Core RC is becoming interesting to build SPA.
I have compiled the steps involved in starting to learn Angular 2. This is detailed explanation, you will feel much easier at end of article.
October 1 2016 – Article updated with ASP.NET Core, Angular 2 Final release with Webpack as module bundler .

Step 1 : Creating an empty ASP.NET Core project

Open Visual Studio 2015 Community Edition Update 3, Select New Web Project naming it “ngCoreContacts” and select “Empty” project template. Don’t forget to install new web tools for ASP.NET Core 1.0
Angular 2 in ASP.Net Core
Creating Empty Asp.NET Core app
I used Visual Studio 2015 Community Edition Update 3(Must update), TypeScript 2.0 (must), latest NPM, Gulp.
We are writing Angular 2 in ASP.NET Core with TypeScript but we can use JavaScript and Dart . TypeScript is personal choice.

Step 2: Configure ASP.NET Core to serve Static Files

ASP.NET Core is designed as pluggable framework to include and use only necessary packages, instead of including too many initial stuff.
Lets create HTML file named “index.html” under wwwroot folder.
Right click on wwwroot folder, Add New Item and create index.html file. This HTML page will act as default page.
For ASP.NET Core to serve static files, we need to add StaticFiles middle ware in Configure method of Startup.cspage. Ensure that packages are restored properly.
project.json is redesigned to make it better, we have Static Files middleware to serve static assets like HTML, JS files etc.
Interestingly Program.cs is entry point of application execution just like void main().
Run the application now, ASP.NET Core renders static HTML page.
Delete this index.html page, we will be injecting this dynamically later. Till now you saw demonstration of wwwroot as root folder for ASP.NET Core web apps.

Step 3: Angular 2 in ASP.NET Core

Angular 2 is famously claiming to be ONE Framework for MOBILE and DESKTOP apps. There’s won’t be any breaking changes after final release.
This tutorial refers 5 MIN QUICK START for getting started, it’s more focused on other light weight code editors; but here we are using Visual Studio 2015 Community Edition Update 3 for its built in TypeScript tooling and other features.
We will be using Webpack for module bundler, it’s an excellent alternative to the systemJS approach. To know more about inner details of read “webpack and Angular 2
Majority of webpack scripting is based on AngularClass’s angular2-webpack-starter. I have modified according to ASP.NET Core web apps.
ASP.NET Core used here is SPA type of website, no MVC used here.
Why I choose Webpack for Angular 2 in ASP.NET Core?
  • Webpack is much simpler to code, plugins based working with static files.
  • Webpack 2 with dev-server runs application in memory, live reloading & compilation is easy. It provides tree shaking to eliminate unused code.
  • Integrating with 3rd party packages like Angular Material 2, AngularFire, bootstrap are just one line inclusion
  • AngularClass webpack  starter kit provides HMR (Hot Module Replacement) – maintain execution state while code gets modified.
  • Webpack is adopted by Angular CLI, AngularService by Microsoft for making bundles much smaller.

Step 4: Adding NPM Configuration file for Angular 2 packages

Angular 2 team is pushing the code changes using NPM rather than CDN or any other source, due to this we need to add NPM configuration file (package.json) to this ASP.NET Core application.
Right Click on “ngCoreContacts“, add new file “NPM Configuration File“; by default package.json is added to ASP.NET Core project. This acts Node Package Manager (NPM) file, a must for adding packages for Angular 2
From the Angular 2 Quick start provided above, we need to add dependencies for required for Angular 2 in ASP.NET Core application. Copy Paste below configuration in package.json file
Right after saving this, ASP.NET Core starts restoring the packages. It would download all packages mentioned in dependencies section of above package.json.
Sometimes in solution explorer you might see ‘Dependencies – not installed’, don’t worry this bug in tooling. All the npm packages are installed.

Step 5: Add TypeScript configuration file – must for Angular 2 in ASP.NET Core using TypeScript

We are creating Angular 2 in ASP.NET Core starting with TypeScript, this obvious reason adds to include TypeScript Configuration file which does work of transpiling it to JavaScript, module loading, target ES5 standards.
Refer my article on Getting Started with TypeScript if you just want beginner intro on it.
Add “tsconfig.json” in the project, copy paste below configuration.
baseUrl” ensures that TypeScript files are transpiled to JavaScript from the ‘./clientsrc‘. This folder is virtual directory for TypeScript
It’s mandatory to install TypeScript 2.o for working with Angular 2.
At present typings.json is not required because we are using @types with TypeScript. However if your using any other packages which don’t have entries in @types then typings.json has to be added.

Step 6: Usage of Webpack as module bundler

What is Webpack?

Webpack is a powerful module bundler. A bundle is a JavaScript file that incorporate assets that belong together and should be served to the client in a response to a single file request. A bundle can include JavaScript, CSS styles, HTML, and almost any other kind of file.
Webpack roams over your application source code, looking for import statements, building a dependency graph, and emitting one (or more) bundles. With plugin “loaders” Webpack can preprocess and minify different non-JavaScript files such as TypeScript, SASS, and LESS files.
In package.json, we have added webpack packages as devdependencies. They will perform all bundling work.
What webpack does is written in a JavaScript configuration file know as webpack.config.js. As always the applications are run in DevelopmentTest and Production environment.
There are some common functionalities and some specific to environments. We will focus on development and production environment to write accordingly.
Development environment should have source maps for debugging TypeScript files, minifying bundles of JS, CSS etc files not necessary.
Production environment should minify bundles to reduce loading time, do not include source maps. Webpack 2 also does tree shaking i.e. eliminate unused code to further reduce bundle sizes.
The entire source code is on my github repo, fork or clone to play with it.
webpack.config.js – Based on environment set process.env.NODE_ENV, it runs either dev or prod configurations.
Webpack.common.js before bundling environment specific files, it performs tasks meant to be used for both environment.
  1. Webpack splits Angular 2 apps into 3 files polyfills(to maintain backward compatibility with older browsers) , vendors(all JS, CSS, HTML, SCSS, images, JSON etc into one file) and boot (application specific files)
  2. resolve based on various file extensions
  3. Webpack itself doesn’t know what to do with a non-JavaScript file. We teach it to process such files into JavaScript with loaders. For this, we have written loaders TS, HTML, JSON, Fonts, images
  4. Any static assets placed in clientsrc/assets” will be copied to assets folder using CopyWebpackPlugin
  5. CleanWebpackPlugin cleans wwwroot/dist” folder every time we run it, so that we get fresh set of files.
  6. I told you above to delete the index.html file, now the clientsrc/index.html will be moved to wwwroot using HtmlWebpackPlugin. Plus Webpack injects the bundle files i.e. polyfillsvendorboot JS files and includes them in HTML script reference.
Now let’s see webpack.dev.js for development purpose
  1. Running webpack-dev-server” – this runs entire application in memory, any changes to source file gets applied immediately
  2. Loads application in debug mode with source map. Everything run in memory i.e. html, js, static files are loaded in memory.
  3. Runs the application on localhost 3000 port. Port can be changed as your convenience
Now let’s see webpack.prod.js for production purpose
  1. Merges all the bundle files and copies to wwwroot.
  2. Minifies all files to load faster using UglifyJsPlugin plugin

Step 7: Writing Angular 2 application

Until now we created ASP.NET Core app, added TSconfig file, webpack configuration. Now it’s time to write Angular 2 application
In the github repo, you can see “clientsrc” folder. This contains the angular 2 app which gets bundled into using webpack configurations we wrote
Clientsrc folder has index.html, polyfills.browses.ts, vendor.browsers.ts and mostly importantly boot.ts
We have app folder containing HTML, Angular 2 components and root level module (app.module.ts) which gets loaded while bootstrapping application.
Some of files might be not interesting now, will focus them in separate articles later.

Step 8: Running the application

Before running make sure you have run command “npm install”. This might not be needed but still it will ensure all packages are installed.
Now let’s run the application in development mode
  1. From command line (directory should be same as package.json), type “npm start” & hit enter. It will start running the webpack-dev-server which loads application and listens on localhost:3000.
  2. When on console it says “bundle is now VALID” then open a browser and navigate to http://localhost:3000 to see application getting loaded.
Notice wwwroot folder, we don’t see any files copied because everything is running in memory.
Now that application runs properly on browser, let’s understand how Angular 2 app loads
  1. When browser starts rendering index.html page, it encounters <my-app>Loading…</my-app> tag.
  2. Then Angular’s module platformBrowserDynamic bootstraps clientsrc/app/AppModule through line platformBrowserDynamic().bootstrapModule(AppModule)
  3. AppModule then loads the component app.component.ts which is mentioned in @NgModule as bootstrap entry
  4. Clientsrc/src/Appcomponent then resolves the <my-app> tag as selector in it and renders UI with TypeScript code.
When we enter npm start” in console to run the application, execution points scripts section of package.json to below code
This invokes webpack-dev-server, runs the development config and watches for any changes in clientsrc folder. Any changes in this folder will reload application with changes.
Here ASP.NET Core is just HTML based web app, so running this app as npm start to use AngularClass features of reloading, using webpack, Hot module replacement feature.
Running the application in Production mode
Assuming the application is now ready to deployed, we need to have PROD build. For this run command
Now if you see wwwroot folder, we see the HTML, JS bundle files. This wwwroot folder can be deployed on any web server like IIS or nginx
You can either do F5 to run from Visual Studio IDE or run command npm run server:prod

Angular 2 in ASP.NET Core
Angular 2 running on ASP.NET Core using Web pack
There is Angular 2 application development using Angular 2 Command Line Interface also, it’s interesting !!
Check out source code on my Github repo ng2CoreContacts

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.