sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.59k stars 211 forks source link

Remaining module work #633

Open disnet opened 7 years ago

disnet commented 7 years ago

Sweet's module support is getting better, especially with recursive for syntax support #624. But we've still got a ways to go.

The major remaining areas of work are:

Importing for an arbitrary phase is relatively straightforward, just add support for syntax like import { x } from 'mod' for phase 2 and wire the phase number appropriately.

CommonJS will require a bit of design thought. Should we instantiate separately for each phase? Probably, but if not we could just reuse require in the NodeLoader. How should we detect CommonJS vs ES2015? Node core still hasn't decided this but hopefully we can just follow their cowpath.

Implicit runtime imports allow runtime module dependencies to be tracked through macro expansion. To do this right I think we basically have to turn Sweet into a ES2015 module bundler:

// a.js
// ========================
import f from 'f';

export syntax m = ctx => {
  return #`f()`;
}

// main.js
// ========================
import { m } from 'a.js'

m;

// should expand into:
// ========================
import f from 'f';
f();

// or even better with full bundling:
// ========================
let f = // implementation of f
f();

The design of this is cross-cutting with how we decide to handle CommonJS.

Since hygiene gives us so much scope information I think we'd get rollup-style tree-shaking optimizations "for free".