forge

Including TypeScript in a SharePoint Project

If you missed it, Microsoft announced a new language that is a super set of JavaScript that compiles down into regular JavaScript. The compiler is written in JavaScript and works everywhere. The big benefit of the language is type-checking at compile/edit time. If you want to learn more go to http://www.TypeScriptLang.org/.

There is some rather good tooling and documentation, but one problem for me was making it work from inside my SharePoint Projects after I installed TypeScript. The way that the SharePoint tools run, they do the deployment before the TypeScript compiler runs. That’s not exactly helpful, however, you can fix this. First, you’re going to need to right click on your project file and unload it.

Next, you need to right click it again and edit the project file (TypeScriptFarmTest.csproj)

Help Your SharePoint User

Then you need to modify your Project node to include a new InitialTargets attribute pointing to TypeScriptCompile:

<Project ToolsVersion=4.0 DefaultTargets=Build xmlns=http://schemas.microsoft.com/developer/msbuild/2003 InitialTargets=TypeScriptCompile>

Then you’ll need to insert into the inside of this node a new Targets node:

<Target Name=TypeScriptCompile BeforeTargets=Build>
<Message Text=Compiling TypeScript… />
<Exec Command=&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; -target ES5 @(TypeScriptCompile ->’&quot;%(fullpath)&quot;‘, ‘ ‘) />
</Target>

From here save the file, close it, and right click the project and select Reload Project. Now all your TypeScript will compile into JavaScript before the deployment happens (actually before anything else happens because we told Visual Studio and Build to do this operation first, before anything else (with the Project InitialTargets attribute.)

1 reply

Comments are closed.