esperantojs / esperanto

DEPRECATED: An easier way to convert ES6 modules to AMD and CommonJS
http://esperantojs.org
234 stars 21 forks source link

RFC - supply code and AST to esperanto #141

Closed Rich-Harris closed 9 years ago

Rich-Harris commented 9 years ago

First crack at #140. Would welcome feedback (cc @sebmck). It basically follows the proposal in #140, with one thing to be aware of - the properties of the modules option are paths relative to the base dir (whether specified as base or implicit, i.e. process.cwd().

This is necessary because import foo from './foo' could refer to foo/index.js (esperanto works that way because CommonJS works that way... open for discussion - removing support for index.js would simplify some things, but I kinda like it), which means that relative paths in foo need to know whether they're relative to foo.js or foo/index.js.

So usage looks like this:

esperanto.bundle({
  entry: 'main',
  base: 'src',
  modules: {
    'main.js': {
      code: '...',
      ast: {...}
    },
    'foo.js': '/* if no AST is supplied, esperanto generates a fresh one */'
  }
};

The supplied AST should have start and end character offset properties on each node - acorn includes these by default.

sebmck commented 9 years ago

Would it be possible to get the ast option on getStandaloneModule too? That'll be what Babel would use by default, at least for the time being.

Rich-Harris commented 9 years ago

Yep, that works too:

esperanto.toCjs({
  code: '...',
  ast: {...}
}, options );
sebmck commented 9 years ago

Ah awesome! Was digging through the code but didn't see it anywhere. Thanks!

sebmck commented 9 years ago

Alright, so I've basically got this integrated and it's working surprisingly well. The only issue so far is that esperanto will always put 'use strict'; at the top. Babel already does this so it leads to it being duplicated. I could make Babel not do that but there are some scenarios where people want modules but not strict mode (it's insane i know). A strictMode option that defaults to true that I can just set depending on whether it's required would be awesome.

Rich-Harris commented 9 years ago

Damn, you work fast.

there are some scenarios where people want modules but not strict mode (it's insane i know)

O_o I don't know what to say to that...

@thejameskyle raised a related issue recently (https://github.com/esperantojs/esperanto/issues/121) and the consensus was that it wasn't worth giving people the option, but I suppose this changes things. Will get onto it shortly and include it in this PR.

Rich-Harris commented 9 years ago

Released in 0.6.27. To disable the 'use strict' pragma, pass useStrict: false