Compiling Offline

arindam89 edited this page Feb 26, 2015 · 1 revision

How to use the traceur to compile to JavaScript files from the command line.

Traceur includes a shell script traceur to let you compile JavaScript code with ES6 and Traceur experimental features down to vanilla ES5. To use the generated file you will need to include a runtime file (bin/traceur-runtime.js). This runtime file contains polyfills as well as helper functions which reduce the generated code size.

Compiling Files

First, make sure you have Node installed. Then create a Javascript file with some ES6 features extensions in it. For example:

// greeter.js
class Greeter {
  sayHi(name = 'Anonymous') {
    console.log(`Hi ${name}!`);

var greeter = new Greeter();

Invoke the compiler like so:

  $ ./traceur --out out/greeter.js --script greeter.js

This will create an out/ directory containing greeter.js which has all ES6 features compiled to plain ES5 JavaScript.

By default, the input files to the command line compiler are treated as anonymous modules, and thus get wrapped. If you want to treat these files as script (global code) use one or more --script <filename> command line flags.

Using Compiled Files

We can test the greeter.js we just compiled with a minimal HTML page like:

    <script src="bin/traceur-runtime.js"></script>
    <script src="out/greeter.js"></script>
  You should see "Hi Anonymous!" in your console.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.