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

  • Training JavaScript Training JavaScript
  • TypeScript Language Overview TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code. For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.
  • ZeroNet Decentralized websites using Bitcoin crypto and the BitTorrent network. No single point of failure: Site remains online so long as at least 1 peer is serving it. No hosting costs: Sites are served by visitors. Impossible to shut down: It's nowhere because it's everywhere. Fast and works offline: You can access the site even if Internet is unavailable.
  • Training JavaScript - 3. Angular for Frontend Development Angular (commonly referred to as 'Angular 2+' or 'Angular 2') is a TypeScript-based open-source front-end web application platform led by the Angular Team at Google and by a community of individuals and corporations to address all of the parts of the developer's workflow while building complex web applications. Angular is a complete rewrite from the same team that built AngularJS.
  • Async JavaScript and ReactiveX RxJS RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code.
  • Training JavaScript - 2. Create Server by Node.js (JavaScript version) Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Historically, JavaScript was used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage's HTML, to be run client-side by a JavaScript engine in the user's web browser. Node.js enables JavaScript to be used for server-side scripting, and runs scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

Comments

comments powered by Disqus
Back to Top