gobblejs / gobble

The last build tool you'll ever need
333 stars 20 forks source link

Question: Multiple outputs from single source? #81

Closed clintwood closed 9 years ago

clintwood commented 9 years ago

Given a single ./src directory is it possible to create a gobblefile.js that could 'build' more than a single output?

In this contrived example given gobblefile.js:

module.exports =
  gobble('./src')
    .transform('babel') // << put this transform in ./lib
    .transform( 'esperanto', {
      strict: true,
      sourceMap: false, 
      type: 'cjs'
    });

Doing $ gobble watch ./dist builds all transforms code to ./dist (which is expected).

How would I get the babel transform step to be output to say ./lib in the above case?

Rich-Harris commented 9 years ago

You can get part of the way there with this:

lib = gobble('src').transform('babel');

module.exports = gobble([
  lib.moveTo('lib'),
  lib.transform('esperanto', {...}).moveTo('dist')
]);

There isn't a way to write to multiple top-level directories (I tried gobble watch --force . just now and it nuked my project folder - oops!) so I guess the next best thing is symlinks?

ln -s .tmp/dist dist
ln -s .tmp/lib lib

gobble watch .tmp

With build rather than watch it's more straightforward obviously, you just need to copy the directories after building.

clintwood commented 9 years ago

Ahhh... no... sorry about the nuke!

I will fiddle with that, thanks. Ideally I'd like to do an intermediate build (to ./lib for easy debugging, etc.) then bundle into ./dist...

I'll close for now and re-open if I don't win! Thanks!

Rich-Harris commented 9 years ago

You could use environment variables - off the top of my head, though I can't try this out right now:

gobble watch lib  # environment defaults to 'development' - override with --env whatever
gobble build dist # defaults to 'production'
var prod = gobble.env() === 'production';

module.exports = gobble( 'src' )
  .transform( 'babel' )
  .transformIf( prod, 'esperanto-bundle', {...});
clintwood commented 9 years ago

The example I gave in the OP was a contrived example, what I'm really doing is taking the same source, ./src and building for different targets, potentially for iojs where I would not need that much 'babelifying' versus say for browser where most of the es6 would need to transformed to es5 and with different bundling configurations. So in a nutshell I'm looking for more access or control of the processing pipeline at the different transform stages... So potentially I would like to do a simple babel transform to iojs 'compatible' es6 then take that as the source and further transform or bundle for other targets (e.g. other node.js versions or browser targets). I have what I need for now but would be interested if you can see a way of doing this...

Rich-Harris commented 9 years ago

I sometimes find myself doing this sort of thing:

var nodes = [
  gobble( 'foo' ).transform( 'foo' )
];

if ( gobble.env() === 'bar' ) {
  nodes.push( gobble( 'bar' ).transform( 'bar' ) );
}

module.exports = gobble( nodes );

Between that and transformIf you can basically do whatever you need. Though if you find yourself coming up against the hard edges repeatedly and it seems like something Gobble should cater for, then I'm keen to hear about it.

clintwood commented 9 years ago

@Rich-Harris, sorry for not coming back to you earlier! I'm under time pressure for several projects so will revisit this when I get a gap! Thanks.

Rich-Harris commented 9 years ago

@clintwood not at all, I know exactly what it's like!