This sbt plugin compiles the Typescript code in your Play application to javascript fit for consumption by your average browser and device.
The easiest way to get started is to use the demo projects for Angular2 or React. You can create the Angular2 application locally by running sbt new joost-de-vries/play-angular-typescript.g8
. For the React application that's sbt new joost-de-vries/play-reactjs-typescript.g8
Create a tsconfig.json
file in the root of your project with the required compiler options. The following tsc
compiler options are managed by sbt-typescript
so setting them in tsconfig.json
has no effect: outDir
, rootDirs
, paths
, baseUrl
, typeRoots
.
If you use the stage
compile mode the outFile
option is also managed by sbt-typescript
.
To be able to view the original Typescript code from your browser when developing add the following to tsconfig.json
"compilerOptions": {
"sourceMap": true,
"mapRoot": "/assets",
"sourceRoot": "/assets",
Add the following line to your project\plugins.sbt
:
addSbtPlugin("name.de-vries" % "sbt-typescript" % "2.6.2")
If your project is not a Play application you will have to enable sbt-web
in build.sbt
:
lazy val root = (project in file(".")).enablePlugins(SbtWeb)
There are several Javascript engines you can use for the build. The fastest is NodeJs. So make sure you have a recent NodeJs installed and add to build.sbt
JsEngineKeys.engineType := JsEngineKeys.EngineType.Node
NPM libraries are used as standard sbt dependencies (jar files). Add your typescript libraries as dependencies as follows. If the library doesn't include typescript definitions add them too.
resolvers += Resolver.bintrayRepo("webjars","maven")
libraryDependencies ++= Seq(
"org.webjars.npm" % "react" % "15.4.0",
"org.webjars.npm" % "types__react" % "15.0.34"
)
These NPM dependencies are resolved through Webjars. Check whether the versions of the NPM packages you need are available there. If not you can add them yourself. Since we added the webjars resolver they'll be available immediately. Otherwise you'd have to wait a while before being able to use them. NB NPM package names like @angular/code
and @types/react
are a bit different in webjars: angular__react
and types__react
.
Add the following to build.sbt
to resolve against those npms.
resolveFromWebjarsNodeModulesDir := true
To lint your Typescript code add sbt-tslint
to your project and create a tslint.json
file with the linting rules.
To test your Typescript code add an sbt plugin for a JS testframework. For instance sbt-jasmine or sbt-mocha. You can override tsc
configurations for your test code. To do that create a file tsconfig.test.json
and add to build.sbt
(projectTestFile in typescript) := Some("tsconfig.test.json")
Any settings in that file will override those in tsconfig.json
for the compilation of test code.
The typescript version of your project can be found in project/target/node-modules/webjars/typescript
Configure your IDE to use that and point it to the tsconfig.json
.
Sometimes it can be helpful to compile your project directly through the Typescript compiler without sbt-typescript
in between to check whether a problem is an sbt-typescript
problem. To do that you can run
project/target/node-modules/webjars/typescript/bin/tsc -p . -w
Make sure to set the executable bit if necessary.
For this kind of compilation to work you have to fill in the settings in tsconfig.json
that sbt-typescript
normally manages. See the Angular2 demo project for an example.
You can develop using individual javascript files when running sbt ~run
in Play and have your whole typescript application concatenated into a single javascript output file for your stage environment without changes to your sources. To do that you have to add a -DtsCompileMode=stage
parameter to the sbt task in your CI that creates the stage app. So for Play that will often be sbt stage -DtsCompileMode=stage
.
If you are importing modules for which you don't have the typings you can ignore the TS2307 can not find module
error:
tsCodesToIgnore := List(canNotFindModule)
- typescript version upgrade
- upgrade to sbt-web 1.4.3
- webjars-locator 0.35
- upgrade typescript to v2.6.1
- cross build to sbt 0.13.6 and 1.0.1. alpha release
- upgrade to ts 2.5.2
- fixes issue with test tsconfig overrides
- upgrade to sbt-js-engine 1.2.1 and sbt-web 1.4.1
- add correct typeRoots values to tsconfig.json for resolution of @types type def dependencies
- allow for overrides of the tsconfig.json for test code
- upgrade to ts npm 2.4.1
- upgrade to ts npm 2.3.2
- upgrade to ts 2.3 final: npm 2.3.1
- sbt-typescript follows the typescript version
- upgrade to ts 2.3.0
- fixes readme. Tx camilosampedro
- upgrade to ts 2.2.1
- fixes running on Trireme Tx VeryBueno!
- supports multi project builds. Uses sbt-web webModules for js deps resolution instead of nodeModules. Hence the alfa moniker to see whether f.i. @types resolution still works.
- fixes compilation of test assets
- allows configuring outfile with a path
- makes compile errors 1 based instead of 0 based
- upgrades to typescript 2.1 (npm 2.1.4)
- upgrades to typescript 2.1 RC (npm 2.1.1)
- resolves webjar @types type definitions
- upgrades to typescript 2.0.6
- solves an issue (#9) where RxJs would cause a nullpointer.
- uses typescript 2.0.3
- solves an issue (#9) where RxJs would be extracted to the wrong directory.
- uses typescript 2.0 RC (npm 2.0.2)
- uses typescript 2.0 beta (npm 2.0.0)
- uses standard typescript functionality to resolve against webjars. Instead of the previous custom rolled module resolution extension.
- uses a snapshot of the upcoming typescript 2.0
- add output assertion options
- adds convenience task for setting up tsc compilation
- fixes jstaskfailure error
- allows for developing using individual javascript files and using a single javascript file in production
- upgrades to typescript 1.8.10
- upgrades to typescript 1.8.7
- adds support for tests in typescript
- upgrades to typescript 1.8.2
- improves output of single outfile
- fixes a nasty bug in module resolution. This is essential for angular2 applications.
- gives feedback on faulty compiler options.
The plugin is young. Currently it is mostly tested against EngineType.Node
and Angular2 applications with npm style dependencies.
There are some other features I'm planning to implement.
I started this plugin because the features I mentioned above were missing in the existing plugins.
And since I'd like Play and sbt(-web) to be kickass build tools for Typescript and Angular2 applications, and I wanted to give back to the open source community, I thought I'd implement it myself.. But not by writing javascript if I could just as well write Typescript...
Kudos to Brendan Arp for his javascript tsc driver to get me started. And also to all of the other plugins mentioned here. Open source is an amazing tool for collective learning. Just imagine those poor programmers in the 1970s with only IBM manuals to provide them with information.