Firstly, let me say that I love Azure and especially the simplicity that it provides when deploying projects directly from GitHub – I absolutely love the ability to check into the master branch of a project and it automatically deploy to the Azure Web App service that I have it configured on… usually.
Typescript is not automatically compiled on deployment.
When developing applications using Typescript (a good idea for any node project) you would be forgiven for assuming that Azure Web Apps would have the built in ability to recognise that you are using Typescript and automatically run the TSC (typescript compiler) after a deploy… but you would be wrong.
I have spent the last few hours trying to get this sorted out and alas I am successful so I am sharing the solution with you so that you can save yourself a little time and once again enjoy continuous integration and deployment with Azure and your Typescript projects.
Azure CLI
The first thing we need to do is install the Azure CLI globally using NPM, its dead simple and is done using the following command…
npm install azure-cli -g
Once that is done we need to login to the Azure CLI using your Azure credentials (again this dead simple and is done with the following command… (Follow the instructions in the command window and load up the url provided in a browser, entering the code).
azure login
Great, once that is all done and dusted we need to change the config mode, first change directory the root of your node project and run the following command…
azure config mode asm
Once done, we want to use the Azure CLI to generate some deploy scripts for us, these deploy scripts are run after each deployment to Azure… run the following command to generate them…
azure site deploymentscript --node
Great! that generates two files… .deployment and deploy.sh, we want to edit the deploy.sh file to add a few things, basically we want to add a command to the end of the file that tells Azure to run the TSC (Typescript Compiler) for our project and therefore generating all our .js and .js.map files.
In the deploy.sh file locate the following code…
# 3. Install npm packages if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then cd "$DEPLOYMENT_TARGET" eval $NPM_CMD install --production exitWithMessageOnError "npm failed" cd - > /dev/null fi
Immediately after add the following…
# 4. Install typescript definitions cd "$DEPLOYMENT_TARGET" #4. Run TSC TSC="$DEPLOYMENT_TARGET/node_modules/typescript/bin/tsc" echo "Building Typescript files." "$TSC" exitWithMessageOnError "Could not run 'tsc'. Did 'npm install' run OK?" echo "Finished Typescript build."
Ensure typescript is added to project dependencies
It is worth noting that in order for this to work, we need to make sure that Typescript is listed as a project dependency (not always the case if you have installed it globally). Lets do that by running this command from the root of your project…
npm install typescript --save
This should be easier.
I truly believe that as Typescript is a Microsoft developed superset of Javascript that this compilation should be automatic, I would be unsurprised if Microsoft added this feature later to Azure but for now this should get you going.
Enjoy.