Tiven Wang
Wang Tiven June 14, 2017
425 favorite favorites
bookmark bookmark
share share

traceur

traceur

npm install --save-dev traceur

Option 1:

./node_modules/.bin/traceur --out public/index.js index.js

node ./public/index.js

Option 2:

bootstrap.js:

// bootstrap.js
var traceur = require('traceur');
traceur.require.makeDefault(function(filename) {
  // don't transpile our dependencies, just our app
  return filename.indexOf('node_modules') === -1;
});
require('./index');

index.js:

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(length) {
    // Here, it calls the parent class' constructor with lengths
    // provided for the Polygon's width and height
    super(length, length);
    // Note: In derived classes, super() must be called before you
    // can use 'this'. Leaving this out will cause a reference error.
    this.name = 'Square';
  }

  get area() {
    return this.height * this.width;
  }

  set area(value) {
    this.area = value;
  }

  draw() {
    console.log(this.area);
  }
}

new Square(10).draw();
$ node bootstrap.js
100

Using Traceur with Node.js

Babel

babel

Install the cli and preset of babel:

npm install --save-dev babel-cli

npm install --save-dev babel-preset-env

Create the babel’s environment file .babelrc:

{
  "presets": ["env"]
}

Compile JavaScript files in folder src into folder lib:

./node_modules/.bin/babel src -d lib

Typescript

npm install -g typescript

tsc helloworld.ts

node helloworld.js

If you want add more options for typescript compiler, please add a file named tsconfig.json in the root of your project.

If you want read more knowledge about TypeScript and Node, refer to TypeScript Node Starter

References

Similar Posts

Comments

Back to Top