Which compiler to use for Koa - TSC or Babel? - node.js

I am using koa framework of node.js. I have implemented my code using typescript. I have created .ts files for the same and I am using tsc compiler to compile it.
Should I use babel or tsc for compiling the code?

Before sometime, I read about babel. Now, I have a doubt wheather to use babel or tsc for compiling the code. Please suggest.
Both are excellent choices.
Babel: Main focus is transpiler
TypeScript: In addition to be a transpiler main focus is a type checker.
Opinion
I prefer TypeScript for reasons covered here 🌹

Related

WebStorm: import/export statement in JavaScript

I'm using WebStorm and a newbie to it. when I use import/export statements, it gives me an error of
Unexpected token import
but if I try with require/module.exports it works fine.
N.B- I've configured language version as ES6 from Languages and Frameworks.
This is not WebStorm but Node.js that fails. While import is a part of ES6, native support for ES6 modules in Node.js is very limited and requires special setup - see https://nodejs.org/api/esm.html#esm_enabling. So, you have to compile your code with Babel first. Usually transpiling is a part of build process (using Gulp, Grunt, WebPack, etc.). Or, you can transpile your code on-the-fly by passing -r babel-register to Node.js. Of course, you need creating appropriate .babelrc and install the required modules (npm install --save-dev babel-cli babel-preset-env)

Compiling Typescript to NodeJs: es6 types issues

I think i'm missing something with the typescript 2 type system when used with nodejs.
Here is the situation:
Compiling a small nodejs express server written in typescript to plain es5 in order to be run under node 6.10.0 (target: es5 in tsconfig.json).
In my package.json, i installed #types/node (7.0.3) to get node's type informations.
When installing my project using npm (v3.10.10) and then compiling it with typescript (v2.1.5) i get a bunch or errors related to 'Iterable' and 'Iterator' symbol, (which are es6 symbols).
So from what i understand, #types/node use es6 types out of the box, assuming they are already availables.
In order to have the es6 types (Iterator, Iterable and so on) it seams that there is two solutions:
Add the core-js package.
Target es6 instead of es5 in tsconfig.json which will force typescript to use it's lib/lib.es2015..d.ts* definitions files.
In my opinion, the second approach is better.
Is this the official way to go when compiling ts to node ?
ie: targeting es6 in tsconfig ?
How can i be sure that the underlying node engine will effectively support those es6 features ? What if i'm using an old nodejs version without es6 support ?
I'd be glad to discuss to clearly understand the underlying mechanisms !
Thank you
Since node doesn't support completely all es6 features yet see : node green,
it's better to target es5 and install core-js types :
npm install #types/core-js

Importing modules in Angular2

As per understanding , Node js uses CommonJS module pattern , and in CommonJS
pattern we use require() to import a node module.
In Angular 2 application development we use #angular/core , #angular/common etc
node modules.
My question is:
Why do we use "import {} from '#angular/core'"[which is ES6 module syntax]
instead of commonJS require() syntax for accessing node modules
in angular2 code files.
The reason for this, is that Angular2 is written in TypeScript.
TypeScript is a superset of ES2015, and wants to be as close to ES2015 suggested syntax as possible. That's why you use the ES2015 import {} from syntax.
However, TypeScript also comes with a built-in transpiler (tsc). Meaning you write TypeScript code, but target a specific EcmaScript version in your tsconfig.json
When targetting ES5 and looking into the transpiled code, you will
clearly see that behind the scenes TypeScript will translate import {} from to require()in the transpiled files.
When targetting ES6, of course in the transpiled code, your imports will be ES2015 imports. Be aware that when targetting ES6, you will need babel to transpile your ES6 modules to ES5 or use System.js to load your ES6 modules in the browser.
Cheers

ts-node programmatic usage with require('ts-node/register')

ts-node suggests to use require('ts-node/register'). And this can be seen in angular2-webpack-starter Protractor configuration.
What is require('ts-node/register') supposed to do? Does it patch require to transpile TS files, so a part of Node.js application could be written in TypeScript?
It does what you think it does. require('ts-node/register') is actually the same as:
require('./').register({
lazy: true
})
See examples here: https://github.com/TypeStrong/ts-node/tree/master/register
The .register function registers the Typescript compiler for files with .ts and .tsx extension for compilation on the fly.

For --module should I use amd or commonjs?

When I put "export module" in a .ts file, it says I need to add --module.
That requires a KIND. If I'm using TypeScript, RequireJS, and Ext JS, is amd the correct kind?
Yes AMD is what RequireJS expects.

Resources