Example code on Github<\/a>.<\/em><\/p>\n This is the second part in my small series on ASP.NET Core and the coolest of the cool JavaScript libraries out there, except that they are probably already outdated by the time I finish writing this.<\/p>\n In part I<\/a> we took a look on how to install npm and webpack into our ASP.NET Core project. In this part we will set up TypeScript. Visual Studio (VS) will compile TypeScript for you automatically but we will disable that feature and let webpack do the TypeScript build just for the fun of it.<\/p>\n Set up TypeScript<\/strong> Notice how package.json is updated with typescript and ts-loader. You may be wondering what ts-loader<\/a> is. I know I was. It is a “TypeScript loader for webpack” which really does not say much but it is the thing that makes webpack take care of our TypeScript code.<\/p>\n While we are at it let’s install Knockout.js<\/a> which we will use for building view models in TypeScript.<\/p>\n By using @types we tell npm to install the typings for Knockout as well. I tend to think of typings being to TypeScript what header-files are to C++. The typings go into the node_modules folder as everything else.<\/p>\n Next we need to create a configuration file for TypeScript. Right-click your project node in VS solution explorer and click “Add New Item”. Search for “json” in the templates dialog and choose “TypeScript JSON Configuration File”. The file must be name “tsconfig.json”. Change the contents so that it looks something like this:<\/p>\n Notice that I have told the TypeScript loader to put the generated .js files in the folder “wwwroot\/build”, and that I have told it to resolve any 3rd party modules by using “node”<\/a> (i.e. it will look in the “node_modules” folder).<\/p>\n Test TypeScript build<\/strong> Note how VS IntelliSense kicks in as you type. Very cool. Above we set “modeResolution” to “node” so that the TypeScript loader knows to look in node_modules to find Knockout. Now lets add a view model with two observable fields using the Knockout TypeScript definitions. The last line applies the Knockout bindings to the view.<\/p>\n Now if you build your project in VS, you will see a new folder underneath “wwwroot\/build” with the compiled .js file.<\/p>\n Set up the webpack TypeScript load<\/strong> Now, add a file called webpack.config.js to your project at the project root. Paste the following into the file.<\/p>\n This configures webpack to compile the .ts files. It also instructs webpack to take the compiled .js file and bundle it together with some other site.js file that we might have in our project and put it all into a file called bundle.js places in “wwwroot\/dist”. This is the file you want to reference in your HTML files. By the way, the compiled .js files will no longer end up in the “wwwroot\/build” folder so you can delete that.<\/p>\n Webpack build<\/strong> Then remove the line containing “compileOnSave” from tsconfig.json.<\/p>\n Finally, go to the cmd prompt and run the following npm command from your project folder.<\/p>\n You should now see the file bundle.js in “wwwroot\/dist”.<\/p>\n Of course you don’t want to go to the cmd prompt every time you have changed something in your .ts files, so we want VS to run the npm build. Fortunately, the ever-present Mads Kristensen has created a VS extension<\/a> that does it for you. After installing the extension you can see the npm custom build task in Visual Studio’s Task Runner Explorer. Right-click “build” to tell VS to run the build task before or after your normal VS build.<\/p>\n <\/p>\n This will add a line to the package.json file.<\/p>\n Cleaning up<\/strong> Also, you might want to remove bower.json and bundleconfig.json if present, as package.json and webpack.config.js replace them. As far as I know bundleconfig.json works with another Mads Kristensen extension to bundle .js files.<\/p>\n That’s it. Now it is up to you to take fully advantage of webpack for code splitting<\/a> and uglifying<\/a> and what not<\/a>.<\/p>\n
\nStart by installing TypeScript using npm (this is all described on in webpack’s documentation<\/a>):<\/p>\nnpm install --save-dev typescript ts-loader<\/code><\/p>\n
npm install --save-dev knockout @types\/knockout<\/code><\/p>\n
{\n \"compilerOptions\": {\n \"outDir\": \".\/wwwroot\/build\/\",\n \"noImplicitAny\": false,\n \"noEmitOnError\": true,\n \"removeComments\": false,\n \"sourceMap\": true,\n \"target\": \"es5\",\n \"module\": \"commonjs\",\n \"moduleResolution\": \"node\",\n \"compileOnSave\": true\n },\n \"exclude\": [\n \"node_modules\",\n \"wwwroot\"\n ]\n}\n<\/code><\/pre>\n
\nLet us test that we can build TypeScript files. By default VS will build what ever .ts files you add to the project. Start by creating af Scripts folder in your project next to the wwwroot folder. Add a TypeScript a file named “myviewmodel.ts” to the folder. We will create a Knockout view model class so start by importing Knockout to the .ts file by adding the following line at the top.<\/p>\nimport * as ko from \"knockout\"\n<\/code><\/pre>\n
import * as ko from \"knockout\"\n\nclass MyViewModel {\n firstname: KnockoutObservable<string>;\n lastname: KnockoutObservable<string>;\n\n constructor(firstname: string, lastname: string) {\n this.firstname = ko.observable(firstname);\n this.lastname = ko.observable(lastname);\n }\n}\n\nko.applyBindings(new MyViewModel(\"Jakob\", \"Christensen\"));\n<\/code><\/pre>\n
\nInstead of letting VS do the TypeScript build we want webpack to do it and we already installed ts-loader to do it for us. Why would we want to do that now that VS can do it for us? I like to do it because I prefer to keep everything front-end-ish together. So, webpack does the build, the bundling, the code splitting, etc.<\/p>\nvar path = require('path');\n\nmodule.exports = {\n entry: {\n site: [\n '.\/wwwroot\/js\/site.js', \n '.\/scripts\/myviewmodel.ts']\n },\n output: {\n filename: 'bundle.js',\n path: path.resolve(__dirname, 'wwwroot\/dist\/')\n },\n module: {\n rules: [\n {\n test: \/\\.tsx?$\/,\n loader: 'ts-loader',\n exclude: \/node_modules\/,\n },\n ]\n },\n resolve: {\n extensions: [\".tsx\", \".ts\", \".js\"]\n }\n};\n<\/code><\/pre>\n
\nTo build and bundle, first edit your package.json so that the build block looks like this.<\/p>\n \"scripts\": {\n \"build\": \"webpack\"\n },\n<\/code><\/pre>\n
npm run build<\/code><\/p>\n
\"-vs-binding\":{\"BeforeBuild\":[\"build\"]}\n<\/code><\/pre>\n
\nAs I said above, VS automatically picks up .ts files and builds. You don’t want that when using webpack. To disable the VS build, right-click your project in Solution Explorer and choose “Edit [your project name].csproj”. Add the following line under the <PropertyGroup><\/code> element.<\/p>\n
<PropertyGroup>\n <!-- ... -->\n <TypeScriptCompileBlocked>true<\/TypeScriptCompileBlocked>\n<\/PropertyGroup>\n<\/code><\/pre>\n
Share this:<\/h3>