Search code examples
node.jsecmascript-6traceur

How do I set default options for traceur.compile and traceur.require?


Using the official traceur module, is it possible to set the default options for compile and require?

For example, this code works:

var traceur = require('traceur');
console.log(
    traceur.compile('{ let x = 1; }', { experimental:true }).js
);

Now if I remove traceur.compile's 2nd argument (the options object):

console.log(
    traceur.compile('{ let x = 1; }').js
);

Traceur will throw an error as the blockBinding option is not enabled. Is there any way to change the default options, in order to compile files without always passing an options object?

My main concern, apart from applying the DRY principle, is getting the traceur.require function to compile files with customized options -- as far as I can see, traceur.require and traceur.require.makeDefault() do not even take an options argument.

For instance, considering this code sample:

require('traceur').require('./index');

And this piece of code:

require('traceur').require.makeDefault();
require('./index');

Is there any way to compile the required file with the experimental option enabled?
Preferably by altering the default options, as I cannot see any other viable way.

Using Node 0.10.29 and Traceur 0.0.49.


Here's a full example of what I'd like to achieve.

bootstrap.js (entry point):

var traceur = require('traceur');
traceur.options.experimental = true;
traceur.require.makeDefault();
require('./index');

index.js:

import {x} from './lib';

// using a block binding in order to check
// whether this file was compiled with experimental features enabled
{
    let y = x;
    console.log(y);
}

lib.js:

export var x = (() => { 
    if (true) {
        // should be compiled with experimental features enabled too
        let x = 1;
        return x;
    }
})();

Expected console output: 1

traceur.options.experimental=true serves as a setter which enables the experimental features in the traceur.options object, but unfortunately traceur.options does not seem to affect traceur.compile nor traceur.require as far as I can see.


The Using Traceur with Node.js Wiki page does not mention anything about compiling options. The Options for Compiling page does not mention the Traceur API in Node.js, in fact, I cannot find any documentation about the Traceur API in Node.js.


Solution

  • Fabrício Matté ;-) added support for giving the default options to makeDefault(), see https://github.com/google/traceur-compiler/blob/master/src/node/require.js#L58

    A separate bug with the option experimental was fixed today, 16JUL14.