I'm a long time (15+ years) Visual Studio user and am excited to learn both Typescript and the new leaner, meaner Visual Studio Code. I've been trying to setup VSCode like VS, where when you hit F5 it transpiles to Javascript and launches the debugger.
I've found posts that trigger it on save, or using another shortcut (run via tasks.json) - but I'm really surprised there isn't a way of doing this on such a fundamental command, unless I'm really missing something embarrassingly obvious.
At this point I'm just trying to use the "Hello World" greeter sample checked out directly from Microsoft, where it has a manual step of tsc --sourcemap greeter.ts
. I just want that to happen automatically, like it does in VS, on any files that need transpiling before I start to run / debug.
Does anyone know how to do this? Thank you.
The short answer (in a new project directory) is:
.vscode/tasks.json
is not already configured, a message will give you the option to Configure Task Runner, press that button.tasks.json
window this opened up. After having tried both TypeScript options, I prefer the ... in watch mode option. That way, after starting the background process with Ctrl+Shift+B once, incremental transpiles happen whenever a file is saved, making launching the program for debugging that much faster...
For more details, follow this link: http://code.visualstudio.com/docs/languages/typescript#_transpiling-typescript-into-javascript
If you want to generate a source map (a good idea), you need to setup tsconfig.json
. The quick way to do that from VSCode is:
tsc --init --sourceMap
to set up a template tsconfig.json
exit
Finally, if you want to use the source map while debugging, you need to generate and edit the .vscode/launch.json
file. The quick way to do that is:
.vscode/launch.json
doesn't already exist, pressing F5 will prompt you to choose and environment. Choose Node.js."sourceMaps": false
with "sourceMaps": true
.I'm not sure why the defaults omit source maps, it currently seems like one of the most common bumps in the road for people starting to use VSCode + TypeScript.
So, using help from responses here was the path of least resistance. After opening vscode, I:
{ "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": true } }
class Startup { public static main(): number { console.log('Hello World'); return 0; } } Startup.main();
So tsc compiler seems pretty slow, takes about 4-5 seconds to transpile on my 2 year old Win10 computer. I see the benefit of TypeScript - Watch Mode so it could it's transpiling as you hit Save. Otherwise, if you transpile everytime before you debug, it doesn't seem to detect whether it needs a transpile or not, so this is the ideal scenario that matches what Visual Studio developers are used to.
Essentially VS Code allows you to create tasks to automate development processes (cleaning files/folders, compiling, bundling, etc.) and also allows you to create environment configurations to run your project.
To have them work together the environment configuration allows specification of a preLaunchTask which will run the defined task before debugging. To create a workflow similar to Visual Studio you could create a compile task and run the task before the debugger by setting it as the preLaunchTask.
Example running tsc (TypeScript compiler) before debugging
1.Create a tsconfig.json file in the root of your project with your choice of compile options.
Example tsconfig.json file (You may need to change the module setting to best suit your project. amd is typically a web project, commonjs is typically a node.js project but everyone's preference varies:
{
"compilerOptions": {
"target": "es6",
"module": "amd",
"sourceMap": true,
"allowSyntheticDefaultImports": true
},
"exclude": [
"node_modules",
"bower_components",
"jspm_packages",
"tmp",
"temp"
]
}
2.Create a launch.json file to run and debug your project and set the preLaunchTask to tsc.
The easiest way to do this is simply hit F5. The first time you run it will ask to "Select an environment", if you have already created a launch.json file and would like to start fresh, delete the file. For TypeScript I normally choose node.js which will allow you to run in process or within a web context with frameworks such as express.
Example launch.json file (this example is of an express app):
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/bin/www",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "tsc",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outDir": null
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With