Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Traceur to 0.0.82 #80

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions compile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var path = require('path');

var Compiler = require('traceur').NodeCompiler
, xtend = require('xtend')
;
Expand All @@ -26,12 +28,23 @@ function buildTraceurOptions(overrides) {
return options;
}

exports = module.exports = function compileFile(file, contents, traceurOverrides) {
var options = buildTraceurOptions(traceurOverrides);
exports = module.exports = function compileFile(file, contents, opts) {
try{
var compiler = new Compiler(options);

var result = compiler.compile(contents, file, file);
var compiler = new Compiler(buildTraceurOptions(opts.traceurOverrides));

var outFile =
opts.basedir !== undefined ?
path.relative(opts.basedir, file) :
file;

// opts.basedir is passed here as the `sourceRoot` param, which traceur
// totally misappropriates. See
// https://github.com/google/traceur-compiler/issues/1676
// It's ok for here for now because it gets the
// correct path populated in `sources` and browserify overwrites sourceRoot.
// This should be revisited to see if a relative path should be passed in
// `file` and what `sourceRoot` value, if any, should be passed.
var result = compiler.compile(contents, file, outFile, opts.basedir);
}catch(errors){
return { source: null, error: errors[0] };
}
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ function getHash(data) {
* @param {string} src source of the file being compiled to ES5
* @return {string} compiled source
*/
function compileFile(file, src) {
function compileFile(file, src, opts) {
var compiled;
compiled = compile(file, src, exports.traceurOverrides);
opts.traceurOverrides = exports.traceurOverrides;
compiled = compile(file, src, opts);
if (compiled.error) throw new Error(compiled.error);

return compiled.source;
}

function es6ify(filePattern) {
filePattern = filePattern || /\.js$/;
function es6ify(opts) {
if (opts === undefined) opts = {};
if (opts.filePattern === undefined) opts.filePattern = /\.js$/;
else if (!(opts.filePattern instanceof RegExp)) {
throw new Error("`filePattern` must be a RegExp if defined.");
}
var filePattern = opts.filePattern;

return function (file) {

Expand All @@ -51,7 +57,7 @@ function es6ify(filePattern) {

if (!cached || cached.hash !== hash) {
try {
cache[file] = { compiled: compileFile(file, data), hash: hash };
cache[file] = { compiled: compileFile(file, data, opts), hash: hash };
} catch (ex) {
this.emit('error', ex);
return this.queue(null);
Expand Down Expand Up @@ -82,7 +88,9 @@ exports = module.exports = es6ify();
*
* @name es6ify::configure
* @function
* @param {string=} filePattern (default: `/\.js$/) pattern of files that will be es6ified
* @param {{filePattern: (undefined|!RegExp), basedir: (undefined|string)}=} opts
* filePattern (default: `/\.js$/`) pattern of files that will be es6ified
* basedir Base path to compute relative paths for `sources`
* @return {function} function that returns a `TransformStream` when called with a `file`
*/
exports.configure = es6ify;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"homepage": "https://thlorenz.github.io/es6ify/",
"dependencies": {
"through": "~2.2.7",
"traceur": "0.0.79",
"traceur": "0.0.82",
"xtend": "~2.2.0"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion test/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ var format = require('util').format;
src = 'window=this;'+src;
vm.runInNewContext(src, {
window: {},
console: { log: log }
console: { log: log },
// See:
// https://github.com/thlorenz/es6ify/issues/79
// https://github.com/joyent/node/issues/9121
Float32Array: Float32Array,
});
t.end()
});
Expand Down
199 changes: 136 additions & 63 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,145 @@ var test = require('tap').test
, convert = require('convert-source-map')
, compile = require('../compile')
, proxyquire = require('proxyquire')

test('transform adds sourcemap comment and uses cache on second time', function (t) {

t.plan(3);
var data = '';
var compiles = 0;

function trackingCompile() {
compiles++;
var args = [].slice.call(arguments);
return compile.apply(this, args);
// Cached file contents.
, files = {}

/**
* Check the cache for path, or read from fs and cache.
* @param {string} label
* @param {string} path
* @param {function} cb
* @returns {string}
*/
function readFileCache (label, path, cb) {
if (files[label] === undefined) {
fs.readFile(path, { encoding: 'utf8' }, readComplete);
}
else process.nextTick(function () {
readComplete(null, files[label]);
});

function readComplete (err, src) {
if (err) throw err;
files[label] = src;
cb(src);
}
// readComplete
}
// readFileCache

/**
* @function
* @param {{useSourceRoot: boolean}} opts
* useSourceRoot Do or don't specify sourceRoot to traceur.
*/
function addsSourceMap (t, opts) {
t.plan(3);
var data = '';
var compiles = 0;

function trackingCompile() {
compiles++;
var args = [].slice.call(arguments);
return compile.apply(this, args);
}

// Input / output paths
var paths = { out: {} };

paths.in = {
sourceRoot: path.join(__dirname, '..', 'example', 'src'),
// Path that should wind up in `sources` in the map when sourceRoot is used.
sources: path.join('features', 'iterators.js'),
};
paths.in.file = path.join(paths.in.sourceRoot, paths.in.sources);

var es6ifyOpts = {};
// Traceur misappropriates sourceRoot to apply to local input paths, so the
// public option for es6ify is named basedir.
if (opts.useSourceRoot) es6ifyOpts.basedir = paths.in.sourceRoot;
var es6ify = proxyquire('..', { './compile' : trackingCompile } )
es6ify = es6ify.configure(es6ifyOpts);

// File reads pending.
var pending = 0;

// Read some files in.
[
['input', paths.in.file],
[
'generated',
path.join(__dirname, 'transform', 'traceur-generated-template-parser.js'),
],
].forEach(function (file) {
pending++;
readFileCache(file[0], file[1], function () {
if (--pending === 0) fileReadsComplete();
})
});

function fileReadsComplete () {
// Run without, then with, `end()`
[undefined, end].forEach(function (end) {
fs.createReadStream(paths.in.file)
.pipe(es6ify(paths.in.file))
.on('error', function (e) { throw e; })
.pipe(through(write, end));
});
}
// fileReadsComplete

function write (buf) { data += buf; }
function end () {
var sourceMap = convert.fromSource(data).toObject();

if (opts.useSourceRoot) {
paths.out.sources = paths.in.sources;
paths.out.file = paths.out.sources;
paths.out.sourceRoot = paths.in.sourceRoot;
}
else {
paths.out.sources = path.basename(paths.in.sources);
paths.out.file = paths.in.file;
paths.out.sourceRoot = path.dirname(paths.in.file) + '/';
}

var es6ify = proxyquire('..', { './compile' : trackingCompile } )

var file = path.join(__dirname, '../example/src/features/iterators.js');

// first time
fs.createReadStream(file)
.pipe(es6ify(file))
.on('error', console.error)
.pipe(through(write));

// second time
fs.createReadStream(file)
.pipe(es6ify(file))
.on('error', console.error)
.pipe(through(write, end));
// Traceur converts all "\" to "/" so we need to do so also before comparing
Object.keys(paths.out).forEach(function (key) {
paths.out[key] = paths.out[key].replace(/\\/g, '/');
});

t.deepEqual(
sourceMap
, { version: 3,
file: paths.out.file,
sources: [
paths.out.sources,
'@traceur/generated/TemplateParser/2',
'@traceur/generated/TemplateParser/3',
],
names: [],
mappings: sourceMap.mappings,
sourceRoot: paths.out.sourceRoot,
sourcesContent: [
files.input,
files.generated.replace(/\s+$/, ''),
'void 0'
] }
, 'adds sourcemap comment including original source'
);
t.ok(sourceMap.mappings.length);
t.equal(compiles, 1, 'compiles only the first time');
}
}
// addsSourceMap

test("transform adds sourcemap comment, doesn't use sourceRoot, and uses cache on second time", function (t) {
addsSourceMap(t, { useSourceRoot: false });
});

function write (buf) { data += buf; }
function end () {
var sourceMap = convert.fromSource(data).toObject();

// Traceur converts all \s to /s so we need to do so also before comparing
var fileConverted = file.replace(/\\/g, '/');
var sourceRootConverted = path.join(path.dirname(file), path.sep).replace(/\\/g, '/');

t.deepEqual(
sourceMap
, { version: 3,
file: fileConverted,
sources: [ fileConverted, '@traceur/generated/TemplateParser/1' ],
names: [],
mappings: sourceMap.mappings,
sourceRoot: sourceRootConverted,
sourcesContent: [
'module.exports = function () {\n' +
' for (let element of [1, 2, 3]) {\n' +
' console.log(\'element:\', element);\n' +
' }\n' +
'};\n',

'\n for (var $__placeholder__0 =\n' +
' $__placeholder__1[\n' +
' $traceurRuntime.toProperty(Symbol.iterator)](),\n' +
' $__placeholder__2;\n' +
' !($__placeholder__3 = $__placeholder__4.next()).done; ) {\n' +
' $__placeholder__5;\n' +
' $__placeholder__6;\n' +
' }'
] }
, 'adds sourcemap comment including original source'
);
t.ok(sourceMap.mappings.length);
t.equal(compiles, 1, 'compiles only the first time');
}
test("transform adds sourcemap comment, uses sourceRoot, and uses cache on second time", function (t) {
addsSourceMap(t, { useSourceRoot: true });
});

test('transform does not add sourcemaps if traceurOverrides.sourceMaps is false', function (t) {
Expand Down
9 changes: 9 additions & 0 deletions test/transform/traceur-generated-template-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

for (var $__placeholder__0 =
$__placeholder__1[
$traceurRuntime.toProperty(Symbol.iterator)](),
$__placeholder__2;
!($__placeholder__3 = $__placeholder__4.next()).done; ) {
$__placeholder__5;
$__placeholder__6;
}