I\'m trying to convert a pet project to TypeScript and don\'t seem to be able to use the tsc
utility to watch and compile my files. The help says I should use t
The tsc compiler will only watch those files that you pass on the command line. It will not watch files that are included using a /// <sourcefile>
reference. If your working with the bash, you could use find to recursively find all *.ts
files and compile them:
find . -name "*.ts" | xargs tsc -w
Technically speaking you have a few options here:
If you are using an IDE like Sublime Text and integrated MSN plugin for Typescript: http://blogs.msdn.com/b/interoperability/archive/2012/10/01/sublime-text-vi-emacs-typescript-enabled.aspx you can create a build system which compile the .ts
source to .js
automatically. Here is the explanation how you can do it: How to configure a Sublime Build System for TypeScript.
You can define even to compile the source code to destination .js
file on file save. There is a sublime package hosted on github: https://github.com/alexnj/SublimeOnSaveBuild which make this happen, only you need to include the ts
extension in the SublimeOnSaveBuild.sublime-settings
file.
Another possibility would be to compile each file in the command line. You can compile even multiple files at once by separating them with spaces like so: tsc foo.ts bar.ts
. Check this thread: How can I pass multiple source files to the TypeScript compiler?, but i think the first option is more handy.
EDIT: Note, this is if you have multiple tsconfig.json files in your typescript source. For my project we have each tsconfig.json file compile to a differently-named .js file. This makes watching every typescript file really easy.
I wrote a sweet bash script that finds all of your tsconfig.json files and runs them in the background, and then if you CTRL+C the terminal it will close all the running typescript watch commands.
This is tested on MacOS, but should work anywhere that BASH 3.2.57 is supported. Future versions may have changed some things, so be careful!
#!/bin/bash
# run "chmod +x typescript-search-and-compile.sh" in the directory of this file to ENABLE execution of this script
# then in terminal run "path/to/this/file/typescript-search-and-compile.sh" to execute this script
# (or "./typescript-search-and-compile.sh" if your terminal is in the folder the script is in)
# !!! CHANGE ME !!!
# location of your scripts root folder
# make sure that you do not add a trailing "/" at the end!!
# also, no spaces! If you have a space in the filepath, then
# you have to follow this link: https://stackoverflow.com/a/16703720/9800782
sr=~/path/to/scripts/root/folder
# !!! CHANGE ME !!!
# find all typescript config files
scripts=$(find $sr -name "tsconfig.json")
for s in $scripts
do
# strip off the word "tsconfig.json"
cd ${s%/*} # */ # this function gets incorrectly parsed by style linters on web
# run the typescript watch in the background
tsc -w &
# get the pid of the last executed background function
pids+=$!
# save it to an array
pids+=" "
done
# end all processes we spawned when you close this process
wait $pids
Helpful resources:
Create a file named tsconfig.json
in your project root and include following lines in it:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"module": "commonjs",
"target": "ES5",
"outDir": "ts-built",
"rootDir": "src"
}
}
Please note that outDir
should be the path of the directory to receive compiled JS files, and rootDir
should be the path of the directory containing your source (.ts) files.
Open a terminal and run tsc -w
, it'll compile any .ts
file in src
directory into .js
and store them in ts-built
directory.
tsc 0.9.1.1 does not seem to have a watch feature.
You could use a PowerShell script like the one:
#watch a directory, for changes to TypeScript files.
#
#when a file changes, then re-compile it.
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "V:\src\MyProject"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$changed = Register-ObjectEvent $watcher "Changed" -Action {
if ($($eventArgs.FullPath).EndsWith(".ts"))
{
$command = '"c:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.exe" "$($eventArgs.FullPath)"'
write-host '>>> Recompiling file ' $($eventArgs.FullPath)
iex "& $command"
}
}
write-host 'changed.Id:' $changed.Id
#to stop the watcher, then close the PowerShell window, OR run this command:
# Unregister-Event < change Id >
Ref: Automatically watch and compile TypeScript files.
TypeScript 1.5 beta has introduced support for a configuration file called tsconfig.json. In that file you can configure the compiler, define code formatting rules and more importantly for you, provide it with information about the TS files in your project.
Once correctly configured, you can simply run the tsc command and have it compile all the TypeScript code in your project.
If you want to have it watch the files for changes then you can simply add --watch to the tsc command.
Here's an example tsconfig.json file
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false
},
"include": [
"**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]}
In the example above, I include all .ts files in my project (recursively). Note that you can also exclude files using an "exclude" property with an array.
For more information, refer to the documentation: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html