google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 580 forks source link

API request: Recursive module compile from string to string #1626

Open sindresorhus opened 9 years ago

sindresorhus commented 9 years ago

Both grunt-traceur and gulp-traceur could use a simple API for getting a single file output from a module and its dependencies.

https://github.com/aaronfrost/grunt-traceur/issues/21 https://github.com/sindresorhus/gulp-traceur/issues/17 (and https://github.com/sindresorhus/gulp-traceur/pull/53)

arv commented 9 years ago

Totally agree.

We should also make it an option to embed a minimal runtime in this case (but lets not let that distract this issue).

johnjbarton commented 9 years ago

Our current API

var traceurAPI = require('./api.js');
var out = './output.js';
var sources =  {name: 'input.js', type: 'module'};
var options = {};
traceurAPI.recursiveModuleCompileToSingleFile(out, sources, options).then(function() {
      process.exit(0);
    }).catch(function(err) {
      var errors = err.errors || [err];
      errors.forEach(function(err) {
        console.error(err.stack || err);
      });
      process.exit(1);
    });

How can we improve this?

sindresorhus commented 9 years ago

Did not know about that API. Though the gulp plugin would need ability to supply input as a string and get output as a string and support for source maps.

See: https://github.com/sindresorhus/gulp-traceur/pull/53/files

// @epithet @origin1tech @guybedford @davidjnelson

origin1tech commented 9 years ago

I don't believe that "recursiveModuleCompileToSingleFile" is publicly exposed. Haven't looked in a few weeks so correct me if I'm wrong. For the purpose of plugins it could be exposed in:

compilter = new traceur.NodeCompiler(options);

Also recursiveModuleCompileToSingleFile could have an option to return the stream rather than writing out to file. This could be a param in the signature or perhaps more clear an additional method say recursiveModuleCompileToStream

Plugins will need see (https://github.com/origin1tech/gulp-traceur-compiler/blob/master/lib/recursiveModuleCompile.js#L48)

   compiled = compiler.write(tree, filePath);  

as opposed to below for obvious reasons see (https://github.com/google/traceur-compiler/blob/master/src/node/recursiveModuleCompile.js#L55)

   compiler.writeTreeToFile(tree, resolvedOutputFile); 

That said and as @sindresorhus will attest, although I was hot originally on single file output I've pull back on that. The more you work with traceur/6to5 the more single file libs feel counterproductive. That's purely my perspective for what that's worth.

johnjbarton commented 9 years ago

I think we want the API to be [<VinylFile>] -> [<VinylFile>]. Then the gulp-traceur can interface with streams. The concat should be done by another gulp plugin. That way the plugins like gulp-remember and glup-cache can work.

origin1tech commented 9 years ago

I see, my question would then be how would concating in the proper order based on import tree be handled? That's the part that gets sticky.

johnjbarton commented 9 years ago

Each <VinylFile> will be a module registration; the root modules will also need to be pulled from the registry to trigger evaluation. So they don't need to be in order but they all need to exist. But your point is broadly true: maybe it's not that simple after all.

paynecodes commented 9 years ago

:+1: for order of files based on import tree